blob: c25ed41ff20e62060401c9ee1b2edd62ebc52d72 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`syslog` --- Unix syslog library routines
2==============================================
3
4.. module:: syslog
5 :platform: Unix
6 :synopsis: An interface to the Unix syslog library routines.
7
8
9This module provides an interface to the Unix ``syslog`` library routines.
10Refer to the Unix manual pages for a detailed description of the ``syslog``
11facility.
12
Sean Reifscheider13daf122010-04-23 09:29:52 +000013This module wraps the system ``syslog`` module. A pure Python
14library that can speak to a syslog server is available in
15the :mod:`logging.handlers` module as :class:`SysLogHandler`.
16
Georg Brandl116aa622007-08-15 14:28:22 +000017The module defines the following functions:
18
19
20.. function:: syslog([priority,] message)
21
Sean Reifscheider13daf122010-04-23 09:29:52 +000022 Send the string *message* to the system logger. A trailing newline is
23 added if necessary. Each message is tagged with a priority composed
24 of a *facility* and a *level*. The optional *priority* argument, which
25 defaults to :const:`LOG_INFO`, determines the message priority. If the
26 facility is not encoded in *priority* using logical-or (``LOG_INFO |
27 LOG_USER``), the value given in the :func:`openlog` call is used.
28
29 If :func:`openlog` has not been called prior to the call to
30 :func:'syslog', ``openlog()`` will be called with no arguments.
Georg Brandl116aa622007-08-15 14:28:22 +000031
32
Sean Reifscheider13daf122010-04-23 09:29:52 +000033.. function:: openlog([ident[, logopt[, facility]]])
Georg Brandl116aa622007-08-15 14:28:22 +000034
Sean Reifscheider13daf122010-04-23 09:29:52 +000035 Logging options of subsequent :func:`syslog` calls can be set by
36 calling :func:`openlog`. :func:`syslog` will call :func:`openlog`
37 with no arguments if the log is not currently open.
38
39 The optional *ident* keyword argument is a string which is prepended
40 to every message, and defaults to ''sys.argv[0]'' with leading
41 path components stripped. The optional *logopt* keyword argument
42 (default=0) is a bit field - see below for possible values to combine.
43 The optional *facility* keyword argument (default=:const:`LOG_USER`)
44 sets the default facility for messages which do not have a facility
45 explicitly encoded.
46
47 .. versionchanged::3.2
48 In previous versions, keyword arguments were not allowed, and *ident*
49 was required. The default for *ident* was dependent on the system
50 libraries, and often was ''python'' instead of the name of the
51 python program file.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53
54.. function:: closelog()
55
Sean Reifscheider13daf122010-04-23 09:29:52 +000056 Reset the syslog module values and call the system library
57 ''closelog()''.
58
59 This causes the module to behave as it does when initially imported.
60 For example, :func:'openlog' will be called on the first :func:'syslog'
61 call (if :func:'openlog' hasn't already been called), and *ident*
62 and other :func:'openlog' parameters are reset to defaults.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
65.. function:: setlogmask(maskpri)
66
Sean Reifscheider13daf122010-04-23 09:29:52 +000067 Set the priority mask to *maskpri* and return the previous mask value.
68 Calls to :func:`syslog` with a priority level not set in *maskpri*
69 are ignored. The default is to log all priorities. The function
70 ``LOG_MASK(pri)`` calculates the mask for the individual priority
71 *pri*. The function ``LOG_UPTO(pri)`` calculates the mask for all
72 priorities up to and including *pri*.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74The module defines the following constants:
75
76Priority levels (high to low):
77 :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`,
78 :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`,
79 :const:`LOG_DEBUG`.
80
81Facilities:
82 :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`,
83 :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`,
84 :const:`LOG_CRON` and :const:`LOG_LOCAL0` to :const:`LOG_LOCAL7`.
85
86Log options:
87 :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, :const:`LOG_NOWAIT`
88 and :const:`LOG_PERROR` if defined in ``<syslog.h>``.
89
Sean Reifscheider13daf122010-04-23 09:29:52 +000090
91Examples
92--------
93
94Simple example
95~~~~~~~~~~~~~~
96
97A simple set of examples::
98
99 import syslog
100
101 syslog.syslog('Processing started')
102 if error:
103 syslog.syslog(syslog.LOG_ERR, 'Processing started')
104
105An example of setting some log options, these would include the process ID
106in logged messages, and write the messages to the destination facility
107used for mail logging::
108
109 syslog.openlog(logopt=syslog.LOG_PID, facility=syslog.LOG_MAIL)
110 syslog.syslog('E-mail processing initiated...')