How to use logger Command in Linux?

The logger command in Linux adds the entries in the /var/log/syslog file. This file contains all the actions performed in the Linux distribution. The logger command allows users to make amendments to the log file with the logger command.

This blog will explain the usage of the logger command in Linux with the help of examples the blog contents are:

Let’s start the blog with the general syntax of using the logger command in Linux.

How Does the logger Command Work in Linux?

The general syntax of using the logger command in Linux is:

$ logger [options] [log]

The explanation of the general syntax of using the logger command in Linux is:

  • Logger will revoke the command
  • Different options of the Logger command can be used
  • Enter the entry which is supposed to add in the log

The different options of the logger command which are optional to use are:

dIt is used to for the utilization of the UDP for the connectivity
eIt is used to ignore the empty lines in the file
fIt is used to log the contents of the specified file
iIt is used to logging the PID of the logger process
PIt is used for specifying the port

In the next section, different examples are being used to explain the usage of the logger command in Linux.

Examples of the logger Command in Linux

To explain the usage of the logger command, we will first display the last 10 lines of the /var/log/syslog file with the tail command:

$ tail -n 10 /var/log/syslog

Using the logger, we will make different amendments in the /var/log/syslog file.

Example 1: Add Log in the /var/log/syslog File

To add any comment in the /var/log/syslog file, we can use the logger command. For example, we will add the comment “Welcome to itsLinuxFoss” with the logger command:

$ logger “Welcome to itsLinuxFoss”

The line is added in the last of the /var/log/syslog file so that we will display the syslog file:

$ tail  /var/log/syslog

The comment has been added in the log file of syslog.

Example 2: Add the Standard Output

We can also add the standard output of any executed command in the syslog file. For example, we will add the output of the hostname command in the syslog file:

$ logger ‘hostname’

To confirm the execution of the above command, display the output of the /var/log/syslog file:

$ tail  /var/log/syslog

Example 3: Add the Contents of the Specified File

We can also add the contents of any specified file in the syslog, for example, we have the file whose contents can be added to the log file using the “f” option with the command:

$ logger -f mylogfile.txt

Again, to confirm the execution of the above command, display the contents of the syslog file using the tail command:

$ tail  /var/log/syslog

Example 4: Add the Contents of the Specified by Ignoring the Empty Line

In the previous example, we can see an empty line between the two lines of the file which can be ignored by using the “e” option:

$ logger -e -f mylogfile.txt

Display the syslog file:

$ tail  /var/log/syslog

The empty line has been ignored.

That’s all about the usage of the logger command in Linux.

Conclusion

To add the entries in the syslog file, use the logger command in Linux by following the command syntax “logger [options] [log]”. The syslog file records the history of every action performed in the Linux shell. In this post, different examples are used to explain the usage of the logger command utility in Linux.