| 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 |  | 
| Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 20 | .. function:: syslog(message) | 
|  | 21 | syslog(priority, message) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 23 | Send the string *message* to the system logger.  A trailing newline is added | 
|  | 24 | if necessary.  Each message is tagged with a priority composed of a | 
|  | 25 | *facility* and a *level*.  The optional *priority* argument, which defaults | 
|  | 26 | to :const:`LOG_INFO`, determines the message priority.  If the facility is | 
|  | 27 | not encoded in *priority* using logical-or (``LOG_INFO | LOG_USER``), the | 
|  | 28 | value given in the :func:`openlog` call is used. | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 29 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 30 | If :func:`openlog` has not been called prior to the call to :func:`syslog`, | 
|  | 31 | ``openlog()`` will be called with no arguments. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 |  | 
|  | 33 |  | 
| Sandro Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 34 | .. function:: openlog([ident[, logoption[, facility]]]) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 36 | Logging options of subsequent :func:`syslog` calls can be set by calling | 
|  | 37 | :func:`openlog`.  :func:`syslog` will call :func:`openlog` with no arguments | 
|  | 38 | if the log is not currently open. | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 39 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 40 | The optional *ident* keyword argument is a string which is prepended to every | 
|  | 41 | message, and defaults to ``sys.argv[0]`` with leading path components | 
| Sandro Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 42 | stripped.  The optional *logoption* keyword argument (default is 0) is a bit | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 43 | field -- see below for possible values to combine.  The optional *facility* | 
|  | 44 | keyword argument (default is :const:`LOG_USER`) sets the default facility for | 
|  | 45 | messages which do not have a facility explicitly encoded. | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 46 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 47 | .. versionchanged:: 3.2 | 
|  | 48 | In previous versions, keyword arguments were not allowed, and *ident* was | 
|  | 49 | required.  The default for *ident* was dependent on the system libraries, | 
|  | 50 | 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] | 51 |  | 
|  | 52 |  | 
|  | 53 | .. function:: closelog() | 
|  | 54 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 55 | Reset the syslog module values and call the system library ``closelog()``. | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 56 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 57 | This causes the module to behave as it does when initially imported.  For | 
|  | 58 | example, :func:`openlog` will be called on the first :func:`syslog` call (if | 
|  | 59 | :func:`openlog` hasn't already been called), and *ident* and other | 
|  | 60 | :func:`openlog` parameters are reset to defaults. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 |  | 
|  | 62 |  | 
|  | 63 | .. function:: setlogmask(maskpri) | 
|  | 64 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 65 | Set the priority mask to *maskpri* and return the previous mask value.  Calls | 
|  | 66 | to :func:`syslog` with a priority level not set in *maskpri* are ignored. | 
|  | 67 | The default is to log all priorities.  The function ``LOG_MASK(pri)`` | 
|  | 68 | calculates the mask for the individual priority *pri*.  The function | 
|  | 69 | ``LOG_UPTO(pri)`` calculates the mask for all priorities up to and including | 
|  | 70 | *pri*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | The module defines the following constants: | 
|  | 73 |  | 
|  | 74 | Priority levels (high to low): | 
|  | 75 | :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`, | 
|  | 76 | :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`, | 
|  | 77 | :const:`LOG_DEBUG`. | 
|  | 78 |  | 
|  | 79 | Facilities: | 
|  | 80 | :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`, | 
|  | 81 | :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`, | 
| R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 82 | :const:`LOG_CRON`, :const:`LOG_SYSLOG`, :const:`LOG_LOCAL0` to | 
|  | 83 | :const:`LOG_LOCAL7`, and, if defined in ``<syslog.h>``, | 
|  | 84 | :const:`LOG_AUTHPRIV`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 |  | 
|  | 86 | Log options: | 
| R David Murray | eac0939 | 2012-03-29 07:15:45 -0400 | [diff] [blame] | 87 | :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, and, if defined | 
|  | 88 | in ``<syslog.h>``, :const:`LOG_ODELAY`, :const:`LOG_NOWAIT`, and | 
|  | 89 | :const:`LOG_PERROR`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 |  | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 91 |  | 
|  | 92 | Examples | 
|  | 93 | -------- | 
|  | 94 |  | 
|  | 95 | Simple example | 
|  | 96 | ~~~~~~~~~~~~~~ | 
|  | 97 |  | 
|  | 98 | A simple set of examples:: | 
|  | 99 |  | 
|  | 100 | import syslog | 
|  | 101 |  | 
|  | 102 | syslog.syslog('Processing started') | 
|  | 103 | if error: | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 104 | syslog.syslog(syslog.LOG_ERR, 'Processing started') | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 105 |  | 
| Georg Brandl | 8569e58 | 2010-05-19 20:57:08 +0000 | [diff] [blame] | 106 | An example of setting some log options, these would include the process ID in | 
|  | 107 | logged messages, and write the messages to the destination facility used for | 
|  | 108 | mail logging:: | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 109 |  | 
| Sandro Tosi | 0b7e536 | 2011-12-24 14:51:49 +0100 | [diff] [blame] | 110 | syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL) | 
| Sean Reifscheider | 13daf12 | 2010-04-23 09:29:52 +0000 | [diff] [blame] | 111 | syslog.syslog('E-mail processing initiated...') |