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