Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 10 | This module provides an interface to the Unix ``syslog`` library routines. |
| 11 | Refer to the Unix manual pages for a detailed description of the ``syslog`` |
| 12 | facility. |
| 13 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 14 | This module wraps the system ``syslog`` module. A pure Python |
| 15 | library that can speak to a syslog server is available in |
| 16 | the :mod:`logging.handlers` module as :class:`SysLogHandler`. |
| 17 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | The module defines the following functions: |
| 19 | |
| 20 | |
| 21 | .. function:: syslog([priority,] message) |
| 22 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 23 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | |
| 33 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 34 | .. function:: openlog([ident[, logopt[, facility]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 35 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 36 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | .. function:: closelog() |
| 56 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 57 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | .. function:: setlogmask(maskpri) |
| 67 | |
Sean Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +0000 | [diff] [blame^] | 68 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 74 | |
| 75 | The module defines the following constants: |
| 76 | |
| 77 | Priority 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 | |
| 82 | Facilities: |
| 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 | |
| 87 | Log 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 Reifscheider | f6ce3cb | 2010-04-23 08:31:55 +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: |
| 104 | syslog.syslog(syslog.LOG_ERR, 'Processing started') |
| 105 | |
| 106 | An example of setting some log options, these would include the process ID |
| 107 | in logged messages, and write the messages to the destination facility |
| 108 | used for mail logging:: |
| 109 | |
| 110 | syslog.openlog(logopt=syslog.LOG_PID, facility=syslog.LOG_MAIL) |
| 111 | syslog.syslog('E-mail processing initiated...') |