syslog, openlog, closelog, or setlogmask Subroutine

Purpose

Controls the system log.

Library

Standard C Library (libc.a)

Syntax

#include <syslog.h>

void openlog ( ID, LogOption, Facility) const char *ID; int LogOption, Facility;

void syslog ( Priority, Value,... ) int Priority; const char *Value;

void closelog ( )

int setlogmask( MaskPriority) int MaskPriority;

void bsdlog (Priority, Value,...) int Priority; const char *Value;

Description

Attention: Do not use the syslog, openlog, closelog, or setlogmask subroutine in a multithreaded environment. See the multithread alternatives in the syslog_r (syslog_r, openlog_r, closelog_r, or setlogmask_r Subroutine), openlog_r, closelog_r, or setlogmask_r subroutine article. The syslog subroutine is not threadsafe; for threadsafe programs the syslog_r subroutine should be used instead.

The syslog subroutine writes messages onto the system log maintained by the syslogd command.

Note: Messages passed to syslog that are longer than 900 bytes may be truncated by syslogd before being logged.

The message is similar to the printf fmt string, with the difference that %m is replaced by the current error message obtained from the errno global variable. A trailing new-line can be added to the message if needed.

Messages are read by the syslogd command and written to the system console or log file, or forwarded to the syslogd command on the appropriate host.

If special processing is required, the openlog subroutine can be used to initialize the log file.

Messages are tagged with codes indicating the type of Priority for each. A Priority is encoded as a Facility, which describes the part of the system generating the message, and as a level, which indicates the severity of the message.

If the syslog subroutine cannot pass the message to the syslogd command, it writes the message on the /dev/console file, provided the LOG_CONS option is set.

The closelog subroutine closes the log file.

The setlogmask subroutine uses the bit mask in the MaskPriority parameter to set the new log priority mask and returns the previous mask.

The LOG_MASK and LOG_UPTO macros in the sys/syslog.h file are used to create the priority mask. Calls to the syslog subroutine with a priority mask that does not allow logging of that particular level of message causes the subroutine to return without logging the message.

Parameters

Item Description
ID Contains a string that is attached to the beginning of every message. The Facility parameter encodes a default facility from the previous list to be assigned to messages that do not have an explicit facility encoded.
LogOption Specifies a bit field that indicates logging options. The values of LogOption are:
LOG_CONS
Sends messages to the console if unable to send them to the syslogd command. This option is useful in daemon processes that have no controlling terminal.
LOG_NDELAY
Opens the connection to the syslogd command immediately, instead of when the first message is logged. This option is useful for programs that need to manage the order in which file descriptors are allocated.
LOG_NOWAIT
Logs messages to the console without waiting for forked children. Use this option for processes that enable notification of child termination through SIGCHLD; otherwise, the syslog subroutine may block, waiting for a child process whose exit status has already been collected.
LOG_ODELAY
Delays opening until the syslog subroutine is called.
LOG_PID
Logs the process ID with each message. This option is useful for identifying daemons.
Facility Specifies which of the following values generated the message:
LOG_AUTH
Indicates the security authorization system: the login command, the su command, and so on.
LOG_DAEMON
Logs system daemons.
LOG_KERN
Logs messages generated by the kernel. Kernel processes should use the bsdlog routine to generate syslog messages. The syntax of bsdlog is identical to syslog. The bsdlog messages can only be created by kernel processes and must be of LOG_KERN priority. The syslog subroutine cannot log LOG_KERN facility messages. Instead it will log LOG_USER facility messages.
LOG_LPR
Logs the line printer spooling system.
LOG_LOCAL0 through LOG_LOCAL7
Reserved for local use.
LOG_MAIL
Logs the mail system.
LOG_NEWS
Logs the news subsystem.
LOG_UUCP
Logs the UUCP subsystem.
LOG_USER
Logs messages generated by user processes. This is the default facility when none is specified.
Priority Specifies the part of the system generating the message, and as a level, indicates the severity of the message. The level of severity is selected from the following list:
LOG_ALERT
Indicates a condition that should be corrected immediately; for example, a corrupted database.
LOG_CRIT
Indicates critical conditions; for example, hard device errors.
LOG_DEBUG
Displays messages containing information useful to debug a program.
LOG_EMERG
Indicates a panic condition reported to all users; system is unusable.
LOG_ERR
Indicated error conditions.
LOG_INFO
Indicates general information messages.
LOG_NOTICE
Indicates a condition requiring special handling, but not an error condition.
LOG_WARNING
Logs warning messages.
MaskPriority Enables logging for the levels indicated by the bits in the mask that are set and disabled where the bits are not set. The default mask allows all priorities to be logged.
Value Specifies the values given in the Value parameters and follows the the same syntax as the printf subroutine Format parameter.

Examples

  1. To log an error message concerning a possible security breach, such as the following, enter:
    syslog (LOG_ALERT, "who:internal error 23");
     
  2. To initialize the log file, set the log priority mask, and log an error message, enter:
    openlog ("ftpd", LOG_PID, LOG_DAEMON);
    setlogmask (LOG_UPTO (LOG_ERR));
    syslog (LOG_INFO, ""); 
     
  3. To log an error message from the system, enter:
    syslog (LOG_INFO | LOG_LOCAL2, "foobar error: %m");