The logger command allows scripts to create and manage log messages:
- Place a message in the syslog file /var/log/messages:
$ logger LOG_MESSAGE
Consider this example:
$ logger This is a test log line
$ tail -n 1 /var/log/messages
Sep 29 07:47:44 slynux-laptop slynux: This is a test log line
The /var/log/messages log file is a general purpose log file. When the logger command is used, it logs to /var/log/messages by default.
- The -t flag defines a tag for the message:
$ logger -t TAG This is a message
$ tail -n 1 /var/log/messages
Sep 29 07:48:42 slynux-laptop TAG: This is a message
The -p option to logger and configuration files in /etc/rsyslog.d control where log messages are saved.
To save to a custom file, follow these steps:
- Create a new configuration file in /etc/rsyslog.d
- Add a pattern for a priority and the log file
- Restart the log daemon
Consider the following example:
# cat /etc/rsyslog.d/myConfig
local7.* /var/log/local7
# cd /etc/init.d
# ./syslogd restart
# logger -p local7.info A line to be placed in /var/log/local7
- The -f option will log the lines from another file:
$ logger -f /var/log/source.log