When you run this, on the console you will see error ( 'The five boxing wizards jump quickly.' ) warning ( 'Jail zesty vixen who grabbed pay from quack.' ) logger2. info ( 'How quickly daft jumping zebras vex.' ) logger2. debug ( 'Quick zephyrs blow, vexing daft Jim.' ) logger1. getLogger ( 'myapp.area1' ) logger2 = logging. info ( 'Jackdaws love my big sphinx of quartz.' ) # Now, define a couple of other loggers which might represent areas in your # application: logger1 = logging. addHandler ( console ) # Now, we can log to the root logger, or any other logger. setFormatter ( formatter ) # add the handler to the root logger logging. Formatter ( ' %(name)-12s : %(levelname)-8s %(message)s ' ) # tell the handler to use this format console. INFO ) # set a format which is simpler for console use formatter = logging. DEBUG, format = ' %(asctime)s %(name)-12s %(levelname)-8s %(message)s ', datefmt = '%m- %d %H:%M', filename = '/temp/myapp.log', filemode = 'w' ) # define a Handler which writes INFO messages or higher to the sys.stderr console = logging. Import logging # set up logging to file - see previous section for more details logging. Modify the severity level of the logger and/or handler to debug. At that time, the only change that needs to happen is to
Statements can remain intact in the source code and remain dormant until you Statements, which you will have to delete or comment out later, the bug Print statements for debugging, use bug: Unlike the print Very helpful when writing and testing an application. The ability to create new handlers with higher- or lower-severity filters can be That changed was the addition and configuration of a new handler named fh. Notice that the ‘application’ code does not care about multiple handlers. addHandler ( fh ) # 'application' code logger. setFormatter ( formatter ) # add the handlers to logger logger. Formatter ( ' %(asctime)s - %(name)s - %(levelname)s - %(message)s ' ) ch. ERROR ) # create formatter and add it to the handlers formatter = logging. DEBUG ) # create console handler with a higher log level ch = logging. DEBUG ) # create file handler which logs even debug messages fh = logging. info ( 'done with auxiliary_module.some_function()' ) info ( 'calling auxiliary_module.some_function()' ) auxiliary_module. info ( 'finished auxiliary_something' ) logger. info ( 'calling auxiliary_something' ) a. info ( 'created an instance of auxiliary_module.Auxiliary' ) logger. info ( 'creating an instance of auxiliary_module.Auxiliary' ) a = auxiliary_module. setFormatter ( formatter ) # add the handlers to the logger logger. Formatter ( ' %(asctime)s - %(name)s - %(levelname)s - %(message)s ' ) fh. Import logging import auxiliary_module # create logger with 'spam_application' logger = logging. Adding handlers other than NullHandler to a logger in a library.Using loggers as attributes in a class or passing them as parameters.Opening the same log file multiple times.Using a context manager for selective logging.Formatting times using UTC (GMT) via configuration.Buffering logging messages and outputting them conditionally.Using particular formatting styles throughout your application.Inserting a BOM into messages sent to a SysLogHandler.A more elaborate multiprocessing example.Using a rotator and namer to customize log rotation processing.An example dictionary-based configuration.Subclassing QueueListener - a ZeroMQ example.Subclassing QueueHandler - a ZeroMQ example.Logging to a single file from multiple processes.Using Filters to impart contextual information.Using objects other than dicts to pass contextual information.Using LoggerAdapters to impart contextual information.Adding contextual information to your logging output.Sending and receiving logging events across a network.