Skip to Content
Linux Security Cookbook
book

Linux Security Cookbook

by Daniel J. Barrett, Richard E. Silverman, Robert G. Byrnes
June 2003
Intermediate to advanced
336 pages
8h 54m
English
O'Reilly Media, Inc.
Content preview from Linux Security Cookbook

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Linux Security Cookbook - Second Edition

Practical Linux Security Cookbook - Second Edition

Tajinder Kalsi
Mastering Linux Command Line

Mastering Linux Command Line

Coding Gears | Train Your Brain

Publisher Resources

ISBN: 0596003919Errata Page