9.31. Sending Messages to the System Logger
Problem
You want to add information about interesting events to the system log.
Solution
Use the logger program. A simple example:
$ logger "using Ethereal to debug a network problem"
Suppose “food” is the name of a program, short for “Foo Daemon.” Log a simple message:
$ logger -t "food[$$]" -p local3.warning "$count connections from $host"
Direct stdout and stderr output to syslog:
$ food 2>&1 | logger -t "food[$$]" -p local3.notice &
Send stdout and stderr to syslog, using different priorities (bash only):
$ food 1> >(logger -t "food[$$]" -p local3.info) \ 2> >(logger -t "food[$$]" -p local3.err) &
You can also write to the system log from shell scripts [Recipe 9.32], Perl programs [Recipe 9.33], or C programs [Recipe 9.34].
Discussion
The system logger isn’t just for system programs: you can use it with your own programs and scripts, or even interactively. This is a great way to record information for processes that run in the background (e.g., as cron jobs), when stdout and stderr aren’t necessarily connected to anything useful. Don’t bother to create, open, and maintain your own log files: let the system logger do the work.
Interactively, logger can be used almost like
echo
to record a message with the default
user
facility and
notice
priority. Your username will be prepended
to each message as an identifier.
Our recipe shows a sample “Foo
Daemon” (food) that uses the
local3
facility and various priority levels, depending on the importance ...
Get Linux Security Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.