logging.handlers
— 日志处理程序
¶
重要
此页面仅包含参考信息。有关教程,请参阅
包中提供了下列有用处理程序。注意,其中 3 个处理程序 (
StreamHandler
,
FileHandler
and
NullHandler
) 实际定义在
logging
模块本身,但已文档化在此除其它处理程序外。
The
StreamHandler
类,位于核心
logging
包,将日志记录输出发送给流,譬如
sys.stdout
,
sys.stderr
或任何像文件对象 (或更准确地说,任何对象支持
write()
and
flush()
方法)。
logging.
StreamHandler
(
stream=None
)
¶
返回新的实例化
StreamHandler
类。若
stream
有指定,实例将使用它为日志记录输出;否则,
sys.stderr
会被使用。
emit
(
record
)
¶
If a formatter is specified, it is used to format the record. The record is then written to the stream with a newline terminator. If exception information is present, it is formatted using
traceback.print_exception()
and appended to the stream.
The
FileHandler
类,位于核心
logging
package, sends logging output to a disk file. It inherits the output functionality from
StreamHandler
.
logging.
FileHandler
(
filename
,
mode='a'
,
encoding=None
,
delay=False
)
¶
返回新的实例化
FileHandler
类。打开指定文件并用作日志记录流。若
mode
未指定,
'a'
被使用。若
encoding
不是
None
,用于采用该编码打开文件。若
delay
为 True,则延迟打开文件直到首次调用
emit()
. By default, the file grows indefinitely.
2.6 版改变: delay 被添加。
close
(
)
¶
关闭文件。
emit
(
record
)
¶
将记录输出到文件。
2.7 版新增。
The
NullHandler
类,位于核心
logging
package, does not do any formatting or output. It is essentially a ‘no-op’ handler for use by library developers.
logging.
NullHandler
¶
返回新的实例化
NullHandler
类。
emit
(
record
)
¶
This method does nothing.
handle
(
record
)
¶
This method does nothing.
createLock
(
)
¶
此方法返回
None
for the lock, since there is no underlying I/O to which access needs to be serialized.
见
Configuring Logging for a Library
for more information on how to use
NullHandler
.
2.6 版新增。
The
WatchedFileHandler
类,位于
logging.handlers
module, is a
FileHandler
which watches the file it is logging to. If the file changes, it is closed and reopened using the file name.
A file change can happen because of usage of programs such as newsyslog and logrotate which perform log file rotation. This handler, intended for use under Unix/Linux, watches the file to see if it has changed since the last emit. (A file is deemed to have changed if its device or inode have changed.) If the file has changed, the old file stream is closed, and the file opened to get a new stream.
This handler is not appropriate for use under Windows, because under Windows open log files cannot be moved or renamed - logging opens the files with exclusive locks - and so there is no need for such a handler. Furthermore,
ST_INO
is not supported under Windows;
stat()
always returns zero for this value.
logging.handlers.
WatchedFileHandler
(
filename
[
,
mode
[
,
encoding
[
,
delay
]
]
]
)
¶
返回新的实例化
WatchedFileHandler
类。打开指定文件并用作日志记录流。若
mode
未指定,
'a'
被使用。若
encoding
不是
None
,用于采用该编码打开文件。若
delay
为 True,则延迟打开文件直到首次调用
emit()
. By default, the file grows indefinitely.
emit
(
record
)
¶
Outputs the record to the file, but first checks to see if the file has changed. If it has, the existing stream is flushed and closed and the file opened again, before outputting the record to the file.
The
RotatingFileHandler
类,位于
logging.handlers
模块,支持轮换磁盘日志文件。
logging.handlers.
RotatingFileHandler
(
filename
,
mode='a'
,
maxBytes=0
,
backupCount=0
,
encoding=None
,
delay=0
)
¶
返回新的实例化
RotatingFileHandler
类。打开指定文件并用作日志记录流。若
mode
未指定,
'a'
被使用。若
encoding
不是
None
,用于采用该编码打开文件。若
delay
为 True,则延迟打开文件直到首次调用
emit()
. By default, the file grows indefinitely.
可以使用
maxBytes
and
backupCount
值以允许文件
rollover
按预先确定大小。当大小即将超过时,关闭文件并为输出默默打开新文件。出现翻转每当当前日志文件接近
maxBytes
in length; if either of
maxBytes
or
backupCount
is zero, rollover never occurs. If
backupCount
非 0,系统将通过追加扩展名 .1、.2 等到文件名以保存旧日志文件。例如,采用
backupCount
5 和基文件名
app.log
,将获得
app.log
,
app.log.1
,
app.log.2
,直到
app.log.5
。正写入的文件始终是
app.log
。 当填充此文被时,关闭它并重命名为
app.log.1
,及若文件
app.log.1
,
app.log.2
等存在,那么将它们重命名为
app.log.2
,
app.log.3
等分别。
2.6 版改变: delay 被添加。
doRollover
(
)
¶
进行翻转,如上所述。
emit
(
record
)
¶
将记录输出到文件,以迎合先前描述的翻转。
The
TimedRotatingFileHandler
类,位于
logging.handlers
模块,支持按一定时间间隔轮换磁盘日志文件。
logging.handlers.
TimedRotatingFileHandler
(
filename
,
when='h'
,
interval=1
,
backupCount=0
,
encoding=None
,
delay=False
,
utc=False
)
¶
返回新的实例化
TimedRotatingFileHandler
class. The specified file is opened and used as the stream for logging. On rotating it also sets the filename suffix. Rotating happens based on the product of
当
and
interval
.
可以使用 当 to specify the type of interval . The list of possible values is below. Note that they are not case sensitive.
|
值 |
Type of interval |
|---|---|
|
|
Seconds |
|
|
Minutes |
|
|
Hours |
|
|
Days |
|
|
Weekday (0=Monday) |
|
|
Roll over at midnight |
When using weekday-based rotation, specify ‘W0’ for Monday, ‘W1’ for Tuesday, and so on up to ‘W6’ for Sunday. In this case, the value passed for interval isn’t used.
The system will save old log files by appending extensions to the filename. The extensions are date-and-time based, using the strftime format
%Y-%m-%d_%H-%M-%S
or a leading portion thereof, depending on the rollover interval.
When computing the next rollover time for the first time (when the handler is created), the last modification time of an existing log file, or else the current time, is used to compute when the next rotation will occur.
若 utc argument is true, times in UTC will be used; otherwise local time is used.
若 backupCount is nonzero, at most backupCount files will be kept, and if more would be created when rollover occurs, the oldest one is deleted. The deletion logic uses the interval to determine which files to delete, so changing the interval may leave old files lying around.
若
delay
为 True,则延迟打开文件直到首次调用
emit()
.
2.6 版改变: delay and utc 被添加。
doRollover
(
)
¶
进行翻转,如上所述。
emit
(
record
)
¶
Outputs the record to the file, catering for rollover as described above.
The
SocketHandler
类,位于
logging.handlers
module, sends logging output to a network socket. The base class uses a TCP socket.
logging.handlers.
SocketHandler
(
host
,
port
)
¶
返回新的实例化
SocketHandler
class intended to communicate with a remote machine whose address is given by
host
and
port
.
close
(
)
¶
关闭套接字。
emit
(
)
¶
Pickles the record’s attribute dictionary and writes it to the socket in binary format. If there is an error with the socket, silently drops the packet. If the connection was previously lost, re-establishes the connection. To unpickle the record at the receiving end into a
LogRecord
,使用
makeLogRecord()
函数。
handleError
(
)
¶
Handles an error which has occurred during
emit()
. The most likely cause is a lost connection. Closes the socket so that we can retry on the next event.
makeSocket
(
)
¶
This is a factory method which allows subclasses to define the precise type of socket they want. The default implementation creates a TCP socket (
socket.SOCK_STREAM
).
makePickle
(
record
)
¶
Pickles the record’s attribute dictionary in binary format with a length prefix, and returns it ready for transmission across the socket.
Note that pickles aren’t completely secure. If you are concerned about security, you may want to override this method to implement a more secure mechanism. For example, you can sign pickles using HMAC and then verify them on the receiving end, or alternatively you can disable unpickling of global objects on the receiving end.
send
(
packet
)
¶
Send a pickled string packet to the socket. This function allows for partial sends which can happen when the network is busy.
createSocket
(
)
¶
Tries to create a socket; on failure, uses an exponential back-off algorithm. On initial failure, the handler will drop the message it was trying to send. When subsequent messages are handled by the same instance, it will not try connecting until some time has passed. The default parameters are such that the initial delay is one second, and if after that delay the connection still can’t be made, the handler will double the delay each time up to a maximum of 30 seconds.
This behaviour is controlled by the following handler attributes:
retryStart
(initial delay, defaulting to 1.0 seconds).
retryFactor
(multiplier, defaulting to 2.0).
retryMax
(maximum delay, defaulting to 30.0 seconds).
This means that if the remote listener starts up after the handler has been used, you could lose messages (since the handler won’t even attempt a connection until the delay has elapsed, but just silently drop messages during the delay period).
The
DatagramHandler
类,位于
logging.handlers
模块,继承自
SocketHandler
以支持通过 UDP 套接字发送日志消息。
logging.handlers.
DatagramHandler
(
host
,
port
)
¶
返回新的实例化
DatagramHandler
class intended to communicate with a remote machine whose address is given by
host
and
port
.
emit
(
)
¶
Pickles the record’s attribute dictionary and writes it to the socket in binary format. If there is an error with the socket, silently drops the packet. To unpickle the record at the receiving end into a
LogRecord
,使用
makeLogRecord()
函数。
makeSocket
(
)
¶
The factory method of
SocketHandler
is here overridden to create a UDP socket (
socket.SOCK_DGRAM
).
send
(
s
)
¶
Send a pickled string to a socket.
The
SysLogHandler
类,位于
logging.handlers
module, supports sending logging messages to a remote or local Unix syslog.
logging.handlers.
SysLogHandler
(
address=('localhost'
,
SYSLOG_UDP_PORT)
,
facility=LOG_USER
,
socktype=socket.SOCK_DGRAM
)
¶
返回新的实例化
SysLogHandler
class intended to communicate with a remote Unix machine whose address is given by
address
in the form of a
(host, port)
tuple. If
address
未指定,
('localhost', 514)
is used. The address is used to open a socket. An alternative to providing a
(host, port)
tuple is providing an address as a string, for example ‘/dev/log’. In this case, a Unix domain socket is used to send the message to the syslog. If
facility
未指定,
LOG_USER
is used. The type of socket opened depends on the
socktype
argument, which defaults to
socket.SOCK_DGRAM
and thus opens a UDP socket. To open a TCP socket (for use with the newer syslog daemons such as rsyslog), specify a value of
socket.SOCK_STREAM
.
Note that if your server is not listening on UDP port 514,
SysLogHandler
may appear not to work. In that case, check what address you should be using for a domain socket - it’s system dependent. For example, on Linux it’s usually ‘/dev/log’ but on OS/X it’s ‘/var/run/syslog’. You’ll need to check your platform and use the appropriate address (you may need to do this check at runtime if your application needs to run on several platforms). On Windows, you pretty much have to use the UDP option.
2.7 版改变: socktype 被添加。
close
(
)
¶
关闭到远程主机的套接字。
emit
(
record
)
¶
The record is formatted, and then sent to the syslog server. If exception information is present, it is not sent to the server.
encodePriority
(
facility
,
priority
)
¶
Encodes the facility and priority into an integer. You can pass in strings or integers - if strings are passed, internal mapping dictionaries are used to convert them to integers.
The symbolic
LOG_
values are defined in
SysLogHandler
and mirror the values defined in the
sys/syslog.h
头文件。
Priorities
|
Name (string) |
Symbolic value |
|---|---|
|
|
LOG_ALERT |
|
|
LOG_CRIT |
|
|
LOG_DEBUG |
|
|
LOG_EMERG |
|
|
LOG_ERR |
|
|
LOG_INFO |
|
|
LOG_NOTICE |
|
|
LOG_WARNING |
Facilities
|
Name (string) |
Symbolic value |
|---|---|
|
|
LOG_AUTH |
|
|
LOG_AUTHPRIV |
|
|
LOG_CRON |
|
|
LOG_DAEMON |
|
|
LOG_FTP |
|
|
LOG_KERN |
|
|
LOG_LPR |
|
|
LOG_MAIL |
|
|
LOG_NEWS |
|
|
LOG_SYSLOG |
|
|
LOG_USER |
|
|
LOG_UUCP |
|
|
LOG_LOCAL0 |
|
|
LOG_LOCAL1 |
|
|
LOG_LOCAL2 |
|
|
LOG_LOCAL3 |
|
|
LOG_LOCAL4 |
|
|
LOG_LOCAL5 |
|
|
LOG_LOCAL6 |
|
|
LOG_LOCAL7 |
mapPriority
(
levelname
)
¶
Maps a logging level name to a syslog priority name. You may need to override this if you are using custom levels, or if the default algorithm is not suitable for your needs. The default algorithm maps
DEBUG
,
INFO
,
WARNING
,
ERROR
and
CRITICAL
to the equivalent syslog names, and all other level names to ‘warning’.
The
NTEventLogHandler
类,位于
logging.handlers
module, supports sending logging messages to a local Windows NT, Windows 2000 or Windows XP event log. Before you can use it, you need Mark Hammond’s Win32 extensions for Python installed.
logging.handlers.
NTEventLogHandler
(
appname
,
dllname=None
,
logtype='Application'
)
¶
返回新的实例化
NTEventLogHandler
类。
appname
is used to define the application name as it appears in the event log. An appropriate registry entry is created using this name. The
dllname
should give the fully qualified pathname of a .dll or .exe which contains message definitions to hold in the log (if not specified,
'win32service.pyd'
is used - this is installed with the Win32 extensions and contains some basic placeholder message definitions. Note that use of these placeholders will make your event logs big, as the entire message source is held in the log. If you want slimmer logs, you have to pass in the name of your own .dll or .exe which contains the message definitions you want to use in the event log). The
logtype
是某一
'Application'
,
'System'
or
'Security'
, and defaults to
'Application'
.
close
(
)
¶
At this point, you can remove the application name from the registry as a source of event log entries. However, if you do this, you will not be able to see the events as you intended in the Event Log Viewer - it needs to be able to access the registry to get the .dll name. The current version does not do this.
emit
(
record
)
¶
Determines the message ID, event category and event type, and then logs the message in the NT event log.
getEventCategory
(
record
)
¶
Returns the event category for the record. Override this if you want to specify your own categories. This version returns 0.
getEventType
(
record
)
¶
Returns the event type for the record. Override this if you want to specify your own types. This version does a mapping using the handler’s typemap attribute, which is set up in
__init__()
to a dictionary which contains mappings for
DEBUG
,
INFO
,
WARNING
,
ERROR
and
CRITICAL
. If you are using your own levels, you will either need to override this method or place a suitable dictionary in the handler’s
typemap
属性。
getMessageID
(
record
)
¶
Returns the message ID for the record. If you are using your own messages, you could do this by having the
msg
passed to the logger being an ID rather than a format string. Then, in here, you could use a dictionary lookup to get the message ID. This version returns 1, which is the base message ID in
win32service.pyd
.
The
SMTPHandler
类,位于
logging.handlers
module, supports sending logging messages to an email address via SMTP.
logging.handlers.
SMTPHandler
(
mailhost
,
fromaddr
,
toaddrs
,
subject
,
credentials=None
,
secure=None
)
¶
返回新的实例化
SMTPHandler
class. The instance is initialized with the from and to addresses and subject line of the email. The
toaddrs
should be a list of strings. To specify a non-standard SMTP port, use the (host, port) tuple format for the
mailhost
argument. If you use a string, the standard SMTP port is used. If your SMTP server requires authentication, you can specify a (username, password) tuple for the
credentials
自变量。
To specify the use of a secure protocol (TLS), pass in a tuple to the
secure
argument. This will only be used when authentication credentials are supplied. The tuple should be either an empty tuple, or a single-value tuple with the name of a keyfile, or a 2-value tuple with the names of the keyfile and certificate file. (This tuple is passed to the
smtplib.SMTP.starttls()
method.)
2.6 版改变: credentials 被添加。
2.7 版改变: secure 被添加。
emit
(
record
)
¶
Formats the record and sends it to the specified addressees.
getSubject
(
record
)
¶
If you want to specify a subject line which is record-dependent, override this method.
The
MemoryHandler
类,位于
logging.handlers
module, supports buffering of logging records in memory, periodically flushing them to a
target
handler. Flushing occurs whenever the buffer is full, or when an event of a certain severity or greater is seen.
MemoryHandler
is a subclass of the more general
BufferingHandler
, which is an abstract class. This buffers logging records in memory. Whenever each record is added to the buffer, a check is made by calling
shouldFlush()
to see if the buffer should be flushed. If it should, then
flush()
is expected to do the flushing.
logging.handlers.
BufferingHandler
(
capacity
)
¶
Initializes the handler with a buffer of the specified capacity.
emit
(
record
)
¶
Appends the record to the buffer. If
shouldFlush()
returns true, calls
flush()
to process the buffer.
flush
(
)
¶
You can override this to implement custom flushing behavior. This version just zaps the buffer to empty.
shouldFlush
(
record
)
¶
Returns true if the buffer is up to capacity. This method can be overridden to implement custom flushing strategies.
logging.handlers.
MemoryHandler
(
capacity
,
flushLevel=ERROR
,
target=None
)
¶
返回新的实例化
MemoryHandler
class. The instance is initialized with a buffer size of
capacity
。若
flushLevel
未指定,
ERROR
源于使用。若无
target
is specified, the target will need to be set using
setTarget()
before this handler does anything useful.
flush
(
)
¶
对于
MemoryHandler
, flushing means just sending the buffered records to the target, if there is one. The buffer is also cleared when this happens. Override if you want different behavior.
setTarget
(
target
)
¶
Sets the target handler for this handler.
shouldFlush
(
record
)
¶
Checks for buffer full or a record at the flushLevel or higher.
The
HTTPHandler
类,位于
logging.handlers
module, supports sending logging messages to a Web server, using either
GET
or
POST
语义。
logging.handlers.
HTTPHandler
(
host
,
url
,
method='GET'
)
¶
返回新的实例化
HTTPHandler
类。
host
can be of the form
host:port
, should you need to use a specific port number.
mapLogRecord
(
record
)
¶
Provides a dictionary, based on
record
, which is to be URL-encoded and sent to the web server. The default implementation just returns
record.__dict__
. This method can be overridden if e.g. only a subset of
LogRecord
is to be sent to the web server, or if more specific customization of what’s sent to the server is required.
emit
(
record
)
¶
Sends the record to the Web server as a URL-encoded dictionary. The
mapLogRecord()
method is used to convert the record to the dictionary to be sent.
注意
Since preparing a record for sending it to a Web server is not the same as a generic formatting operation, using
setFormatter()
to specify a
Formatter
对于
HTTPHandler
has no effect. Instead of calling
format()
, this handler calls
mapLogRecord()
and then
urllib.urlencode()
to encode the dictionary in a form suitable for sending to a Web server.
另请参阅
logging
API reference for the logging module.
logging.config
用于 logging 模块的配置 API。
logging.handlers
— 日志处理程序
键入搜索术语或模块、类、函数名称。