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
The syslog subroutine writes messages onto the system log maintained by the syslogd command.
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:
|
Facility | Specifies
which of the following values generated the message:
|
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:
|
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
- To log an error message concerning a possible security
breach, such as the following, enter:
syslog (LOG_ALERT, "who:internal error 23");
- 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, "");
- To log an error message from
the system, enter:
syslog (LOG_INFO | LOG_LOCAL2, "foobar error: %m");