Logger - The logging tool

All Linux distributions and BSD systems (FreeBSD, OpenBSD, etc) come with the logger tool installed out of the box. Logger is a very simple tool that uses the syslog() function to send (and test) logs from the command line. In its basic form, the C code looks like this for logger:



#include <syslog.h>
syslog(pri, "%s", buf);


Which sends the content from buf (which is the argument from the command line) to the syslog() function with the user supplied priority (which is the facility+level).



This is a logging test

The first step to test logger is to go to your command line terminal and execute the command:



$ logger "This is a logging test"


Which will generate a log on your log file (by default /var/log/syslog or /var/log/messages) that looks like:

May 26 04:15:22 testingserver dcid: This is a logging test


Which is the date, followed by the hostname of the server (testingserver) and the username (dcid) that called the logger command. If you are testing if your logging is working, and you see that log line, it means it worked.



Specifying the facility and level with logger

But logger is more flexible than just that. Let's say you want to send all error events to a specific log file and want to verify it is working. You can pass the facility and level (error) from logger as well:

$ logger -p daemon.error "This is a logging error test"


The other useful option available is the "-t" that allows you to replace your username with the program name (id) to show up in the log:

$ logger -t logtester -p daemon.error "This is a logging error test"


And now you should see the following on the logs:

May 26 04:24:57 testingserver logtester: This is a logging error test


And that's it. Logger is easy and useful.



Posted in   log-tutorial     by Daniel Cid (@dcid)

Simple, affordable, log management and analysis.