dcsimg

Use Logger to Write Messages to Log Files

By Joe Brockmeier (Send Email)
Posted Feb 14, 2011


Want a quick and easy way to write to system logs? Check out logger, a shell utility that works with syslog.

Want a quick and easy way to write to system logs? Check out logger, an shell utility that works with syslog. Don't let its ease of use cause you to overlook its value.

The logger utility is part of the bsdutils package on Debian-based systems and the util-linux-ng package on Fedora (and presumably Red Hat Enterprise Linux, although I don't have a RHEL system handy to check). It should be installed by default.

The most common use for logger is as a lightweight way to pass messages from a script to logfiles. Say you've got a backup script or another job that runs while you're home snoozing. Rather than having the script send an email and clog up your inbox, you can add logger to your script, and check periodically, at your leisure, to make sure things went as planned -- or use logger to just log error messages when the job doesn't run as planned or encounters an error.

The basic syntax for logger is logger message. It doesn't require any options if you're just going to write a static message to the logfile and you want to write to the default /var/log/messages at the "notice" level. (See the syslog man page if you need info on the level names.)

To change the log level or log that you send messages to, use the -p option, like so:

logger -i -p mail.err "Oh Noes"

This will log "Oh Noes" to /var/log/mail.err with the process ID, the user that ran the utility, and (of course) the time stamp. In fact, it will also log into /var/log/syslog and /var/log/mail.warn, and the other mail logs. Crank down the warning level if you don't want it logged to as many mail logs.

Of course, you can send to other logs, just change the facility name you use with the -p option.

If you want to use a tag other than the username, the -t option will let you set a tag like "WARNING" or "SCRIPT" -- whatever you want to use to make it easy to search through logs or trigger a notice from another program watching your logfiles.

Another use for the logger utility is to write a message to system log from the command line. Want to make a note of why a system is rebooted? Just use logger message and replace "message" with the note you want to leave in the log.

The logger utility is a very simple tool for server admins, but don't let its ease of use cause you to overlook its value.

Joe 'Zonker' Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at jzb@zonker.net and follow him on Twitter.

Follow ServerWatch on Twitter

Page 1 of 1


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.