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