Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
| 9 | This module provides an interface to the Unix ``syslog`` library routines. |
| 10 | Refer to the Unix manual pages for a detailed description of the ``syslog`` |
| 11 | facility. |
| 12 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 13 | This module wraps the system ``syslog`` family of routines. A pure Python |
| 14 | library that can speak to a syslog server is available in the |
| 15 | :mod:`logging.handlers` module as :class:`SysLogHandler`. |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | The module defines the following functions: |
| 18 | |
| 19 | |
| 20 | .. function:: syslog([priority,] message) |
| 21 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 22 | 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 Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 29 | If :func:`openlog` has not been called prior to the call to :func:`syslog`, |
| 30 | ``openlog()`` will be called with no arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | |
Sandro Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 33 | .. function:: openlog([ident[, logoption[, facility]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 35 | 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 Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 38 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 39 | 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 Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 41 | stripped. The optional *logoption* keyword argument (default is 0) is a bit |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 42 | 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 Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 45 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 46 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | .. function:: closelog() |
| 53 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 54 | Reset the syslog module values and call the system library ``closelog()``. |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 56 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | .. function:: setlogmask(maskpri) |
| 63 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 64 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | The module defines the following constants: |
| 72 | |
| 73 | Priority 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 | |
| 78 | Facilities: |
| 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 Murray | 07cf1d8 | 2012-03-29 06:47:35 -0400 | [diff] [blame] | 81 | :const:`LOG_CRON`, :const:`LOG_SYSLOG` and :const:`LOG_LOCAL0` to |
| 82 | :const:`LOG_LOCAL7`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | Log 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 Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 88 | |
| 89 | Examples |
| 90 | -------- |
| 91 | |
| 92 | Simple example |
| 93 | ~~~~~~~~~~~~~~ |
| 94 | |
| 95 | A simple set of examples:: |
| 96 | |
| 97 | import syslog |
| 98 | |
| 99 | syslog.syslog('Processing started') |
| 100 | if error: |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 101 | syslog.syslog(syslog.LOG_ERR, 'Processing started') |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 103 | An example of setting some log options, these would include the process ID in |
| 104 | logged messages, and write the messages to the destination facility used for |
| 105 | mail logging:: |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 106 | |
Sandro Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 107 | syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL) |
Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 108 | syslog.syslog('E-mail processing initiated...') |