Write to Static Custom Log file from sysLog In Ubuntu

Rajnilari2015
Posted by in Others category on for Beginner level | Points: 250 | Views : 3107 red flag
Rating: 5 out of 5  
 1 vote(s)

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.


 Download source code for Write to Static Custom Log file from sysLog In Ubuntu

Introduction

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

Function Name Return Type Signature Purpose
openlog void 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 void 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.
closelog void closelog(void) This function closes the open file descriptors.
setlogmask int setlogmask(int) 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

#include 
#include 
int  main(int argc, char *argv[])
{
    openlog(NULL, LOG_PID, LOG_USER);
    syslog(LOG_INFO, "test by niladri");
    closelog();
    return 0;
}

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

./MyCustomLogger.out

In-order to check whether the logging happened or not , type the below

cat /var/log/MyCustomLogger.log

The result

References

syslog

An Overview of the syslog Protocol

RSYSLOG

Conclusion

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

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)