In this article, we will explore the syslog logging framework.The article aims at filtering the records from syslog and create a Static Custom Log file from sysLog In Ubuntu.
It is a very common requirement in almost every project to have a Logging Framework for logging the important informations about the project.This basically comes into handy during the production phase in-order to figure out from where the bugs creeped in.In C paradigm so far the LINUX/UNIX platform is concern, there are many such logging libraries available.In this article, we will explore the syslog logging framework.The article aims at filtering the records from syslog and create a Static Custom Log file from sysLog In Ubuntu. The current environment where the experiment has done is Ubuntu 12.04 version.
What is Syslog?
In short,it is a Event based Message logging framework,defined in RFC 3164,developed in the 1980s by Eric Allman as part of the Sendmail project.It sends the logging messages to the Syslog Server.The components of syslog is defined in a header file name syslog.h
The below are the important functions/macros defined inside the syslog.h
||openlog(const char *, int, int)
||It helps to open a connection to the logging facility.Some commonly use CONSTANTS for facility are LOG_USER,LOG_KERN,LOG_LOCAL0...LOG_LOCAL7 etc.
||syslog(int, const char *, ...)
||This function logs the message.The priority argument can accept any of the CONSTANTS like LOG_INFO,LOG_ERR,LOG_WARNING etc.
||This function closes the open file descriptors.
||It sets the log priority mask.
Using the code
Let us first go to the terminal, and open the VIM editor and write the below program
int main(int argc, char *argv)
openlog(NULL, LOG_PID, LOG_USER);
syslog(LOG_INFO, "test by niladri");
Save the file as MyCustomLogger.c.Compile the program using the below command
gcc -MyCustomLogger.c -o MyCustomLogger
Now visit the /etc/rsyslog.d.Quoting Wikipedia
Rsyslog is an open-source software utility used on UNIX and Unix-like computer systems for forwarding log messages in an IP network. It implements the basic syslog protocol, extends it with content-based filtering, rich filtering capabilities, flexible configuration options and adds features such as using TCP for transport.
Now there are two config files inside the /etc/rsyslog.d namely 20-ufw.conf and 50-default.conf alerady present.We need to first create our own .conf file.For that we need to trigger the below command
sudo nano /etc/rsyslog.d/30-mycustomlogger.conf
The purpose of using sudo is to give permissions to any particular command that a user wants to execute from the root/admin privilege.The user needs to enter user password to give system based permissions.
nano is a text editor for UNIX
The moment we enter the command, the nano text editor opens for us with the file name 30-mycustomlogger.conf.Then enter the below content
# Log messages to file MyCustomLogger.log
if $programname == 'MyCustomLogger' then /var/log/MyCustomLogger.log
# Uncomment the following to stop logging anything that matches the last rule.
We have used the Filtering by program name using the expression-based syntax of RSYSLOG.Interested readers can find more information about the Filters of RSYSLOG from here
Save the file.Then perform a check so that the MyCustomLogger.log file doesnot exist
sudo rm -f /var/log/MyCustomLogger.log
The rm command removes files/directories if that exists.
Lastly we need to restart the rsyslog service
sudo service rsyslog restart
Now execute the object file MyCustomLogger by using the below command
In-order to check whether the logging happened or not , type the below
An Overview of the syslog Protocol
In this article we have seen how to create custom logging in C Program using the sysLog In Ubuntu.Thanks for reading.The zipped file is attached herewith