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 :

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!


Popular posts from this blog

Exploring Node.js Internals

I found a great article explaining Node JS internals, must read :

Some other articles :

Introduction to Node.jsBeing an official website, explains what Node.js is, as well as its package managers, and lists web frameworks built on top of it.“JavaScript & Node.js”, The Node Beginner BookThis book by Manuel Kiessling does a fantastic job of explaining Node.js, after warning that JavaScript in the browser is not the same as the one in Node.js, even though both are written in the same language.Beginning Node.jsThis beginner book goes beyond an explanation of the runtime. It teaches about packages and streams and creating a web server with the Express framework.LibUVThis is the official documentation of the supporting C++ code of the Node.js runtime.V8This is the official documentation of the JavaScript engine that makes it possible to write Node.js with JavaScript. x

How to check if environment variable is set using shell script?

If you are wondering how can we check whether an environment variable is set or not in the system, here are few methods :
1. Manually a. Run “set” command on the shell prompt, it will list out all the variables set in the system : # set b. Run “echo” command on the shell to print the value of the environment variable : # echo $<environment variable>
2. Through Shell Script Let say we intent to check whether environment variable “envVar” is set or not?
a. Method 1 – checking if “envVar” is set to “” (no value) : if [ "$envVar" == "" ] then echo “envVar is not set” else echo “envVar is set” fi
b.Method 2 – checking if “envVar” is non NULL : if [ -n "$envVar" ] then echo “envVar has non NULL value, it is set.” else echo “envVar has NULL value, it is not set.” fi