SYSLOG And Its Usage

Quick and dirty way!

The best place to learn about syslog is here. But if you know all the theory and now wants to get started quickly, this can help.

Syslog Contains

Syslog contains following three parameters:

  1. Logging Facility: This is the type of the program that is generating the log, it has to be one of the well defines facilities in the RFC 3164.
  2. Log Severity: What is the severity of the log that is going to generate. This can be one of the following: Emergency(level 0), Alert(level 1), Critical(level 2), Error(level-3), Warning(level-4), Notice(level-5), Information(level-6), Debug(level-7). Higher number severity will generate all the severities about below it. Example: Debug will generate all logs in the system, warning will generate all the logs below level-4 i.e. Error, Critical, Alert, Emergency.
  3. Log Message: This the log message that user wants to print.

Code Snippet

#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
setlogmask (LOG_UPTO (LOG_NOTICE));
int main(void) {
openlog("example", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);

syslog(LOG_INFO, "My first log message from user %", getuid());
syslog(LOG_WARNING, "Exiting from the program");

closelog();
return 0;
}

Options

/*
* Option flags for openlog.
*
* LOG_ODELAY no longer does anything.
* LOG_NDELAY is the inverse of what it used to be.
*/
#define LOG_PID 0x01 /* log the pid with each message */
#define LOG_CONS 0x02 /* log on the console if errors in sending */
#define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */
#define LOG_NDELAY 0x08 /* don't delay open */
#define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */
#define LOG_PERROR 0x20 /* log to stderr as well */
/* facility codes */
#define LOG_KERN (0<<3) /* kernel messages */
#define LOG_USER (1<<3) /* random user-level messages */
#define LOG_MAIL (2<<3) /* mail system */
#define LOG_DAEMON (3<<3) /* system daemons */
#define LOG_AUTH (4<<3) /* security/authorization messages */
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
#define LOG_LPR (6<<3) /* line printer subsystem */
#define LOG_NEWS (7<<3) /* network news subsystem */
#define LOG_UUCP (8<<3) /* UUCP subsystem */
#define LOG_CRON (9<<3) /* clock daemon */
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */

/* other codes through 15 reserved for system use */
#define LOG_LOCAL0 (16<<3) /* reserved for local use */
#define LOG_LOCAL1 (17<<3) /* reserved for local use */
#define LOG_LOCAL2 (18<<3) /* reserved for local use */
#define LOG_LOCAL3 (19<<3) /* reserved for local use */
#define LOG_LOCAL4 (20<<3) /* reserved for local use */
#define LOG_LOCAL5 (21<<3) /* reserved for local use */
#define LOG_LOCAL6 (22<<3) /* reserved for local use */
#define LOG_LOCAL7 (23<<3) /* reserved for local use */
/*
* arguments to setlogmask.
*/
#define LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
#define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1) /* all priorities through pri */

Next Round

In the next round will cover the various local and remote configuration parameters, stay connected!

Like what you read? Give Milind Deore a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.