blob: 21eee1eb5ef3dc35305cdbe47329b318775a541a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Brandlf8bff482010-04-24 08:56:58 +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 Reifscheiderf6ce3cb2010-04-23 08:31:55 +000016
Georg Brandl8ec7f652007-08-15 14:28:01 +000017The module defines the following functions:
18
19
20.. function:: syslog([priority,] message)
21
Georg Brandlf8bff482010-04-24 08:56:58 +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 Reifscheiderf6ce3cb2010-04-23 08:31:55 +000028
Georg Brandlf8bff482010-04-24 08:56:58 +000029 If :func:`openlog` has not been called prior to the call to :func:`syslog`,
30 ``openlog()`` will be called with no arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +000031
32
Sandro Tosi55ae12b2011-12-24 14:51:26 +010033.. function:: openlog([ident[, logoption[, facility]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000034
Georg Brandlf8bff482010-04-24 08:56:58 +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 Reifscheiderf6ce3cb2010-04-23 08:31:55 +000038
Georg Brandlf8bff482010-04-24 08:56:58 +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 Tosi55ae12b2011-12-24 14:51:26 +010041 stripped. The optional *logoption* keyword argument (default is 0) is a bit
Georg Brandlf8bff482010-04-24 08:56:58 +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 Reifscheiderf6ce3cb2010-04-23 08:31:55 +000045
Georg Brandl8ec7f652007-08-15 14:28:01 +000046
47.. function:: closelog()
48
Georg Brandlf8bff482010-04-24 08:56:58 +000049 Reset the syslog module values and call the system library ``closelog()``.
Sean Reifscheiderf6ce3cb2010-04-23 08:31:55 +000050
Georg Brandlf8bff482010-04-24 08:56:58 +000051 This causes the module to behave as it does when initially imported. For
52 example, :func:`openlog` will be called on the first :func:`syslog` call (if
53 :func:`openlog` hasn't already been called), and *ident* and other
54 :func:`openlog` parameters are reset to defaults.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56
57.. function:: setlogmask(maskpri)
58
Georg Brandlf8bff482010-04-24 08:56:58 +000059 Set the priority mask to *maskpri* and return the previous mask value. Calls
60 to :func:`syslog` with a priority level not set in *maskpri* are ignored.
61 The default is to log all priorities. The function ``LOG_MASK(pri)``
62 calculates the mask for the individual priority *pri*. The function
63 ``LOG_UPTO(pri)`` calculates the mask for all priorities up to and including
64 *pri*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66The module defines the following constants:
67
68Priority levels (high to low):
69 :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`,
70 :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`,
71 :const:`LOG_DEBUG`.
72
73Facilities:
74 :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`,
75 :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`,
76 :const:`LOG_CRON` and :const:`LOG_LOCAL0` to :const:`LOG_LOCAL7`.
77
78Log options:
79 :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, :const:`LOG_NOWAIT`
80 and :const:`LOG_PERROR` if defined in ``<syslog.h>``.
81
Sean Reifscheiderf6ce3cb2010-04-23 08:31:55 +000082
83Examples
84--------
85
86Simple example
87~~~~~~~~~~~~~~
88
89A simple set of examples::
90
91 import syslog
92
93 syslog.syslog('Processing started')
94 if error:
Georg Brandlf8bff482010-04-24 08:56:58 +000095 syslog.syslog(syslog.LOG_ERR, 'Processing started')
Sean Reifscheiderf6ce3cb2010-04-23 08:31:55 +000096
Georg Brandlf8bff482010-04-24 08:56:58 +000097An example of setting some log options, these would include the process ID in
98logged messages, and write the messages to the destination facility used for
99mail logging::
Sean Reifscheiderf6ce3cb2010-04-23 08:31:55 +0000100
Sandro Tosi55ae12b2011-12-24 14:51:26 +0100101 syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
Sean Reifscheiderf6ce3cb2010-04-23 08:31:55 +0000102 syslog.syslog('E-mail processing initiated...')