logging.handlers --- 日誌紀錄處理器¶
The following useful handlers are provided in the package. Note that three of
the handlers (StreamHandler, FileHandler and
NullHandler) are actually defined in the logging module itself,
but have been documented here along with the other handlers.
StreamHandler¶
The StreamHandler class, located in the core logging package,
sends logging output to streams such as sys.stdout, sys.stderr or any
file-like object (or, more precisely, any object which supports write()
and flush() methods).
- class logging.StreamHandler(stream=None)¶
Returns a new instance of the
StreamHandlerclass. If stream is specified, the instance will use it for logging output; otherwise, sys.stderr will be used.- emit(record)¶
If a formatter is specified, it is used to format the record. The record is then written to the stream followed by
terminator. If exception information is present, it is formatted usingtraceback.print_exception()and appended to the stream.
- flush()¶
Flushes the stream by calling its
flush()method. Note that theclose()method is inherited fromHandlerand so does no output, so an explicitflush()call may be needed at times.
- setStream(stream)¶
Sets the instance's stream to the specified value, if it is different. The old stream is flushed before the new stream is set.
- 參數:
stream -- The stream that the handler should use.
- 回傳:
the old stream, if the stream was changed, or
Noneif it wasn't.
在 3.7 版被加入.
- terminator¶
String used as the terminator when writing a formatted record to a stream. Default value is
'\n'.If you don't want a newline termination, you can set the handler instance's
terminatorattribute to the empty string.In earlier versions, the terminator was hardcoded as
'\n'.在 3.2 版被加入.
FileHandler¶
The FileHandler class, located in the core logging package,
sends logging output to a disk file. It inherits the output functionality from
StreamHandler.
- class logging.FileHandler(filename, mode='a', encoding=None, delay=False, errors=None)¶
Returns a new instance of the
FileHandlerclass. The specified file is opened and used as the stream for logging. If mode is not specified,'a'is used. If encoding is notNone, it is used to open the file with that encoding. If delay is true, then file opening is deferred until the first call toemit(). By default, the file grows indefinitely. If errors is specified, it's used to determine how encoding errors are handled.在 3.6 版的變更: As well as string values,
Pathobjects are also accepted for the filename argument.在 3.9 版的變更: 新增 errors 參數。
- close()¶
關閉檔案。
NullHandler¶
在 3.1 版被加入.
The NullHandler class, located in the core logging package,
does not do any formatting or output. It is essentially a 'no-op' handler
for use by library developers.
- class logging.NullHandler¶
Returns a new instance of the
NullHandlerclass.- emit(record)¶
此方法不做任何事。
- handle(record)¶
此方法不做任何事。
- createLock()¶
This method returns
Nonefor the lock, since there is no underlying I/O to which access needs to be serialized.
See Configuring Logging for a Library for more information on how to use
NullHandler.
WatchedFileHandler¶
The WatchedFileHandler class, located in the 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