How to use the command logger (with examples)
The logger command is a utility program that allows you to add messages to the system log (syslog) in Unix-like operating systems. This can be useful for debugging, system monitoring, or generating system logs. The logger command takes input from either a message specified as an argument or from standard input.
Use case 1: Log a message to syslog
Code:
logger "This is a log message"
Motivation: Logging messages to syslog is a common practice in system administration and troubleshooting. By using the logger command, you can easily add informative log messages to the system log files.
Explanation: In this use case, the logger command is used with a message specified as an argument. The message, enclosed in double quotes, will be logged to the syslog. By default, the syslog file is located at /var/log/syslog
.
Example Output: The message “This is a log message” will be added to the system log.
Use case 2: Take input from stdin and log to syslog
Code:
echo "This is a log entry" | logger
Motivation: Sometimes, you may need to log the output of a command or script directly to the syslog. By using the logger command in conjunction with other commands like echo, you can effortlessly log information from standard input to the system log.
Explanation: In this use case, the echo command is used to generate a log entry. The output of the echo command, "This is a log entry"
, is piped (|
) to the logger command. The logger command then logs the entry to the syslog.
Example Output: The log entry “This is a log entry” will be added to the system log.
Use case 3: Send output to a remote syslog server running at a given port
Code:
echo "This is a log entry" | logger --server hostname --port port
Motivation: In some cases, it may be necessary to send log entries to a remote syslog server for centralized logging and monitoring. By specifying the remote syslog server’s hostname and port with the logger command, you can direct log entries to the desired destination.
Explanation: In this use case, the echo command generates a log entry. The output of the echo command is piped to the logger command. The --server
argument is used to specify the hostname of the remote syslog server, and the --port
argument is used to specify the port number on which the syslog server is listening.
Example Output: The log entry “This is a log entry” will be sent to the remote syslog server running at the specified hostname and port.
Use case 4: Use a specific tag for every line logged
Code:
echo "This is a log entry" | logger --tag tag
Motivation: Adding a tag to each log entry can provide additional information for filtering and identifying log messages. By using the --tag
option with the logger command, you can assign a specific tag to every line logged.
Explanation: In this use case, the echo command generates a log entry. The output of the echo command is piped to the logger command. The --tag
argument is used to specify the desired tag for each log entry.
Example Output: The log entry “This is a log entry” will be added to the system log with the specified tag.
Use case 5: Log messages with a given priority
Code:
echo "This is a warning" | logger --priority user.warning
Motivation: Log messages can have different priorities, such as informational, warning, or error. By using the --priority
option with the logger command, you can specify the desired priority for the log messages.
Explanation: In this use case, the echo command generates a log entry. The output of the echo command is piped to the logger command. The --priority
argument is used to specify the desired priority for each log entry. In this example, the priority is set to user.warning
.
Example Output: The log entry “This is a warning” will be added to the system log with the specified priority.
Conclusion:
In this article, we explored various use cases of the logger command. By simply leveraging the power of the logger command, you can easily add informative log messages to the system log. Whether you want to log a message, send logs to a remote syslog server, or customize the log tags and priorities, the logger command provides the flexibility and functionality you need for efficient system logging and monitoring.