Skip to main content

Syslog Example

What is Syslog

Syslog is a standard for logging program messages. It allows separation of the software that generates messages from the system that stores them and the software that reports and analyzes them. It also provides devices which would otherwise be unable to communicate a means to notify administrators of problems or performance.

Syslog can be used for computer system management and security auditing as well as generalized informational, analysis, and debugging messages. It is supported by a wide variety of devices (like printers and routers) and receivers across multiple platforms. Because of this, syslog can be used to integrate log data from many different types of systems into a central repository.

Messages refer to a facility (auth, authpriv, daemon, cron, ftp, lpr, kern, mail, news, syslog, user, uucp, local0, ... , local7 ) and are assigned a priority/level (Emergency, Alert, Critical, Error, Warning, Notice, Info or Debug) by the sender of the message.

Configuration allows directing messages to various local devices (console), files (/var/log/) or remote syslog daemons. Care must be taken when updating the configuration as omitting or misdirecting message facility.level can cause important messages to be ignored by syslog or overlooked by the administrator. Logger is a command line utility that can send messages to the syslog.

Configuring Syslog

Syslog has 8 configurable log files that are available for the programmer to use. These log files are referred to as LOG_LOCALx where x is an integer from 0 to 7.

Configure syslog with 
LOG_LOCALx  by adding following line in /etc/syslog.conf  file :

local0.* /var/log/testlog
This tells syslog that any logs written to the LOG_LOCAL0, should be written in '/var/log/testlog' file. 
After updating /etc/syslog.conf, restart the syslogd daemon :
/etc/init.d/syslog restart

Log using Syslog

Now as we have configured the LOG_LOCAL0. We can write debugging information to our log file through syslog, as shown below in test program :

#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
    openlog ("Test", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
    syslog (LOG_INFO, "Program started by User %d", getuid() );
    syslog (LOG_ERR, "ERROR!");
    closelog ();
}

The openlog function initiates syslog for our program. We just need to use once during the initiation of our program. Several options can be used to control the logging behavior, below is the explanation of the options used in the example above.

            Option                                                                    Meaning
LOG_CONS                  When syslog fails to submit a message, it writes the message to system console
LOG_PID                       Inserts the calling process' Process ID (PID) into the message
LOG_NODELAY          Open and connect to syslog
LOG_LOCAL0             Where to write the logs

The syslog call writes messages to our syslog facility LOG_LOCAL0. The first argument (LOG_INFO, LOG_ERR) specifies the log level or priority. This allows more fine tuning for example by specifying different logging files (or actions) for each logging level. See man -S3 syslog for the full list of debugging levels.

The format of the log messages is :
DATE TIME MACHINE-NAME PROGRAM-NAME[PID]: MESSAGE

Output :
Apr 7 21:36:41 centos-osp Test[20304]: Program started by User 0
Apr 7 21:36:41 centos-osp Test[20304]: ERROR!

Comments

Popular posts from this blog

JUnit – Run unit test in an Sequence / Order

In JUnit, we can use @FixMethodOrder(MethodSorters.xxx) to run the test methods in a sequence or order.

import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runners.MethodSorters;importstatic org.hamcrest.CoreMatchers.is;importstatic org.junit.Assert.assertThat;//Sorts by method name@FixMethodOrder(MethodSorters.NAME_ASCENDING)publicclassExecutionOrderTest{@TestpublicvoidtestB(){assertThat(1+1,is(2));}@Testpublicvoidtest1(){assertThat(1+1,is(2));}@TestpublicvoidtestA

Create Runnable Jar - Eclipse Options

When exporting to a Runnable Jar, there are three options in eclipse Helios. Extract required libraries into JARPackage required libraries into JARCopy required libraries into sub folder next to JAR. What are differences : Extract required libraries into JAR - Extracts the actual .class files from the libraries your app uses and puts those .class files inside the runnable JAR. So, the runnable JAR will not only contain the .class files of your application, but also the .class files of all the libraries your application uses. Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible. Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the .class files of your application. Option #2 is convenient be…