blob: d264a3340c98b01fcc6f568025760ad70140b91d [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008--------------
Georg Brandl116aa622007-08-15 14:28:22 +00009
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
Georg Brandl8569e582010-05-19 20:57:08 +000014This module wraps the system ``syslog`` family of routines. A pure Python
15library that can speak to a syslog server is available in the
16:mod:`logging.handlers` module as :class:`SysLogHandler`.
Sean Reifscheider13daf122010-04-23 09:29:52 +000017
Georg Brandl116aa622007-08-15 14:28:22 +000018The module defines the following functions:
19
20
Ezio Melottie0add762012-09-14 06:32:35 +030021.. function:: syslog(message)
22 syslog(priority, message)
Georg Brandl116aa622007-08-15 14:28:22 +000023
Georg Brandl8569e582010-05-19 20:57:08 +000024 Send the string *message* to the system logger. A trailing newline is added
25 if necessary. Each message is tagged with a priority composed of a
26 *facility* and a *level*. The optional *priority* argument, which defaults
27 to :const:`LOG_INFO`, determines the message priority. If the facility is
28 not encoded in *priority* using logical-or (``LOG_INFO | LOG_USER``), the
29 value given in the :func:`openlog` call is used.
Sean Reifscheider13daf122010-04-23 09:29:52 +000030
Georg Brandl8569e582010-05-19 20:57:08 +000031 If :func:`openlog` has not been called prior to the call to :func:`syslog`,
32 ``openlog()`` will be called with no arguments.
Georg Brandl116aa622007-08-15 14:28:22 +000033
Saiyang Gou7514f4f2020-02-12 23:47:42 -080034 .. audit-event:: syslog.syslog priority,message syslog.syslog
35
Georg Brandl116aa622007-08-15 14:28:22 +000036
Sandro Tosi0b7e5362011-12-24 14:51:49 +010037.. function:: openlog([ident[, logoption[, facility]]])
Georg Brandl116aa622007-08-15 14:28:22 +000038
Georg Brandl8569e582010-05-19 20:57:08 +000039 Logging options of subsequent :func:`syslog` calls can be set by calling
40 :func:`openlog`. :func:`syslog` will call :func:`openlog` with no arguments
41 if the log is not currently open.
Sean Reifscheider13daf122010-04-23 09:29:52 +000042
Georg Brandl8569e582010-05-19 20:57:08 +000043 The optional *ident* keyword argument is a string which is prepended to every
44 message, and defaults to ``sys.argv[0]`` with leading path components
Sandro Tosi0b7e5362011-12-24 14:51:49 +010045 stripped. The optional *logoption* keyword argument (default is 0) is a bit
Georg Brandl8569e582010-05-19 20:57:08 +000046 field -- see below for possible values to combine. The optional *facility*
47 keyword argument (default is :const:`LOG_USER`) sets the default facility for
48 messages which do not have a facility explicitly encoded.
Sean Reifscheider13daf122010-04-23 09:29:52 +000049
Saiyang Gou7514f4f2020-02-12 23:47:42 -080050 .. audit-event:: syslog.openlog ident,logoption,facility syslog.openlog
51
Georg Brandl8569e582010-05-19 20:57:08 +000052 .. versionchanged:: 3.2
53 In previous versions, keyword arguments were not allowed, and *ident* was
54 required. The default for *ident* was dependent on the system libraries,
Andrés Delfino271818f2018-09-14 14:13:09 -030055 and often was ``python`` instead of the name of the Python program file.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. function:: closelog()
59
Georg Brandl8569e582010-05-19 20:57:08 +000060 Reset the syslog module values and call the system library ``closelog()``.
Sean Reifscheider13daf122010-04-23 09:29:52 +000061
Georg Brandl8569e582010-05-19 20:57:08 +000062 This causes the module to behave as it does when initially imported. For
63 example, :func:`openlog` will be called on the first :func:`syslog` call (if
64 :func:`openlog` hasn't already been called), and *ident* and other
65 :func:`openlog` parameters are reset to defaults.
Georg Brandl116aa622007-08-15 14:28:22 +000066
Saiyang Gou7514f4f2020-02-12 23:47:42 -080067 .. audit-event:: syslog.closelog "" syslog.closelog
68
Georg Brandl116aa622007-08-15 14:28:22 +000069
70.. function:: setlogmask(maskpri)
71
Georg Brandl8569e582010-05-19 20:57:08 +000072 Set the priority mask to *maskpri* and return the previous mask value. Calls
73 to :func:`syslog` with a priority level not set in *maskpri* are ignored.
74 The default is to log all priorities. The function ``LOG_MASK(pri)``
75 calculates the mask for the individual priority *pri*. The function
76 ``LOG_UPTO(pri)`` calculates the mask for all priorities up to and including
77 *pri*.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Saiyang Gou7514f4f2020-02-12 23:47:42 -080079 .. audit-event:: syslog.setlogmask maskpri syslog.setlogmask
80
Georg Brandl116aa622007-08-15 14:28:22 +000081The module defines the following constants:
82
83Priority levels (high to low):
84 :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`,
85 :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`,
86 :const:`LOG_DEBUG`.
87
88Facilities:
89 :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`,
90 :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`,
R David Murrayeac09392012-03-29 07:15:45 -040091 :const:`LOG_CRON`, :const:`LOG_SYSLOG`, :const:`LOG_LOCAL0` to
92 :const:`LOG_LOCAL7`, and, if defined in ``<syslog.h>``,
93 :const:`LOG_AUTHPRIV`.
Georg Brandl116aa622007-08-15 14:28:22 +000094
95Log options:
R David Murrayeac09392012-03-29 07:15:45 -040096 :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, and, if defined
97 in ``<syslog.h>``, :const:`LOG_ODELAY`, :const:`LOG_NOWAIT`, and
98 :const:`LOG_PERROR`.
Georg Brandl116aa622007-08-15 14:28:22 +000099
Sean Reifscheider13daf122010-04-23 09:29:52 +0000100
101Examples
102--------
103
104Simple example
105~~~~~~~~~~~~~~
106
107A simple set of examples::
108
109 import syslog
110
111 syslog.syslog('Processing started')
112 if error:
Georg Brandl8569e582010-05-19 20:57:08 +0000113 syslog.syslog(syslog.LOG_ERR, 'Processing started')
Sean Reifscheider13daf122010-04-23 09:29:52 +0000114
Georg Brandl8569e582010-05-19 20:57:08 +0000115An example of setting some log options, these would include the process ID in
116logged messages, and write the messages to the destination facility used for
117mail logging::
Sean Reifscheider13daf122010-04-23 09:29:52 +0000118
Sandro Tosi0b7e5362011-12-24 14:51:49 +0100119 syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
Sean Reifscheider13daf122010-04-23 09:29:52 +0000120 syslog.syslog('E-mail processing initiated...')