logger Command in Linux



logger is a command used in Linux for writing messages to the system log. It is a handy tool for both system administrators and developers since it allows them to add custom log entries from the command line or within scripts.

By using logger, you can track events, debug issues, or simply record informational messages. This command sends messages to the syslog daemon, which then handles the login based on your system's configuration.

Table of Contents

Here is a comprehensive guide to the options available with the logger command −

Syntax of logger Command

Here's how you can structure the logger command on a Linux system −

logger [options] [message]

Where,

  • [options] are various flags you can use to modify the behavior of the logger command.
  • [message] is the message you want to log. If no message is specified, the logger reads from the standard input.

logger Command Options

Below are several options you can utilize with the logger command −

Options Description
-d Uses UDP datagrams instead of the default UNIX domain sockets.
-f file Logs the contents of a specified file.
-i Logs the process ID (PID) of the logger process.
-n server Specifies a remote syslog server to send the message to.
-P port Specifies the port number for the remote syslog server.
-p priority Specifies the priority of the log message (e.g., user.notice).
-s Logs the message to standard error as well as the system log.
-S size Sets the maximum size of the log message in bytes.
-t tag Adds a custom tag to each log entry.
-T Uses TCP instead of the default UDP for remote logging.
-u socket Logs to a specific UNIX socket instead of the default.

Examples of logger Command in Linux

Let's dive into some practical examples of using the Linux logger command −

  • Basic Logging
  • Logging with a Tag
  • Logging with a Priority
  • Logging from a File
  • Including the Process ID
  • Using UDP for Logging
  • Login to a Remote Syslog Server

Basic Logging

Using the command logger, you can log a simple message to the system log. For example −

logger "This is a test log message"

This command writes "This is a test log message" to the system log, which is useful for recording basic events and messages.

logger Command in Linux1

Logging with a Tag

You can also add a custom tag to your log entries using the logger command on Linux. For example −

logger -t myscript "This is a tagged log message"

This logs the message with the tag myscript, making it easier to identify the source of the log entry, which is helpful for organizing and tracking log messages from different scripts or processes.

logger Command in Linux2

Logging with a Priority

To specify the priority level of the log message, you can use −

logger -p user.notice "This is a notice message"

This logs the message with a priority of user.notice, ensuring it is categorized appropriately in the system log and processed based on its significance.

logger Command in Linux3

Logging from a File

You can also log the contents of a specific file by using −

logger -f /path/to/file

This command reads the contents of the file at /path/to/file and logs it to the system log, which is particularly useful for capturing and storing log data from files.

Including the Process ID

In order to include the process ID (PID) in the log entry, use −

logger -i "This log entry includes the PID"

This adds the PID of the logger process to the log message, making it easier to trace back the source process, which is invaluable for debugging and system monitoring.

logger Command in Linux4

Using UDP for Logging

You can also opt to use UDP datagrams for logging instead of UNIX domain sockets −

logger -d "This message uses UDP"

This sends the log message using UDP, which can be beneficial for remote logging where network communication needs to be lightweight and efficient.

logger Command in Linux5

Logging to a Remote Syslog Server

For sending log messages to a remote syslog server, use −

logger -n syslog.example.com "Remote log message"

This command sends the log message to the remote syslog server at syslog.example.com, which is advantageous for centralized logging across multiple systems.

That's how the logger command is used in Linux systems.

Conclusion

The logger is an essential command in Linux for writing messages to the system log, facilitating resource access management and debugging. It ensures that events are tracked, issues are debugged, and informational messages are recorded efficiently.

In this tutorial, we walked you through the installation process, provided a detailed explanation of the syntax, explored various options, and offered practical examples. With this knowledge, you can effectively utilize the logger command to maintain robust logging practices and streamline system monitoring in multi-tasking environments.

Advertisements