Writing to syslog

This week I will discuss the syslog() function, which writes a message

to syslog.

Daemons and user-written utilities that run in the background write

messages to syslog frequently. An interactive program may also write to

syslog so you can peruse the log afterwards, analyze the program's

performance and detect bugs and errors in its execution. Logging is

also an important security measure that enables you to track down

suspicious or anomalous activity after it has taken place. The syslog.h

header file declares the syslog() function as follows:

void syslog(int priority, const char *msg, ...);

The first argument is a combination of the severity and facility of the

message (the latter is optional). The severity codes are as follows:

LOG_EMERG - A panic message. Normally broadcast to all users.

LOG_ALERT - A malfunction that should be fixed immediately

LOG_CRIT - Critical condition, e.g., a broken network cable

LOG_ERR - An error message

LOG_WARNING - A warning condition

LOG_NOTICE - A non-error condition that requires special attention

LOG_INFO - Logs an informational message

LOG_DEBUG - A debug-level message

You may also include the facility of the message, if appropriate. For

example, the LOG_KERN facility code indicates a kernel message.

Likewise, LOG_MAIL indicates a message originated by the mail

subsystem, LOG_LOCAL0 indicates a local users, and so on (see syslog.h

for a complete list of facility codes). The severity and facility

values are combined using the bitwise OR operator. The result is the

message's priority.

The remaining arguments of syslog() are a printf-style string that may

contain format flags, and any other arguments required by the format,

except that the special flag %m is replaced by the error message

corresponding to the current errno value. Here is an example of using

syslog() with two arguments:

int main()


syslog(LOG_INFO, "main has started.\n");

return 0;


The above syslog() call adds the following entry to syslog:

November 22 10:12:34 bustopher syslog: main has started.

In the following example, syslog takes four arguments, as required by

its format string:

int f(int stat, const char *op)


if (stat!=SUCCESS)

syslog(LOG_WARN, "%s failed, errno: %d (%m)\n", op, errno);

return 0;


ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon