4 Ways to View or Monitor Log Files in Real Time

by time news

How can I view the contents of a log file in real time on Linux? Well, there are many utilities that can help a user to output the content of a file while the file is continually changing or updating. One of the best known and most widely used utilities to display the contents of a file in real time in Linux is the tail (manage files effectively) command.

1. tail command: monitor logs in real time

As it was told, the tail command is the most common solution to display a log file in real time. However, the command to display the file has two versions, as illustrated in the following examples.

In the first example, the command tail needs the argument “-f” to follow the contents of a file.

$ sudo tail -f /var/log/apache2/access.log

o en Fedora / AlmaLinux / RHEL.

tail -f /var/log/httpd/access_log

Monitor Apache logs in real time

The second version of the command is actually a command itself: tailf. You will not need to use the ” argument-f” because the command is integrated with the “-f” .

You don’t need to install anything, if you didn’t want to you could create an alias for that purpose by typing:

alias tailf=”tail -f”

Monitoring Apache logs in real time

$ sudo tailf /var/log/apache2/access.log

Typically, the logrotate utility rotates log files frequently on a Linux server. To view the log files that are rotated daily, you can use the tail command with the -F argument.

will perform “tail -F” a trace if a new log file is created and it will start to trace the new file instead of the old one.

$ sudo tail -F /var/log/apache2/access.log

However, by default, the tail command will show the latest 10 lines of a file. For example, if you want to see in real time only the last two lines of the log file, use the “-n” file combined with the “-f” flag, as shown in the following example.

$ sudo tail -n2 -f /var/log/apache2/access.log

2. Multitail Command – Monitor multiple log files in real time

Another interesting command to display log files in real time is the multitail command. The command name implies that the multitail utility you can monitor and track multiple files in real time. Multitail also allows you to navigate back and forth in the monitored file.

To install the multitail utility On Debian and RedHat based systems, issue the following command.

$ sudo apt install multitail [En Debian y Ubuntu]
$ sudo yum install multitail [En RedHat y CentOS]
$ sudo dnf install multitail [En la versión Fedora 22+]

To display the output of two log files simultaneously, run the command as shown in the following example.

$ sudo multitail /var/log/apache2/access.log /var/log/apache2/error.log

multiqueue monitor logs

3. lnav command: Monitor multiple log files in real time

Another interesting command, similar to multitail command, is the lnav command. The Lnav utility You can also view and follow multiple files and display their content in real time.

To install the lnav utility on Debian and RedHat based Linux distributions by issuing the following command.

$ sudo apt install lnav [En Debian y Ubuntu]
$ sudo yum install lnav [en RedHat y CentOS]
$ sudo dnf install lnav [En la versión Fedora 22+]

Observe the contents of two log files simultaneously by issuing the command as shown in the following example.

$ sudo lnav /var/log/apache2/access.log /var/log/apache2/error.log

lnav – Real-time log monitoring

4. Less command: show real-time output of log files

Finally, you can show the live output of a file with less command if you type “Shift+F” .

The same as the tail utility by pressing “Shift+F” in a file opened in any less it will start after the end of the file. Alternatively, you can also start minus with minus”+F” flag to enter the live view of the file.

$ sudo less +F /var/log/apache2/access.log

View records using the less command

And these are the commands that we have presented in this article to monitor the logs in real time. Thank you and share this article among your friends.

Pin It

You may also like

Leave a Comment