As a Linux user, you’ve likely come across the logger command at some point. This handy tool allows you to record messages, or “logs,” from the command line or within scripts. But there’s more to logger than meets the eye – keep reading to learn some lesser-known features and practical examples for using logger to boost your efficiency.
What Is the Linux Logger Command?
Simply put, logger is a command-line utility for sending log messages to the system logging daemon, syslogd. These logs can be used to track system events, monitor the behavior of scripts and applications, and troubleshoot problems.
Here’s the basic syntax for using logger:
logger [options] message
message can be any string of text, and there are a variety of options available for customizing the log entry. For example, you can specify the log level (e.g.
-p local0.notice), add a tag to the log message (e.g.
-t mytag), or write the log to a specific file (e.g.
5 Examples of Using Linux Logger
- Debugging a script: If you’re working on a script and something isn’t quite right, you can use logger to print out debugging messages as the script runs. This can help you track down the source of the problem.
- Monitoring system events: By default, logger writes log messages to the syslog facility, which is a common repository for system logs. You can use logger to record important events, such as when a service starts or stops, or when a user logs in or out.
- Sending log messages to a remote server: With the
noption, you can specify the hostname or IP address of a remote syslog server to which you want to send log messages. This can be useful for centralizing logs from multiple systems or for offloading log storage to a dedicated server.
- Writing logs to a file: If you want to save log messages to a specific file, you can use the
foption. This can be useful for creating a separate log for a specific script or application, or for making it easier to search through log entries.
- Customizing the log level: By default, logger writes log messages at the “user” level. However, you can use the
poption to specify a different log level, such as
err. This can be useful for filtering log messages based on importance.
Directly to Console
-e option to send log messages directly to the console. The message will be printed to the screen in addition to being logged. This can be a quick and easy way to display messages to the user without using
Key Points to Remember
- The logger command is a handy tool for recording log messages from the command line or within scripts.
- You can customize the log entry with options like
- You can send log messages to a remote syslog server or write them to a file.
eoption allows you to print log messages to the console.
Challenge: Test Your Knowledge
Try using logger to send a log message to a remote syslog server and write the log to a file on your local system. Use the following command:
logger -p local0.notice -t mytag -f /path/to/logfile -n syslog.example.com "This is a test log message"
This command will send the log message “This is a test log message” to the syslog facility on the remote server
syslog.example.com, with a log level of
notice and a tag of
mytag. It will also write the log message to the file
/path/to/logfile on your local system.