Introduction to Logging David Beazley Copyright (C) 2008 http://www.dabeaz.com Note: This is a supplemental subject component to Dave's Python training classes. Details at: http://www.dabeaz.com/python.html Last Update : March 22, 2009 Application Logging Complex systems are often instrumented with some kind of logging capability Debugging and diagnostics Auditing Security Performance statistics Example : Web server logs 2
Error Handling Even in small programs, you often run into subtle issues related to exception handling for line in open("portfolio.dat"): fields = line.split() try: name = fields[0] shares = int(fields[1]) price = float(fields[2]) except ValueError:???? Do you print a warning? print "Bad line", line Do you ignore the error? pass Short answer: It depends 3 The Logging Problem Logging is a problem that seems simple, but usually isn't in practice. Problems: Figuring out what to log Where to send log data Log data management Implementation details 4
Homegrown Solutions Observation: Most significant applications will implement some kind of logging facility. Homegrown solutions tend to grow into a huge tangled hack. No one actually wants to work on logging--- it's often added as a half-baked afterthought. 5 logging Module A module for adding a logging capability to your application. A Python port of log4j (Java/Apache) Highly configurable Implements almost everything that you could possibly want for a logging framework 6
Logger Objects logging module relies on "Logger" objects A Logger is a target for logging messages Routes log data to one or more "handlers" Handler msg Logger Handler... Handler 7 Logging Big Picture Creating Logger objects Sending messages to a Logger object Attaching handlers to a Logger Configuring Logger objects 8
Getting a Logger To create/fetch a Logger object log = logging.getlogger("logname") If this is the first call, a new Logger object is created and associated with the given name If a Logger object with the given name was already created, a reference to that object is returned. This is used to avoid having to pass Logger objects around between program modules. 9 Logging Messages The logging module defines 5 logging "levels" CRITICAL 50 Critical errors ERROR 40 Error messages WARNING 30 Warning messages INFO 20 Information messages DEBUG 10 Debugging messages A logging message consists of a logging level and a logging message string (ERROR,"message") Logger Handler Handler 10
How to Issue a Message Methods for writing to the log log.critical(fmt [, *args ]) log.error(fmt [, *args ]) log.warning(fmt [, *args ]) log.info(fmt [, *args ]) log.debug(fmt [, *args ]) These are always used on some Logger object import logging log = logging.getlogger("logname") log.info("hello World") log.critical("a critical error occurred") 11 Logging Messages Logging functions work like printf log.error("filename '%s' is invalid", filename) log.error("errno=%d, %s", e.errno,e.strerror) log.warning("'%s' doesn't exist. Creating it", filename) Here is sample output Filename 'foo.txt' is invalid errno=9, Bad file descriptor 'out.dat' doesn't exit. Creating it 12
Logging Exceptions Messages can optionally include exception info try:... except RuntimeError: log.error("update failed",exc_info=true) Will include traceback info from the current exception (if any) Sample output with exception traceback Update failed Traceback (most recent call last): File "<stdin>", line 2, in <module> RuntimeError: Invalid user name 13 Example Usage Logging often gets added as an optional feature def read_portfolio(filename,log=none): for line in open(filename): fields = line.split() try: name = fields[0] shares = int(fields[1]) price = float(fields[2]) except ValueError: if log: log.warning("bad line: %s",line) By doing this, the handling of the log message becomes user-configurable More flexible than just hard-coding a print 14
Log Handlers A Logger object only receives messages It does not produce any output Must use a handler to output messages Msg Logger Handler Handler Output 15 Attaching a Handler Example of attaching a handler to a Logger import logging, sys # Create a logger object log = logging.getlogger("logname") # Create a handler object stderr_hand = logging.streamhandler(sys.stderr) # Attach handler to logger log.addhandler(stderr_hand) # Issue a log message (routed to sys.stderr) log.error("an error message") 16
Attaching Multiple Handlers Sending messages to stderr and a file import logging, sys # Create a logger object log = logging.getlogger("logname") # Create handler objects stderr_hand = logging.streamhandler(sys.stderr) logfile_hand = logging.filehandler("log.txt") # Attach the handlers to logger log.addhandler(stderr_hand) log.addhandler(logfile_hand) # Issue a log message. Message goes to both handlers log.error("an error message") 17 Handler Types There are many types of handlers logging.streamhandler logging.filehandler logging.handlers.rotatingfilehandler logging.handlers.timedrotatingfilehandler logging.handlers.sockethandler logging.handlers.datagramhandler logging.handlers.smtphandler logging.handlers.sysloghandler logging.handlers.nteventloghandler logging.handlers.memoryhandler logging.handlers.httphandler Consult a reference for details. More examples later 18
Level Filtering All messages have numerical "level" 50 CRITICAL 40 ERROR 30 WARNING 20 INFO 10 DEBUG 0 NOTSET Each Logger has a level filter log.setlevel(logging.info) Only messages with a level higher than the set level will be forwarded to the handlers 19 Level Filtering All Handlers also have a level setting stderr_hand = logging.streamhander(sys.stderr) stderr_hand.setlevel(logging.info) Handlers only produce output for messages with a level higher than their set level Each Handler can have its own level setting 20
Level Filtering Logger Handler messages level level Output Big picture : Different objects are receiving the messages and only responding to those messages that meet a certain level threshold Logger level is a "global" setting Handler level is just for that handler. 21 Level Filtering Example # Create handler objects stderr_hand = logging.streamhandler(sys.stderr) stderr_hand.setlevel(logging.critical) logfile_hand = logging.filehandler("log.txt") logfile_hand.setlevel(logging.debug) Logger stderr_hand (CRITICAL) messages level level logfile_hand (DEBUG) level 22
Advanced Filtering All log messages can be routed through a filter object. # Define a filter object. Must implement.filter() class MyFilter(logging.Filter): def filter(self,logrecord): # Return True/False to keep message... # Create a filter object myfilt = MyFilter() # Attach it to a Logger object log.addfilter(myfilt) # Attach it to a Handler object hand.addfilter(myfilt) 23 Advanced Filtering Filter objects receive a LogRecord object class MyFilter(logging.Filter): def filter(self,logrecord):... LogRecord attributes logrecord.name logrecord.levelno logrecord.levelname logrecord.pathname logrecord.filename logrecord.module logrecord.lineno logrecord.created logrecord.asctime logrecord.thread logrecord.threadname logrecord.process logrecord.message Name of the logger Numeric logging level Name of logging level Path of source file Filename of source file Module name Line number Time when logging call executed ASCII-formated date/time Thread-ID Thread name Process ID Logged message 24
Filtering Example Only produce messages from a specific module class ModuleFilter(logging.Filter): def init (self,modname): logging.filter. init (self) self.modname = modname def filter(self,logrecord): return logrecord.module == self.modname log = getlogger("logname") modfilt = ModuleFilter("somemod") log.addfilter(modfilt) 25 Multiple Filters Multiple Filters may be added log.addfilter(f) log.addfilter(g) log.addfilter(h) Messages must pass all to be output Filters can be removed later log.removefilter(f) 26
Log Message Format By default, log messages are just the message log.error("an error occurred") An error occurred However, you can add more information Logger name and level Thread names Date/time 27 Customized Formatters Create a Formatter object # Create a message format msgform = logging.formatter( "%(levelname)s:%(name)s:%(asctime)s:%(message)s" ) # Create a handler stderr_hand = logging.streamhandler(sys.stderr) # Set the handler's message format stderr_hand.setformatter(msgform) Formatter determines what gets put in output ERROR:logname:2007-01-24 11:27:26,286:Message 28
Message Format Special format codes %(name)s %(levelno)s %(levelname)s %(pathname)s %(filename)s %(module)s %(lineno)d %(created)f %(asctime)s %(thread)d %(threadname)s %(process)d %(message)s Name of the logger Numeric logging level Name of logging level Path of source file Filename of source file Module name Line number Time when logging call executed ASCII-formated date/time Thread-ID Thread name Process ID Logged message Information is specific to where logging call was made (e.g., source file, line number, etc) 29 Message Formatting Each Handler has a single message formatter Use setformatter() to set it hand.setformatter(form) Different handlers can have different message formats 30
Multiple loggers An application may have many loggers Each is identified by a logger name netlog = logging.getlogger("network") guilog = logging.getlogger("gui") thrlog = logging.getlogger("threads") Each logger object is independent Has own handlers, filters, levels, etc. 31 Hierarchical Loggers Loggers can be organized in a name hierarchy applog = logging.getlogger("app") netlog = logging.getlogger("app.network") conlog = logging.getlogger("app.network.connection") guilog = logging.getlogger("app.gui") thrlog = logging.getlogger("app.threads") Messages flow up the name hierarchy app app.network app.gui app.threads app.network.connection 32
Hierarchical Loggers With a hierarchy, filters and handlers can be attached to every single Logger involved The level of a child logger is inherited from the parent unless set directly To prevent message forwarding on a Logger: log.propagate = False Commentary: Clearly it can get quite advanced 33 The Root Logger Logging module optionally defines a "root" logger to which all logging messages are sent Initialized if you use one of the following: logging.critical() logging.error() logging.warning() logging.info() logging.debug() 34
The Root Logger Root logger is useful for quick solutions and short scripts import logging logging.basicconfig( level = logging.info, filename = "log.txt", format = "%(levelname)s:%(asctime)s:%(message)s" ) logging.info("my program is starting")... 35 Putting it Together Adding logging to your program involves two steps Adding support for logging objects and adding statements to issue log messages Providing a means for configuring the logging environment Let's briefly talk about the second point 36
Logging Configuration Logging is something that frequently gets reconfigured (e.g., during debugging) To configure logging for your application, there are two approaches you can take Isolate it to a well-known module Use config files (ConfigParser) 37 A Sample Configuration A sample configuration module # logconfig.py import logging, sys # Set the message format format = logging.formatter( "%(levelname)-10s %(asctime)s %(message)s") # Create a CRITICAL message handler crit_hand = logging.streamhandler(sys.stderr) crit_hand.setlevel(logging.critical) crit_hand.setformatter(format) # Create a handler for routing to a file applog_hand = logging.filehandler('app.log') applog_hand.setformatter(format) # Create a top-level logger called 'app' app_log = logging.getlogger("app") app_log.setlevel(logging.info) app_log.addhandler(applog_hand) app_log.addhandler(crit_hand) 38
A Sample Configuration To use the previous configuration, you import # main.py import logconfig import otherlib... Mainly, you just need to make sure the logging gets set up before other modules start using it In other modules... import logging log = logging.getlogger('app')... log.critical("an error occurred") 39 Using a Config File You can also configure with an INI file ; logconfig.ini [loggers] keys=root,app [handlers] keys=crit,applog [formatters] keys=format [logger_root] level=notset handlers= [logger_app] level=info propagate=0 qualname=app handlers=crit,applog [handler_crit] class=streamhandler level=critical formatter=format args=(sys.stderr,) [handler_applog] class=filehandler level=notset formatter=format args=('app.log',) [formatter_format] format=%(levelname)-10s %(asctime)s %(message)s datefmt= 40
Reading a Config File To use the previous configuration use this # main.py import logging.config logging.config.fileconfig('logconfig.ini')... The main advantage of using an INI file is that you don't have to go in and modify your code Easier to have a set of different configuration files for different degrees of logging For example, you could have a production configuration and a debugging configuration 41 Summary There are many more subtle configuration details concerning the logging module However, this is enough to give you a small taste of using it in order to get started 42