Site Overlay

Filtering logs in realtime with the magic of less

less is a program that allows you to navigate and search within a file or buffer. As such it’s extremely useful for monitoring and searching through log output.

Using shift + G and g allows you to navigate to the bottom and the top of a file. To search you use / to search forward and ? to do it backwards in the buffer.

My absolute favourite capability, and one that seems like magic the first time you use it, is to use & <filter> in conjunction with F. Just usingFwill monitor the file you are in and any new lines written to it will turn up in the pager, just like if you used tail -f, but with the added benefit of being in a pager. That is really useful on its own. & <filter is what really brings it to the next level. It gives you the ability to filter logs in real time.

Say that you’re trying to investigate an issue in your web app. It only occurs when calling a specific endpoint and due to the overwhelming volume of requests to all other endpoints you can’t just monitor the log output as is. All you do is to open the log with less +F <logfile> and then press & and type the filter you need. Suddenly you only see the output you need, and your work is so much easier.

What’s your favourite way to work with less?

Leave a Reply

Your email address will not be published. Required fields are marked *