syslog --- Unix syslog 函式庫例程


This module provides an interface to the Unixsyslog library routines.Refer to the Unix manual pages for a detailed description of thesyslogfacility.

適用: Unix, not WASI, not iOS.

This module wraps the systemsyslog family of routines. A pure Pythonlibrary that can speak to a syslog server is available in thelogging.handlers module asSysLogHandler.

該模組定義了以下函式:

syslog.syslog(message)
syslog.syslog(priority,message)

Send the stringmessage to the system logger. A trailing newline is addedif necessary. Each message is tagged with a priority composed of afacility and alevel. The optionalpriority argument, which defaultstoLOG_INFO, determines the message priority. If the facility isnot encoded inpriority using logical-or (LOG_INFO|LOG_USER), thevalue given in theopenlog() call is used.

Ifopenlog() has not been called prior to the call tosyslog(),openlog() will be called with no arguments.

引發一個附帶引數prioritymessage稽核事件syslog.syslog

在 3.2 版的變更:In previous versions,openlog() would not be called automatically ifit wasn't called prior to the call tosyslog(), deferring to the syslogimplementation to callopenlog().

在 3.12 版的變更:This function is restricted in subinterpreters.(Only code that runs in multiple interpreters is affected andthe restriction is not relevant for most users.)openlog() must be called in the main interpreter beforesyslog() may be usedin a subinterpreter. Otherwise it will raiseRuntimeError.

syslog.openlog([ident[,logoption[,facility]]])

Logging options of subsequentsyslog() calls can be set by callingopenlog().syslog() will callopenlog() with no argumentsif the log is not currently open.

The optionalident keyword argument is a string which is prepended to everymessage, and defaults tosys.argv[0] with leading path componentsstripped. The optionallogoption keyword argument (default is 0) is a bitfield -- see below for possible values to combine. The optionalfacilitykeyword argument (default isLOG_USER) sets the default facility formessages which do not have a facility explicitly encoded.

引發一個附帶引數identlogoptionfacility稽核事件syslog.openlog

在 3.2 版的變更:In previous versions, keyword arguments were not allowed, andident wasrequired.

在 3.12 版的變更:This function is restricted in subinterpreters.(Only code that runs in multiple interpreters is affected andthe restriction is not relevant for most users.)This may only be called in the main interpreter.It will raiseRuntimeError if called in a subinterpreter.

syslog.closelog()

Reset the syslog module values and call the system librarycloselog().

This causes the module to behave as it does when initially imported. Forexample,openlog() will be called on the firstsyslog() call (ifopenlog() hasn't already been called), andident and otheropenlog() parameters are reset to defaults.

引發一個不附帶引數的稽核事件syslog.closelog

在 3.12 版的變更:This function is restricted in subinterpreters.(Only code that runs in multiple interpreters is affected andthe restriction is not relevant for most users.)This may only be called in the main interpreter.It will raiseRuntimeError if called in a subinterpreter.

syslog.setlogmask(maskpri)

Set the priority mask tomaskpri and return the previous mask value. Callstosyslog() with a priority level not set inmaskpri are ignored.The default is to log all priorities. The functionLOG_MASK(pri)calculates the mask for the individual prioritypri. The functionLOG_UPTO(pri) calculates the mask for all priorities up to and includingpri.

引發一個附帶引數maskpri稽核事件syslog.setlogmask

The module defines the following constants:

syslog.LOG_EMERG
syslog.LOG_ALERT
syslog.LOG_CRIT
syslog.LOG_ERR
syslog.LOG_WARNING
syslog.LOG_NOTICE
syslog.LOG_INFO
syslog.LOG_DEBUG

Priority levels (high to low).

syslog.LOG_AUTH
syslog.LOG_AUTHPRIV
syslog.LOG_CRON
syslog.LOG_DAEMON
syslog.LOG_FTP
syslog.LOG_INSTALL
syslog.LOG_KERN
syslog.LOG_LAUNCHD
syslog.LOG_LPR
syslog.LOG_MAIL
syslog.LOG_NETINFO
syslog.LOG_NEWS
syslog.LOG_RAS
syslog.LOG_REMOTEAUTH
syslog.LOG_SYSLOG
syslog.LOG_USER
syslog.LOG_UUCP
syslog.LOG_LOCAL0
syslog.LOG_LOCAL1
syslog.LOG_LOCAL2
syslog.LOG_LOCAL3
syslog.LOG_LOCAL4
syslog.LOG_LOCAL5
syslog.LOG_LOCAL6
syslog.LOG_LOCAL7

Facilities, depending on availability in<syslog.h> forLOG_AUTHPRIV,LOG_FTP,LOG_NETINFO,LOG_REMOTEAUTH,LOG_INSTALL andLOG_RAS.

syslog.LOG_PID
syslog.LOG_CONS
syslog.LOG_NDELAY
syslog.LOG_ODELAY
syslog.LOG_NOWAIT
syslog.LOG_PERROR

Log options, depending on availability in<syslog.h> forLOG_ODELAY,LOG_NOWAIT andLOG_PERROR.

範例

簡單範例

一組簡單範例:

importsyslogsyslog.syslog('Processing started')iferror:syslog.syslog(syslog.LOG_ERR,'Processing started')

An example of setting some log options, these would include the process ID inlogged messages, and write the messages to the destination facility used formail logging:

syslog.openlog(logoption=syslog.LOG_PID,facility=syslog.LOG_MAIL)syslog.syslog('E-mail processing initiated...')