The
logging module is very flexible; applications can define a tree of logging subsystems, and each logger in this tree can filter out certain messages, format them differently, and direct messages to a varying number of handlers.
All this flexibility can require a lot of configuration. You can write Python statements to create objects and set their properties, but a complex set-up requires verbose but boring code.
logging also supports a fileConfig() function that parses a file, but the file format doesn’t support configuring filters, and it’s messier to generate programmatically.
Python 2.7 adds a
dictConfig() function that uses a dictionary to configure logging. There are many ways to produce a dictionary from different sources: construct one with code; parse a file containing JSON; or use a YAML parsing library if one is installed. For more information see Configuration functions.
The following example configures two loggers, the root logger and a logger named “network”. Messages sent to the root logger will be sent to the system log using the syslog protocol, and messages to the “network” logger will be written to a
network.log file that will be rotated once the log reaches 1MB.
Three smaller enhancements to the
logging module, all implemented by Vinay Sajip, are:- The
SysLogHandlerclass now supports syslogging over TCP. The constructor has a socktype parameter giving the type of socket to use, eithersocket.SOCK_DGRAMfor UDP orsocket.SOCK_STREAMfor TCP. The default protocol remains UDP. Loggerinstances gained agetChild()method that retrieves a descendant logger using a relative path. For example, once you retrieve a logger by doinglog = getLogger('app'), callinglog.getChild('network.listen')is equivalent togetLogger('app.network.listen').- The
LoggerAdapterclass gained anisEnabledFor()method that takes a level and returns whether the underlying logger would process a message of that level of importance.
See also
- PEP 391 - Dictionary-Based Configuration For Logging
- PEP written and implemented by Vinay Sajip.
No comments:
Post a Comment