logging.handlers --- 日誌紀錄處理器

原始碼:Lib/logging/handlers.py


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 StreamHandler class. 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 using traceback.print_exception() and appended to the stream.

flush()

Flushes the stream by calling its flush() method. Note that the close() method is inherited from Handler and so does no output, so an explicit flush() 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 None if 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 terminator attribute 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 FileHandler class. The specified file is opened and used as the stream for logging. If mode is not specified, 'a' is used. If encoding is not None, it is used to open the file with that encoding. If delay is true, then file opening is deferred until the first call to emit(). 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, Path objects are also accepted for the filename argument.

在 3.9 版的變更: 新增 errors 參數。

close()

關閉檔案。

emit(record)

Outputs the record to the file.

Note that if the file was closed due to logging shutdown at exit and the file mode is 'w', the record will not be emitted (see bpo-42378).

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 NullHandler class.

emit(record)

此方法不做任何事。

handle(record)

此方法不做任何事。

createLock()

This method returns None for 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