How to use logger Command on Linux?

Introduction

Before we begin talking about how to use logger Command on Linux, let's briefly understand – What is logger command?

The logger command in Linux inserts the entries to the /var/log/syslog file. Each activity taken while using the Linux distribution is recorded in this file. The logger command gives users the ability to edit the log file.

In this tutorial, you will use logger Command on Linux. We will also address a few FAQs on how to use logger Command on Linux.

How Does the logger Command Work on Linux?

The logger command on Linux has the following general syntax:

logger [options] [log]

The following is an explanation of the general syntax for the logger command on Linux:

  • The command will be revoked by logger.
  • The Logger command has various options that can be utilized.
  • Enter the entry that will be added to the log.

The various logger command options that can be used are as follows:

d - It is used to connect using UDP.

e - It is employed to ignore the file's empty lines.

f - It is utilized to log the contents in the given file.

i - It is used to record the logger process' PID.

p - The port can be specified using it.

Different examples are presented to illustrate how to use the logger command on Linux in the next section.

Examples of the logger Command on Linux

We will first use the tail command to see the last 10 lines of the /var/log/syslog file before demonstrating how to use the logger command:

tail -n 10 /var/log/syslog

We will change various entries in the /var/log/syslog file using the logger.

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

The logger command can be used to add any comment to the /var/log/syslog file. As an example, we will use the logger command and add the comment "Welcome to vegastack":

logger "Welcome to vegastack"

The following line is inserted at the end of the /var/log/syslog file so that it can be displayed:

tail  /var/log/syslog

The syslog log file now includes the comment.

Example 2: Add the Standard Output

The standard output of any command that has been run can also be added to the syslog file. For example, we'll include the hostname command's output in the syslog file:

logger `hostname`

Display the output of the /var/log/syslog file to verify that the aforementioned command was successfully executed:

tail  /var/log/syslog

Example 3: Add the Contents of the Specified File

As an example, we have a file whose contents may be added to the log file using the "f" option with the command: We can also add the contents of any specified file in the syslog.

logger -f mylogfile.txt

Once more, use the tail command to see the syslog file's contents in order to verify that the aforementioned command was properly executed:

tail  /var/log/syslog

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

The file has two lines in the above example, but there is an empty line between them that can be disregarded by using the "e" option:

logger -e -f mylogfile.txt

Show the syslog file:

tail  /var/log/syslog

The empty line was disregarded.

That's all there is to know about using the logger command on Linux.

FAQs to Use logger Command in Linux

How can I specify the log level when using the "logger" command? 

You can specify the log level using the "-p" or "--priority" option, followed by the desired priority level (e.g., "user.notice" or "local7.warning").

Can I log a message with a specific timestamp using "logger"? 

Yes, you can use the "-t" or "--tag" option with a timestamp to log a message with a specific timestamp.

How can I specify a custom log file when using the "logger" command? 

By default, "logger" sends messages to the system log files, but you can redirect the output to a specific log file using the ">>" operator, followed by the file path.

Can I log messages to a remote syslog server using "logger"? 

Yes, you can log messages to a remote syslog server using the "-n" or "--server" option, followed by the server's IP address or hostname.

How do I include additional information in the log messages with "logger"? 

You can include additional information by using the "-i" or "--id" option, followed by your desired identifier.

Can I log messages with "logger" as a specific user? 

Yes, you can use the "-u" or "--socket-user" option to specify the username whose credentials will be used to log the message.

How can I view the logged messages using "logger"? 

Logged messages can be viewed using tools like "tail" or "cat" to read the system log files, such as "/var/log/syslog" or "/var/log/messages".

Conclusion

The logger command on Linux can be used to add entries to the syslog file by using the syntax "logger [options] [log]". The syslog file keeps a history of all operations made in the Linux shell. This tutorial uses a variety of examples to demonstrate how to utilize the logger command utility on Linux.