1 @node Syslog, Mathematics, Low-Level Terminal Interface, Top
2 @c %MENU% System logging and messaging
6 This chapter describes facilities for issuing and logging messages of
7 system administration interest. This chapter has nothing to do with
8 programs issuing messages to their own users or keeping private logs
9 (One would typically do that with the facilities described in
10 @ref{I/O on Streams}).
12 Most systems have a facility called ``Syslog'' that allows programs to
13 submit messages of interest to system administrators and can be
14 configured to pass these messages on in various ways, such as printing
15 on the console, mailing to a particular person, or recording in a log
16 file for future reference.
18 A program uses the facilities in this chapter to submit such messages.
21 * Overview of Syslog:: Overview of a system's Syslog facility
22 * Submitting Syslog Messages:: Functions to submit messages to Syslog
25 @node Overview of Syslog
26 @section Overview of Syslog
28 System administrators have to deal with lots of different kinds of
29 messages from a plethora of subsystems within each system, and usually
30 lots of systems as well. For example, an FTP server might report every
31 connection it gets. The kernel might report hardware failures on a disk
32 drive. A DNS server might report usage statistics at regular intervals.
34 Some of these messages need to be brought to a system administrator's
35 attention immediately. And it may not be just any system administrator
36 -- there may be a particular system administrator who deals with a
37 particular kind of message. Other messages just need to be recorded for
38 future reference if there is a problem. Still others may need to have
39 information extracted from them by an automated process that generates
42 To deal with these messages, most Unix systems have a facility called
43 "Syslog." It is generally based on a daemon called ``Syslogd''
44 Syslogd listens for messages on a Unix domain socket named
45 @file{/dev/log}. Based on classification information in the messages
46 and its configuration file (usually @file{/etc/syslog.conf}), Syslogd
47 routes them in various ways. Some of the popular routings are:
51 Write to the system console
53 Mail to a specific user
57 Pass to another daemon
62 Syslogd can also handle messages from other systems. It listens on the
63 @code{syslog} UDP port as well as the local socket for messages.
65 Syslog can handle messages from the kernel itself. But the kernel
66 doesn't write to @file{/dev/log}; rather, another daemon (sometimes
67 called ``Klogd'') extracts messages from the kernel and passes them on to
68 Syslog as any other process would (and it properly identifies them as
69 messages from the kernel).
71 Syslog can even handle messages that the kernel issued before Syslogd or
72 Klogd was running. A Linux kernel, for example, stores startup messages
73 in a kernel message ring and they are normally still there when Klogd
74 later starts up. Assuming Syslogd is running by the time Klogd starts,
75 Klogd then passes everything in the message ring to it.
77 In order to classify messages for disposition, Syslog requires any process
78 that submits a message to it to provide two pieces of classification
83 This identifies who submitted the message. There are a small number of
84 facilities defined. The kernel, the mail subsystem, and an FTP server
85 are examples of recognized facilities. For the complete list,
86 @xref{syslog; vsyslog}. Keep in mind that these are
87 essentially arbitrary classifications. "Mail subsystem" doesn't have any
88 more meaning than the system administrator gives to it.
91 This tells how important the content of the message is. Examples of
92 defined priority values are: debug, informational, warning, critical.
93 For the complete list, @xref{syslog; vsyslog}. Except for
94 the fact that the priorities have a defined order, the meaning of each
95 of these priorities is entirely determined by the system administrator.
99 A ``facility/priority'' is a number that indicates both the facility
102 @strong{Warning:} This terminology is not universal. Some people use
103 ``level'' to refer to the priority and ``priority'' to refer to the
104 combination of facility and priority. A Linux kernel has a concept of a
105 message ``level,'' which corresponds both to a Syslog priority and to a
106 Syslog facility/priority (It can be both because the facility code for
107 the kernel is zero, and that makes priority and facility/priority the
110 The GNU C library provides functions to submit messages to Syslog. They
111 do it by writing to the @file{/dev/log} socket. @xref{Submitting Syslog
114 The GNU C library functions only work to submit messages to the Syslog
115 facility on the same system. To submit a message to the Syslog facility
116 on another system, use the socket I/O functions to write a UDP datagram
117 to the @code{syslog} UDP port on that system. @xref{Sockets}.
120 @node Submitting Syslog Messages
121 @section Submitting Syslog Messages
123 The GNU C library provides functions to submit messages to the Syslog
127 * openlog:: Open connection to Syslog
128 * syslog; vsyslog:: Submit message to Syslog
129 * closelog:: Close connection to Syslog
130 * setlogmask:: Cause certain messages to be ignored
131 * Syslog Example:: Example of all of the above
134 These functions only work to submit messages to the Syslog facility on
135 the same system. To submit a message to the Syslog facility on another
136 system, use the socket I/O functions to write a UDP datagram to the
137 @code{syslog} UDP port on that system. @xref{Sockets}.
144 The symbols referred to in this section are declared in the file
149 @deftypefun void openlog (char *@var{ident}, int @var{option},
152 @code{openlog} opens or reopens a connection to Syslog in preparation
153 for submitting messages.
155 @var{ident} is an arbitrary identification string which future
156 @code{syslog} invocations will prefix to each message. This is intended
157 to identify the source of the message, and people conventionally set it
158 to the name of the program that will submit the messages.
160 @code{openlog} may or may not open the @file{/dev/log} socket, depending
161 on @var{option}. If it does, it tries to open it and connect it as a
162 stream socket. If that doesn't work, it tries to open it and connect it
163 as a datagram socket. The socket has the ``Close on Exec'' attribute,
164 so the kernel will close it if the process performs an exec.
166 You don't have to use @code{openlog}. If you call @code{syslog} without
167 having called @code{openlog}, @code{syslog} just opens the connection
168 implicitly and uses defaults for the information in @var{ident} and
171 @var{options} is a bit string, with the bits as defined by the following
176 If on, @code{openlog} sets up the connection so that any @code{syslog}
177 on this connection writes its message to the calling process' Standard
178 Error stream in addition to submitting it to Syslog. If off, @code{syslog}
179 does not write the message to Standard Error.
182 If on, @code{openlog} sets up the connection so that a @code{syslog} on
183 this connection that fails to submit a message to Syslog writes the
184 message instead to system console. If off, @code{syslog} does not write
185 to the system console (but of course Syslog may write messages it
186 receives to the console).
189 When on, @code{openlog} sets up the connection so that a @code{syslog}
190 on this connection inserts the calling process' Process ID (PID) into
191 the message. When off, @code{openlog} does not insert the PID.
194 When on, @code{openlog} opens and connects the @file{/dev/log} socket.
195 When off, a future @code{syslog} call must open and connect the socket.
197 @strong{Portability note:} In early systems, the sense of this bit was
198 exactly the opposite.
201 This bit does nothing. It exists for backward compatibility.
205 If any other bit in @var{options} is on, the result is undefined.
207 @var{facility} is the default facility code for this connection. A
208 @code{syslog} on this connection that specifies default facility causes
209 this facility to be associated with the message. See @code{syslog} for
210 possible values. A value of zero means the default default, which is
213 If a Syslog connection is already open when you call @code{openlog},
214 @code{openlog} ``reopens'' the connection. Reopening is like opening
215 except that if you specify zero for the default facility code, the
216 default facility code simply remains unchanged and if you specify
217 LOG_NDELAY and the socket is already open and connected, @code{openlog}
218 just leaves it that way.
220 @c There is a bug in closelog() (glibc 2.1.3) wherein it does not reset the
221 @c default log facility to LOG_USER, which means the default default log
222 @c facility could be whatever the default log facility was for a previous
223 @c Syslog connection. I have documented what the function should be rather
224 @c than what it is because I think if anyone ever gets concerned, the code
230 @node syslog; vsyslog
231 @subsection syslog, vsyslog
233 The symbols referred to in this section are declared in the file
236 @c syslog() is implemented as a call to vsyslog().
239 @deftypefun void syslog (int @var{facility_priority}, char *@var{format}, ...)
241 @code{syslog} submits a message to the Syslog facility. It does this by
242 writing to the Unix domain socket @code{/dev/log}.
244 @code{syslog} submits the message with the facility and priority indicated
245 by @var{facility_priority}. The macro @code{LOG_MAKEPRI} generates a
246 facility/priority from a facility and a priority, as in the following
250 LOG_MAKEPRI(LOG_USER, LOG_WARNING)
253 The possible values for the facility code are (macros):
255 @c Internally, there is also LOG_KERN, but LOG_KERN == 0, which means
256 @c if you try to use it here, just selects default.
260 A miscellaneous user process
264 A miscellaneous system daemon
266 Security (authorization)
272 Network news (e.g. Usenet)
278 Private security (authorization)
299 Results are undefined if the facility code is anything else.
301 @strong{note:} @code{syslog} recognizes one other facility code: that of
302 the kernel. But you can't specify that facility code with these
303 functions. If you try, it looks the same to @code{syslog} as if you are
304 requesting the default facility. But you wouldn't want to anyway,
305 because any program that uses the GNU C library is not the kernel.
307 You can use just a priority code as @var{facility_priority}. In that
308 case, @code{syslog} assumes the default facility established when the
309 Syslog connection was opened. @xref{Syslog Example}.
311 The possible values for the priority code are (macros):
315 The message says the system is unusable.
317 Action on the message must be taken immediately.
319 The message states a critical condition.
321 The message describes an error.
323 The message is a warning.
325 The message describes a normal but important event.
327 The message is purely informational.
329 The message is only for debugging purposes.
332 Results are undefined if the priority code is anything else.
334 If the process does not presently have a Syslog connection open (i.e.
335 it did not call @code{openlog}), @code{syslog} implicitly opens the
336 connection the same as @code{openlog} would, with the following defaults
337 for information that would otherwise be included in an @code{openlog}
338 call: The default identification string is the program name. The
339 default default facility is @code{LOG_USER}. The default for all the
340 connection options in @var{options} is as if those bits were off.
341 @code{syslog} leaves the Syslog connection open.
343 If the @file{dev/log} socket is not open and connected, @code{syslog}
344 opens and connects it, the same as @code{openlog} with the
345 @code{LOG_NDELAY} option would.
347 @code{syslog} leaves @file{/dev/log} open and connected unless its attempt
348 to send the message failed, in which case @code{syslog} closes it (with the
349 hope that a future implicit open will restore the Syslog connection to a
357 syslog (LOG_MAKEPRI(LOG_LOCAL1, LOG_ERROR),
358 "Unable to make network connection to %s. Error=%m", host);
367 @deftypefun void vsyslog (int @var{facility_priority}, char *@var{format}, va_list arglist)
369 This is functionally identical to @code{syslog}, with the BSD style variable
378 The symbols referred to in this section are declared in the file
383 @deftypefun void closelog (void)
385 @code{closelog} closes the current Syslog connection, if there is one.
386 This include closing the @file{dev/log} socket, if it is open.
388 There is very little reason to use this function. It does not flush any
389 buffers; you can reopen a Syslog connection without closing it first;
390 The connection gets closed automatically on exec or exit.
391 @code{closelog} has primarily aesthetic value.
397 @subsection setlogmask
399 The symbols referred to in this section are declared in the file
404 @deftypefun int setlogmask (int @var{mask})
406 @code{setlogmask} sets a mask (the ``logmask'') that determines which
407 future @code{syslog} calls shall be ignored. If a program has not
408 called @code{setlogmask}, @code{syslog} doesn't ignore any calls. You
409 can use @code{setlogmask} to specify that messages of particular
410 priorities shall be ignored in the future.
412 A @code{setlogmask} call overrides any previous @code{setlogmask} call.
414 Note that the logmask exists entirely independently of opening and
415 closing of Syslog connections.
417 Setting the logmask has a similar effect to, but is not the same as,
418 configuring Syslog. The Syslog configuration may cause Syslog to
419 discard certain messages it receives, but the logmask causes certain
420 messages never to get submitted to Syslog in the first place.
422 @var{mask} is a bit string with one bit corresponding to each of the
423 possible message priorities. If the bit is on, @code{syslog} handles
424 messages of that priority normally. If it is off, @code{syslog}
425 discards messages of that priority. Use the message priority macros
426 described in @ref{syslog; vsyslog} and the @code{LOG_MASK} to construct
427 an appropriate @var{mask} value, as in this example:
430 LOG_MASK(LOG_EMERG) | LOG_MASK(LOG_ERROR)
436 ~(LOG_MASK(LOG_INFO))
439 There is also a @code{LOG_UPTO} macro, which generates a mask with the bits
440 on for a certain priority and all priorities above it:
446 The unfortunate naming of the macro is due to the fact that internally,
447 higher numbers are used for lower message priorities.
453 @subsection Syslog Example
455 Here is an example of @code{openlog}, @code{syslog}, and @code{closelog}:
457 This example sets the logmask so that debug and informational messages
458 get discarded without ever reaching Syslog. So the second @code{syslog}
459 in the example does nothing.
464 setlogmask (LOG_UPTO (LOG_NOTICE));
466 openlog ("exampleprog", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
468 syslog (LOG_NOTICE, "Program started by User %d", getuid ());
469 syslog (LOG_INFO, "A tree falls in a forest");