blob: 645c326678f35afb34f37512601a7b8f218b865e [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
Georg Brandl8569e582010-05-19 20:57:08 +000013This module wraps the system ``syslog`` family of routines. A pure Python
14library that can speak to a syslog server is available in the
15:mod:`logging.handlers` module as :class:`SysLogHandler`.
Sean Reifscheider13daf122010-04-23 09:29:52 +000016
Georg Brandl116aa622007-08-15 14:28:22 +000017The module defines the following functions:
18
19
20.. function:: syslog([priority,] message)
21
Georg Brandl8569e582010-05-19 20:57:08 +000022 Send the string *message* to the system logger. A trailing newline is added
23 if necessary. Each message is tagged with a priority composed of a
24 *facility* and a *level*. The optional *priority* argument, which defaults
25 to :const:`LOG_INFO`, determines the message priority. If the facility is
26 not encoded in *priority* using logical-or (``LOG_INFO | LOG_USER``), the
27 value given in the :func:`openlog` call is used.
Sean Reifscheider13daf122010-04-23 09:29:52 +000028
Georg Brandl8569e582010-05-19 20:57:08 +000029 If :func:`openlog` has not been called prior to the call to :func:`syslog`,
30 ``openlog()`` will be called with no arguments.
Georg Brandl116aa622007-08-15 14:28:22 +000031
32
Sandro Tosi0b7e5362011-12-24 14:51:49 +010033.. function:: openlog([ident[, logoption[, facility]]])
Georg Brandl116aa622007-08-15 14:28:22 +000034
Georg Brandl8569e582010-05-19 20:57:08 +000035 Logging options of subsequent :func:`syslog` calls can be set by calling
36 :func:`openlog`. :func:`syslog` will call :func:`openlog` with no arguments
37 if the log is not currently open.
Sean Reifscheider13daf122010-04-23 09:29:52 +000038
Georg Brandl8569e582010-05-19 20:57:08 +000039 The optional *ident* keyword argument is a string which is prepended to every
40 message, and defaults to ``sys.argv[0]`` with leading path components
Sandro Tosi0b7e5362011-12-24 14:51:49 +010041 stripped. The optional *logoption* keyword argument (default is 0) is a bit
Georg Brandl8569e582010-05-19 20:57:08 +000042 field -- see below for possible values to combine. The optional *facility*
43 keyword argument (default is :const:`LOG_USER`) sets the default facility for
44 messages which do not have a facility explicitly encoded.
Sean Reifscheider13daf122010-04-23 09:29:52 +000045
Georg Brandl8569e582010-05-19 20:57:08 +000046 .. versionchanged:: 3.2
47 In previous versions, keyword arguments were not allowed, and *ident* was
48 required. The default for *ident* was dependent on the system libraries,
49 and often was ``python`` instead of the name of the python program file.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
52.. function:: closelog()
53
Georg Brandl8569e582010-05-19 20:57:08 +000054 Reset the syslog module values and call the system library ``closelog()``.
Sean Reifscheider13daf122010-04-23 09:29:52 +000055
Georg Brandl8569e582010-05-19 20:57:08 +000056 This causes the module to behave as it does when initially imported. For
57 example, :func:`openlog` will be called on the first :func:`syslog` call (if
58 :func:`openlog` hasn't already been called), and *ident* and other
59 :func:`openlog` parameters are reset to defaults.
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
62.. function:: setlogmask(maskpri)
63
Georg Brandl8569e582010-05-19 20:57:08 +000064 Set the priority mask to *maskpri* and return the previous mask value. Calls
65 to :func:`syslog` with a priority level not set in *maskpri* are ignored.
66 The default is to log all priorities. The function ``LOG_MASK(pri)``
67 calculates the mask for the individual priority *pri*. The function
68 ``LOG_UPTO(pri)`` calculates the mask for all priorities up to and including
69 *pri*.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71The module defines the following constants:
72
73Priority levels (high to low):
74 :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`,
75 :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`,
76 :const:`LOG_DEBUG`.
77
78Facilities:
79 :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`,
80 :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`,
R David Murray07cf1d82012-03-29 06:47:35 -040081 :const:`LOG_CRON`, :const:`LOG_SYSLOG` and :const:`LOG_LOCAL0` to
82 :const:`LOG_LOCAL7`.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84Log options:
85 :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, :const:`LOG_NOWAIT`
86 and :const:`LOG_PERROR` if defined in ``<syslog.h>``.
87
Sean Reifscheider13daf122010-04-23 09:29:52 +000088
89Examples
90--------
91
92Simple example
93~~~~~~~~~~~~~~
94
95A simple set of examples::
96
97 import syslog
98
99 syslog.syslog('Processing started')
100 if error:
Georg Brandl8569e582010-05-19 20:57:08 +0000101 syslog.syslog(syslog.LOG_ERR, 'Processing started')
Sean Reifscheider13daf122010-04-23 09:29:52 +0000102
Georg Brandl8569e582010-05-19 20:57:08 +0000103An example of setting some log options, these would include the process ID in
104logged messages, and write the messages to the destination facility used for
105mail logging::
Sean Reifscheider13daf122010-04-23 09:29:52 +0000106
Sandro Tosi0b7e5362011-12-24 14:51:49 +0100107 syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
Sean Reifscheider13daf122010-04-23 09:29:52 +0000108 syslog.syslog('E-mail processing initiated...')