Logging Cookbook¶
- Author:
Vinay Sajip <vinay_sajip at red-dove dot com>
This page contains a number of recipes related to logging, which have been founduseful in the past. For links to tutorial and reference information, please seeOther resources.
Using logging in multiple modules¶
Multiple calls tologging.getLogger('someLogger')
return a reference to thesame logger object. This is true not only within the same module, but alsoacross modules as long as it is in the same Python interpreter process. It istrue for references to the same object; additionally, application code candefine and configure a parent logger in one module and create (but notconfigure) a child logger in a separate module, and all logger calls to thechild will pass up to the parent. Here is a main module:
importloggingimportauxiliary_module# create logger with 'spam_application'logger=logging.getLogger('spam_application')logger.setLevel(logging.DEBUG)# create file handler which logs even debug messagesfh=logging.FileHandler('spam.log')fh.setLevel(logging.DEBUG)# create console handler with a higher log levelch=logging.StreamHandler()ch.setLevel(logging.ERROR)# create formatter and add it to the handlersformatter=logging.Formatter('%(asctime)s -%(name)s -%(levelname)s -%(message)s')fh.setFormatter(formatter)ch.setFormatter(formatter)# add the handlers to the loggerlogger.addHandler(fh)logger.addHandler(ch)logger.info('creating an instance of auxiliary_module.Auxiliary')a=auxiliary_module.Auxiliary()logger.info('created an instance of auxiliary_module.Auxiliary')logger.info('calling auxiliary_module.Auxiliary.do_something')a.do_something()logger.info('finished auxiliary_module.Auxiliary.do_something')logger.info('calling auxiliary_module.some_function()')auxiliary_module.some_function()logger.info('done with auxiliary_module.some_function()')
Here is the auxiliary module:
importlogging# create loggermodule_logger=logging.getLogger('spam_application.auxiliary')classAuxiliary:def__init__(self):self.logger=logging.getLogger('spam_application.auxiliary.Auxiliary')self.logger.info('creating an instance of Auxiliary')defdo_something(self):self.logger.info('doing something')a=1+1self.logger.info('done doing something')defsome_function():module_logger.info('received a call to "some_function"')
The output looks like this:
2005-03-23 23:47:11,663 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary2005-03-23 23:47:11,665 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary2005-03-23 23:47:11,668 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO - doing something2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO - done doing something2005-03-23 23:47:11,670 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something2005-03-23 23:47:11,671 - spam_application - INFO - calling auxiliary_module.some_function()2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO - received a call to 'some_function'2005-03-23 23:47:11,673 - spam_application - INFO - done with auxiliary_module.some_function()
Logging from multiple threads¶
Logging from multiple threads requires no special effort. The following exampleshows logging from the main (initial) thread and another thread:
importloggingimportthreadingimporttimedefworker(arg):whilenotarg['stop']:logging.debug('Hi from myfunc')time.sleep(0.5)defmain():logging.basicConfig(level=logging.DEBUG,format='%(relativeCreated)6d%(threadName)s%(message)s')info={'stop':False}thread=threading.Thread(target=worker,args=(info,))thread.start()whileTrue:try:logging.debug('Hello from main')time.sleep(0.75)exceptKeyboardInterrupt:info['stop']=Truebreakthread.join()if__name__=='__main__':main()
When run, the script should print something like the following:
0 Thread-1 Hi from myfunc 3 MainThread Hello from main 505 Thread-1 Hi from myfunc 755 MainThread Hello from main1007 Thread-1 Hi from myfunc1507 MainThread Hello from main1508 Thread-1 Hi from myfunc2010 Thread-1 Hi from myfunc2258 MainThread Hello from main2512 Thread-1 Hi from myfunc3009 MainThread Hello from main3013 Thread-1 Hi from myfunc3515 Thread-1 Hi from myfunc3761 MainThread Hello from main4017 Thread-1 Hi from myfunc4513 MainThread Hello from main4518 Thread-1 Hi from myfunc
This shows the logging output interspersed as one might expect. This approachworks for more threads than shown here, of course.
Multiple handlers and formatters¶
Loggers are plain Python objects. TheaddHandler()
method has nominimum or maximum quota for the number of handlers you may add. Sometimes itwill be beneficial for an application to log all messages of all severities to atext file while simultaneously logging errors or above to the console. To setthis up, simply configure the appropriate handlers. The logging calls in theapplication code will remain unchanged. Here is a slight modification to theprevious simple module-based configuration example:
importlogginglogger=logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create file handler which logs even debug messagesfh=logging.FileHandler('spam.log')fh.setLevel(logging.DEBUG)# create console handler with a higher log levelch=logging.StreamHandler()ch.setLevel(logging.ERROR)# create formatter and add it to the handlersformatter=logging.Formatter('%(asctime)s -%(name)s -%(levelname)s -%(message)s')ch.setFormatter(formatter)fh.setFormatter(formatter)# add the handlers to loggerlogger.addHandler(ch)logger.addHandler(fh)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warning('warn message')logger.error('error message')logger.critical('critical message')
Notice that the ‘application’ code does not care about multiple handlers. Allthat changed was the addition and configuration of a new handler namedfh.
The ability to create new handlers with higher- or lower-severity filters can bevery helpful when writing and testing an application. Instead of using manyprint
statements for debugging, uselogger.debug
: Unlike the printstatements, which you will have to delete or comment out later, the logger.debugstatements can remain intact in the source code and remain dormant until youneed them again. At that time, the only change that needs to happen is tomodify the severity level of the logger and/or handler to debug.
Logging to multiple destinations¶
Let’s say you want to log to console and file with different message formats andin differing circumstances. Say you want to log messages with levels of DEBUGand higher to file, and those messages at level INFO and higher to the console.Let’s also assume that the file should contain timestamps, but the consolemessages should not. Here’s how you can achieve this:
importlogging# set up logging to file - see previous section for more detailslogging.basicConfig(level=logging.DEBUG,format='%(asctime)s%(name)-12s%(levelname)-8s%(message)s',datefmt='%m-%d %H:%M',filename='/tmp/myapp.log',filemode='w')# define a Handler which writes INFO messages or higher to the sys.stderrconsole=logging.StreamHandler()console.setLevel(logging.INFO)# set a format which is simpler for console useformatter=logging.Formatter('%(name)-12s:%(levelname)-8s%(message)s')# tell the handler to use this formatconsole.setFormatter(formatter)# add the handler to the root loggerlogging.getLogger('').addHandler(console)# Now, we can log to the root logger, or any other logger. First the root...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.getLogger('myapp.area1')logger2=logging.getLogger('myapp.area2')logger1.debug('Quick zephyrs blow, vexing daft Jim.')logger1.info('How quickly daft jumping zebras vex.')logger2.warning('Jail zesty vixen who grabbed pay from quack.')logger2.error('The five boxing wizards jump quickly.')
When you run this, on the console you will see
root : INFO Jackdaws love my big sphinx of quartz.myapp.area1 : INFO How quickly daft jumping zebras vex.myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.myapp.area2 : ERROR The five boxing wizards jump quickly.
and in the file you will see something like
10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
As you can see, the DEBUG message only shows up in the file. The other messagesare sent to both destinations.
This example uses console and file handlers, but you can use any number andcombination of handlers you choose.
Note that the above choice of log filename/tmp/myapp.log
implies use of astandard location for temporary files on POSIX systems. On Windows, you may need tochoose a different directory name for the log - just ensure that the directory existsand that you have the permissions to create and update files in it.
Custom handling of levels¶
Sometimes, you might want to do something slightly different from the standardhandling of levels in handlers, where all levels above a threshold getprocessed by a handler. To do this, you need to use filters. Let’s look at ascenario where you want to arrange things as follows:
Send messages of severity
INFO
andWARNING
tosys.stdout
Send messages of severity
ERROR
and above tosys.stderr
Send messages of severity
DEBUG
and above to fileapp.log
Suppose you configure logging with the following JSON:
{"version":1,"disable_existing_loggers":false,"formatters":{"simple":{"format":"%(levelname)-8s - %(message)s"}},"handlers":{"stdout":{"class":"logging.StreamHandler","level":"INFO","formatter":"simple","stream":"ext://sys.stdout"},"stderr":{"class":"logging.StreamHandler","level":"ERROR","formatter":"simple","stream":"ext://sys.stderr"},"file":{"class":"logging.FileHandler","formatter":"simple","filename":"app.log","mode":"w"}},"root":{"level":"DEBUG","handlers":["stderr","stdout","file"]}}
This configuration doesalmost what we want, except thatsys.stdout
would show messagesof severityERROR
and only events of this severity and higher will be trackedas well asINFO
andWARNING
messages. To prevent this, we can set up a filter whichexcludes those messages and add it to the relevant handler. This can be configured byadding afilters
section parallel toformatters
andhandlers
:
{"filters":{"warnings_and_below":{"()":"__main__.filter_maker","level":"WARNING"}}}
and changing the section on thestdout
handler to add it:
{"stdout":{"class":"logging.StreamHandler","level":"INFO","formatter":"simple","stream":"ext://sys.stdout","filters":["warnings_and_below"]}}
A filter is just a function, so we can define thefilter_maker
(a factoryfunction) as follows:
deffilter_maker(level):level=getattr(logging,level)deffilter(record):returnrecord.levelno<=levelreturnfilter
This converts the string argument passed in to a numeric level, and returns afunction which only returnsTrue
if the level of the passed in record isat or below the specified level. Note that in this example I have defined thefilter_maker
in a test scriptmain.py
that I run from the command line,so its module will be__main__
- hence the__main__.filter_maker
in thefilter configuration. You will need to change that if you define it in adifferent module.
With the filter added, we can runmain.py
, which in full is:
importjsonimportloggingimportlogging.configCONFIG='''{ "version": 1, "disable_existing_loggers": false, "formatters": { "simple": { "format": "%(levelname)-8s -%(message)s" } }, "filters": { "warnings_and_below": { "()" : "__main__.filter_maker", "level": "WARNING" } }, "handlers": { "stdout": { "class": "logging.StreamHandler", "level": "INFO", "formatter": "simple", "stream": "ext://sys.stdout", "filters": ["warnings_and_below"] }, "stderr": { "class": "logging.StreamHandler", "level": "ERROR", "formatter": "simple", "stream": "ext://sys.stderr" }, "file": { "class": "logging.FileHandler", "formatter": "simple", "filename": "app.log", "mode": "w" } }, "root": { "level": "DEBUG", "handlers": [ "stderr", "stdout", "file" ] }}'''deffilter_maker(level):level=getattr(logging,level)deffilter(record):returnrecord.levelno<=levelreturnfilterlogging.config.dictConfig(json.loads(CONFIG))logging.debug('A DEBUG message')logging.info('An INFO message')logging.warning('A WARNING message')logging.error('An ERROR message')logging.critical('A CRITICAL message')
And after running it like this:
pythonmain.py2>stderr.log>stdout.log
We can see the results are as expected:
$more*.log::::::::::::::app.log::::::::::::::DEBUG-ADEBUGmessageINFO-AnINFOmessageWARNING-AWARNINGmessageERROR-AnERRORmessageCRITICAL-ACRITICALmessage::::::::::::::stderr.log::::::::::::::ERROR-AnERRORmessageCRITICAL-ACRITICALmessage::::::::::::::stdout.log::::::::::::::INFO-AnINFOmessageWARNING-AWARNINGmessage
Configuration server example¶
Here is an example of a module using the logging configuration server:
importloggingimportlogging.configimporttimeimportos# read initial config filelogging.config.fileConfig('logging.conf')# create and start listener on port 9999t=logging.config.listen(9999)t.start()logger=logging.getLogger('simpleExample')try:# loop through logging calls to see the difference# new configurations make, until Ctrl+C is pressedwhileTrue:logger.debug('debug message')logger.info('info message')logger.warning('warn message')logger.error('error message')logger.critical('critical message')time.sleep(5)exceptKeyboardInterrupt:# cleanuplogging.config.stopListening()t.join()
And here is a script that takes a filename and sends that file to the server,properly preceded with the binary-encoded length, as the new loggingconfiguration:
#!/usr/bin/env pythonimportsocket,sys,structwithopen(sys.argv[1],'rb')asf:data_to_send=f.read()HOST='localhost'PORT=9999s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)print('connecting...')s.connect((HOST,PORT))print('sending config...')s.send(struct.pack('>L',len(data_to_send)))s.send(data_to_send)s.close()print('complete')
Dealing with handlers that block¶
Sometimes you have to get your logging handlers to do their work withoutblocking the thread you’re logging from. This is common in web applications,though of course it also occurs in other scenarios.
A common culprit which demonstrates sluggish behaviour is theSMTPHandler
: sending emails can take a long time, for anumber of reasons outside the developer’s control (for example, a poorlyperforming mail or network infrastructure). But almost any network-basedhandler can block: Even aSocketHandler
operation may do aDNS query under the hood which is too slow (and this query can be deep in thesocket library code, below the Python layer, and outside your control).
One solution is to use a two-part approach. For the first part, attach only aQueueHandler
to those loggers which are accessed fromperformance-critical threads. They simply write to their queue, which can besized to a large enough capacity or initialized with no upper bound to theirsize. The write to the queue will typically be accepted quickly, though youwill probably need to catch thequeue.Full
exception as a precautionin your code. If you are a library developer who has performance-criticalthreads in their code, be sure to document this (together with a suggestion toattach onlyQueueHandlers
to your loggers) for the benefit of otherdevelopers who will use your code.
The second part of the solution isQueueListener
, which has beendesigned as the counterpart toQueueHandler
. AQueueListener
is very simple: it’s passed a queue and some handlers,and it fires up an internal thread which listens to its queue for LogRecordssent fromQueueHandlers
(or any other source ofLogRecords
, for thatmatter). TheLogRecords
are removed from the queue and passed to thehandlers for processing.
The advantage of having a separateQueueListener
class is that youcan use the same instance to service multipleQueueHandlers
. This is moreresource-friendly than, say, having threaded versions of the existing handlerclasses, which would eat up one thread per handler for no particular benefit.
An example of using these two classes follows (imports omitted):
que=queue.Queue(-1)# no limit on sizequeue_handler=QueueHandler(que)handler=logging.StreamHandler()listener=QueueListener(que,handler)root=logging.getLogger()root.addHandler(queue_handler)formatter=logging.Formatter('%(threadName)s:%(message)s')handler.setFormatter(formatter)listener.start()# The log output will display the thread which generated# the event (the main thread) rather than the internal# thread which monitors the internal queue. This is what# you want to happen.root.warning('Look out!')listener.stop()
which, when run, will produce:
MainThread: Look out!
Note
Although the earlier discussion wasn’t specifically talking aboutasync code, but rather about slow logging handlers, it should be noted thatwhen logging from async code, network and even file handlers could lead toproblems (blocking the event loop) because some logging is done fromasyncio
internals. It might be best, if any async code is used in anapplication, to use the above approach for logging, so that any blocking coderuns only in theQueueListener
thread.
Changed in version 3.5:Prior to Python 3.5, theQueueListener
always passed every messagereceived from the queue to every handler it was initialized with. (This wasbecause it was assumed that level filtering was all done on the other side,where the queue is filled.) From 3.5 onwards, this behaviour can be changedby passing a keyword argumentrespect_handler_level=True
to thelistener’s constructor. When this is done, the listener compares the levelof each message with the handler’s level, and only passes a message to ahandler if it’s appropriate to do so.
Sending and receiving logging events across a network¶
Let’s say you want to send logging events across a network, and handle them atthe receiving end. A simple way of doing this is attaching aSocketHandler
instance to the root logger at the sending end:
importlogging,logging.handlersrootLogger=logging.getLogger('')rootLogger.setLevel(logging.DEBUG)socketHandler=logging.handlers.SocketHandler('localhost',logging.handlers.DEFAULT_TCP_LOGGING_PORT)# don't bother with a formatter, since a socket handler sends the event as# an unformatted picklerootLogger.addHandler(socketHandler)# Now, we can log to the root logger, or any other logger. First the root...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.getLogger('myapp.area1')logger2=logging.getLogger('myapp.area2')logger1.debug('Quick zephyrs blow, vexing daft Jim.')logger1.info('How quickly daft jumping zebras vex.')logger2.warning('Jail zesty vixen who grabbed pay from quack.')logger2.error('The five boxing wizards jump quickly.')
At the receiving end, you can set up a receiver using thesocketserver
module. Here is a basic working example:
importpickleimportloggingimportlogging.handlersimportsocketserverimportstructclassLogRecordStreamHandler(socketserver.StreamRequestHandler):"""Handler for a streaming logging request. This basically logs the record using whatever logging policy is configured locally. """defhandle(self):""" Handle multiple requests - each expected to be a 4-byte length, followed by the LogRecord in pickle format. Logs the record according to whatever policy is configured locally. """whileTrue:chunk=self.connection.recv(4)iflen(chunk)<4:breakslen=struct.unpack('>L',chunk)[0]chunk=self.connection.recv(slen)whilelen(chunk)<slen:chunk=chunk+self.connection.recv(slen-len(chunk))obj=self.unPickle(chunk)record=logging.makeLogRecord(obj)self.handleLogRecord(record)defunPickle(self,data):returnpickle.loads(data)defhandleLogRecord(self,record):# if a name is specified, we use the named logger rather than the one# implied by the record.ifself.server.lognameisnotNone:name=self.server.lognameelse:name=record.namelogger=logging.getLogger(name)# N.B. EVERY record gets logged. This is because Logger.handle# is normally called AFTER logger-level filtering. If you want# to do filtering, do it at the client end to save wasting# cycles and network bandwidth!logger.handle(record)classLogRecordSocketReceiver(socketserver.ThreadingTCPServer):""" Simple TCP socket-based logging receiver suitable for testing. """allow_reuse_address=Truedef__init__(self,host='localhost',port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,handler=LogRecordStreamHandler):socketserver.ThreadingTCPServer.__init__(self,(host,port),handler)self.abort=0self.timeout=1self.logname=Nonedefserve_until_stopped(self):importselectabort=0whilenotabort:rd,wr,ex=select.select([self.socket.fileno()],[],[],self.timeout)ifrd:self.handle_request()abort=self.abortdefmain():logging.basicConfig(format='%(relativeCreated)5d%(name)-15s%(levelname)-8s%(message)s')tcpserver=LogRecordSocketReceiver()print('About to start TCP server...')tcpserver.serve_until_stopped()if__name__=='__main__':main()
First run the server, and then the client. On the client side, nothing isprinted on the console; on the server side, you should see something like:
About to start TCP server... 59 root INFO Jackdaws love my big sphinx of quartz. 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. 69 myapp.area1 INFO How quickly daft jumping zebras vex. 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. 69 myapp.area2 ERROR The five boxing wizards jump quickly.
Note that there are some security issues with pickle in some scenarios. Ifthese affect you, you can use an alternative serialization scheme by overridingthemakePickle()
method and implementing youralternative there, as well as adapting the above script to use your alternativeserialization.
Running a logging socket listener in production¶
To run a logging listener in production, you may need to use aprocess-management tool such asSupervisor.Here is a Gistwhich provides the bare-bones files to run the above functionality usingSupervisor. It consists of the following files:
File | Purpose |
---|---|
| A Bash script to prepare the environment fortesting |
| The Supervisor configuration file, which hasentries for the listener and a multi-process webapplication |
| A Bash script to ensure that Supervisor is runningwith the above configuration |
| The socket listener program which receives logevents and records them to a file |
| A simple web application which performs loggingvia a socket connected to the listener |
| A JSON configuration file for the web application |
| A Python script to exercise the web application |
The web application usesGunicorn, which is apopular web application server that starts multiple worker processes to handlerequests. This example setup shows how the workers can write to the same log filewithout conflicting with one another — they all go through the socket listener.
To test these files, do the following in a POSIX environment:
Downloadthe Gistas a ZIP archive using theDownload ZIP button.
Unzip the above files from the archive into a scratch directory.
In the scratch directory, run
bashprepare.sh
to get things ready.This creates arun
subdirectory to contain Supervisor-related andlog files, and avenv
subdirectory to contain a virtual environmentinto whichbottle
,gunicorn
andsupervisor
are installed.Run
bashensure_app.sh
to ensure that Supervisor is running withthe above configuration.Run
venv/bin/pythonclient.py
to exercise the web application,which will lead to records being written to the log.Inspect the log files in the
run
subdirectory. You should see themost recent log lines in files matching the patternapp.log*
. Theywon’t be in any particular order, since they have been handled concurrentlyby different worker processes in a non-deterministic way.You can shut down the listener and the web application by running
venv/bin/supervisorctl-csupervisor.confshutdown
.
You may need to tweak the configuration files in the unlikely event that theconfigured ports clash with something else in your test environment.
The default configuration uses a TCP socket on port 9020. You can use a UnixDomain socket instead of a TCP socket by doing the following:
In
listener.json
, add asocket
key with the path to the domainsocket you want to use. If this key is present, the listener listens on thecorresponding domain socket and not on a TCP socket (theport
key isignored).In
webapp.json
, change the socket handler configuration dictionaryso that thehost
value is the path to the domain socket, and set theport
value tonull
.
Adding contextual information to your logging output¶
Sometimes you want logging output to contain contextual information inaddition to the parameters passed to the logging call. For example, in anetworked application, it may be desirable to log client-specific informationin the log (e.g. remote client’s username, or IP address). Although you coulduse theextra parameter to achieve this, it’s not always convenient to passthe information in this way. While it might be tempting to createLogger
instances on a per-connection basis, this is not a good ideabecause these instances are not garbage collected. While this is not a problemin practice, when the number ofLogger
instances is dependent on thelevel of granularity you want to use in logging an application, it couldbe hard to manage if the number ofLogger
instances becomeseffectively unbounded.
Using LoggerAdapters to impart contextual information¶
An easy way in which you can pass contextual information to be output alongwith logging event information is to use theLoggerAdapter
class.This class is designed to look like aLogger
, so that you can calldebug()
,info()
,warning()
,error()
,exception()
,critical()
andlog()
. These methods have thesame signatures as their counterparts inLogger
, so you can use thetwo types of instances interchangeably.
When you create an instance ofLoggerAdapter
, you pass it aLogger
instance and a dict-like object which contains your contextualinformation. When you call one of the logging methods on an instance ofLoggerAdapter
, it delegates the call to the underlying instance ofLogger
passed to its constructor, and arranges to pass the contextualinformation in the delegated call. Here’s a snippet from the code ofLoggerAdapter
:
defdebug(self,msg,/,*args,**kwargs):""" Delegate a debug call to the underlying logger, after adding contextual information from this adapter instance. """msg,kwargs=self.process(msg,kwargs)self.logger.debug(msg,*args,**kwargs)
Theprocess()
method ofLoggerAdapter
is where thecontextual information is added to the logging output. It’s passed the messageand keyword arguments of the logging call, and it passes back (potentially)modified versions of these to use in the call to the underlying logger. Thedefault implementation of this method leaves the message alone, but insertsan ‘extra’ key in the keyword argument whose value is the dict-like objectpassed to the constructor. Of course, if you had passed an ‘extra’ keywordargument in the call to the adapter, it will be silently overwritten.
The advantage of using ‘extra’ is that the values in the dict-like object aremerged into theLogRecord
instance’s __dict__, allowing you to usecustomized strings with yourFormatter
instances which know aboutthe keys of the dict-like object. If you need a different method, e.g. if youwant to prepend or append the contextual information to the message string,you just need to subclassLoggerAdapter
and overrideprocess()
to do what you need. Here is a simple example:
classCustomAdapter(logging.LoggerAdapter):""" This example adapter expects the passed in dict-like object to have a 'connid' key, whose value in brackets is prepended to the log message. """defprocess(self,msg,kwargs):return'[%s]%s'%(self.extra['connid'],msg),kwargs
which you can use like this:
logger=logging.getLogger(__name__)adapter=CustomAdapter(logger,{'connid':some_conn_id})
Then any events that you log to the adapter will have the value ofsome_conn_id
prepended to the log messages.
Using objects other than dicts to pass contextual information¶
You don’t need to pass an actual dict to aLoggerAdapter
- you couldpass an instance of a class which implements__getitem__
and__iter__
sothat it looks like a dict to logging. This would be useful if you want togenerate values dynamically (whereas the values in a dict would be constant).
Using Filters to impart contextual information¶
You can also add contextual information to log output using a user-definedFilter
.Filter
instances are allowed to modify theLogRecords
passed to them, including adding additional attributes which can then be outputusing a suitable format string, or if needed a customFormatter
.
For example in a web application, the request being processed (or at least,the interesting parts of it) can be stored in a threadlocal(threading.local
) variable, and then accessed from aFilter
toadd, say, information from the request - say, the remote IP address and remoteuser’s username - to theLogRecord
, using the attribute names ‘ip’ and‘user’ as in theLoggerAdapter
example above. In that case, the same formatstring can be used to get similar output to that shown above. Here’s an examplescript:
importloggingfromrandomimportchoiceclassContextFilter(logging.Filter):""" This is a filter which injects contextual information into the log. Rather than use actual contextual information, we just use random data in this demo. """USERS=['jim','fred','sheila']IPS=['123.231.231.123','127.0.0.1','192.168.0.1']deffilter(self,record):record.ip=choice(ContextFilter.IPS)record.user=choice(ContextFilter.USERS)returnTrueif__name__=='__main__':levels=(logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL)logging.basicConfig(level=logging.DEBUG,format='%(asctime)-15s%(name)-5s%(levelname)-8s IP:%(ip)-15s User:%(user)-8s%(message)s')a1=logging.getLogger('a.b.c')a2=logging.getLogger('d.e.f')f=ContextFilter()a1.addFilter(f)a2.addFilter(f)a1.debug('A debug message')a1.info('An info message with%s','some parameters')forxinrange(10):lvl=choice(levels)lvlname=logging.getLevelName(lvl)a2.log(lvl,'A message at%s level with%d%s',lvlname,2,'parameters')
which, when run, produces something like:
2010-09-06 22:38:15,292 a.b.c DEBUG IP: 123.231.231.123 User: fred A debug message2010-09-06 22:38:15,300 a.b.c INFO IP: 192.168.0.1 User: sheila An info message with some parameters2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL level with 2 parameters2010-09-06 22:38:15,300 d.e.f ERROR IP: 127.0.0.1 User: jim A message at ERROR level with 2 parameters2010-09-06 22:38:15,300 d.e.f DEBUG IP: 127.0.0.1 User: sheila A message at DEBUG level with 2 parameters2010-09-06 22:38:15,300 d.e.f ERROR IP: 123.231.231.123 User: fred A message at ERROR level with 2 parameters2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 192.168.0.1 User: jim A message at CRITICAL level with 2 parameters2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1 User: sheila A message at CRITICAL level with 2 parameters2010-09-06 22:38:15,300 d.e.f DEBUG IP: 192.168.0.1 User: jim A message at DEBUG level with 2 parameters2010-09-06 22:38:15,301 d.e.f ERROR IP: 127.0.0.1 User: sheila A message at ERROR level with 2 parameters2010-09-06 22:38:15,301 d.e.f DEBUG IP: 123.231.231.123 User: fred A message at DEBUG level with 2 parameters2010-09-06 22:38:15,301 d.e.f INFO IP: 123.231.231.123 User: fred A message at INFO level with 2 parameters
Use ofcontextvars
¶
Since Python 3.7, thecontextvars
module has provided context-local storagewhich works for boththreading
andasyncio
processing needs. This typeof storage may thus be generally preferable to thread-locals. The following exampleshows how, in a multi-threaded environment, logs can populated with contextualinformation such as, for example, request attributes handled by web applications.
For the purposes of illustration, say that you have different web applications, eachindependent of the other but running in the same Python process and using a librarycommon to them. How can each of these applications have their own log, where alllogging messages from the library (and other request processing code) are directed tothe appropriate application’s log file, while including in the log additionalcontextual information such as client IP, HTTP request method and client username?
Let’s assume that the library can be simulated by the following code:
# webapplib.pyimportloggingimporttimelogger=logging.getLogger(__name__)defuseful():# Just a representative event logged from the librarylogger.debug('Hello from webapplib!')# Just sleep for a bit so other threads get to runtime.sleep(0.01)
We can simulate the multiple web applications by means of two simple classes,Request
andWebApp
. These simulate how real threaded web applications work -each request is handled by a thread:
# main.pyimportargparsefromcontextvarsimportContextVarimportloggingimportosfromrandomimportchoiceimportthreadingimportwebappliblogger=logging.getLogger(__name__)root=logging.getLogger()root.setLevel(logging.DEBUG)classRequest:""" A simple dummy request class which just holds dummy HTTP request method, client IP address and client username """def__init__(self,method,ip,user):self.method=methodself.ip=ipself.user=user# A dummy set of requests which will be used in the simulation - we'll just pick# from this list randomly. Note that all GET requests are from 192.168.2.XXX# addresses, whereas POST requests are from 192.16.3.XXX addresses. Three users# are represented in the sample requests.REQUESTS=[Request('GET','192.168.2.20','jim'),Request('POST','192.168.3.20','fred'),Request('GET','192.168.2.21','sheila'),Request('POST','192.168.3.21','jim'),Request('GET','192.168.2.22','fred'),Request('POST','192.168.3.22','sheila'),]# Note that the format string includes references to request context information# such as HTTP method, client IP and usernameformatter=logging.Formatter('%(threadName)-11s%(appName)s%(name)-9s%(user)-6s%(ip)s%(method)-4s%(message)s')# Create our context variables. These will be filled at the start of request# processing, and used in the logging that happens during that processingctx_request=ContextVar('request')ctx_appname=ContextVar('appname')classInjectingFilter(logging.Filter):""" A filter which injects context-specific information into logs and ensures that only information for a specific webapp is included in its log """def__init__(self,app):self.app=appdeffilter(self,record):request=ctx_request.get()record.method=request.methodrecord.ip=request.iprecord.user=request.userrecord.appName=appName=ctx_appname.get()returnappName==self.app.nameclassWebApp:""" A dummy web application class which has its own handler and filter for a webapp-specific log. """def__init__(self,name):self.name=namehandler=logging.FileHandler(name+'.log','w')f=InjectingFilter(self)handler.setFormatter(formatter)handler.addFilter(f)root.addHandler(handler)self.num_requests=0defprocess_request(self,request):""" This is the dummy method for processing a request. It's called on a different thread for every request. We store the context information into the context vars before doing anything else. """ctx_request.set(request)ctx_appname.set(self.name)self.num_requests+=1logger.debug('Request processing started')webapplib.useful()logger.debug('Request processing finished')defmain():fn=os.path.splitext(os.path.basename(__file__))[0]adhf=argparse.ArgumentDefaultsHelpFormatterap=argparse.ArgumentParser(formatter_class=adhf,prog=fn,description='Simulate a couple of web ''applications handling some ''requests, showing how request ''context can be used to ''populate logs')aa=ap.add_argumentaa('--count','-c',type=int,default=100,help='How many requests to simulate')options=ap.parse_args()# Create the dummy webapps and put them in a list which we can use to select# from randomlyapp1=WebApp('app1')app2=WebApp('app2')apps=[app1,app2]threads=[]# Add a common handler which will capture all eventshandler=logging.FileHandler('app.log','w')handler.setFormatter(formatter)root.addHandler(handler)# Generate calls to process requestsforiinrange(options.count):try:# Pick an app at random and a request for it to processapp=choice(apps)request=choice(REQUESTS)# Process the request in its own threadt=threading.Thread(target=app.process_request,args=(request,))threads.append(t)t.start()exceptKeyboardInterrupt:break# Wait for the threads to terminatefortinthreads:t.join()forappinapps:print('%s processed%s requests'%(app.name,app.num_requests))if__name__=='__main__':main()
If you run the above, you should find that roughly half the requests gointoapp1.log
and the rest intoapp2.log
, and the all the requests arelogged toapp.log
. Each webapp-specific log will contain only log entries foronly that webapp, and the request information will be displayed consistently in thelog (i.e. the information in each dummy request will always appear together in a logline). This is illustrated by the following shell output:
~/logging-contextual-webapp$pythonmain.pyapp1processed51requestsapp2processed49requests~/logging-contextual-webapp$wc-l*.log153app1.log147app2.log300app.log600total~/logging-contextual-webapp$head-3app1.logThread-3(process_request)app1__main__jim192.168.3.21POSTRequestprocessingstartedThread-3(process_request)app1webapplibjim192.168.3.21POSTHellofromwebapplib!Thread-5(process_request)app1__main__jim192.168.3.21POSTRequestprocessingstarted~/logging-contextual-webapp$head-3app2.logThread-1(process_request)app2__main__sheila192.168.2.21GETRequestprocessingstartedThread-1(process_request)app2webapplibsheila192.168.2.21GETHellofromwebapplib!Thread-2(process_request)app2__main__jim192.168.2.20GETRequestprocessingstarted~/logging-contextual-webapp$headapp.logThread-1(process_request)app2__main__sheila192.168.2.21GETRequestprocessingstartedThread-1(process_request)app2webapplibsheila192.168.2.21GETHellofromwebapplib!Thread-2(process_request)app2__main__jim192.168.2.20GETRequestprocessingstartedThread-3(process_request)app1__main__jim192.168.3.21POSTRequestprocessingstartedThread-2(process_request)app2webapplibjim192.168.2.20GETHellofromwebapplib!Thread-3(process_request)app1webapplibjim192.168.3.21POSTHellofromwebapplib!Thread-4(process_request)app2__main__fred192.168.2.22GETRequestprocessingstartedThread-5(process_request)app1__main__jim192.168.3.21POSTRequestprocessingstartedThread-4(process_request)app2webapplibfred192.168.2.22GETHellofromwebapplib!Thread-6(process_request)app1__main__jim192.168.3.21POSTRequestprocessingstarted~/logging-contextual-webapp$grepapp1app1.log|wc-l153~/logging-contextual-webapp$grepapp2app2.log|wc-l147~/logging-contextual-webapp$grepapp1app.log|wc-l153~/logging-contextual-webapp$grepapp2app.log|wc-l147
Imparting contextual information in handlers¶
EachHandler
has its own chain of filters.If you want to add contextual information to aLogRecord
without leakingit to other handlers, you can use a filter that returnsa newLogRecord
instead of modifying it in-place, as shown in the following script:
importcopyimportloggingdeffilter(record:logging.LogRecord):record=copy.copy(record)record.user='jim'returnrecordif__name__=='__main__':logger=logging.getLogger()logger.setLevel(logging.INFO)handler=logging.StreamHandler()formatter=logging.Formatter('%(message)s from%(user)-8s')handler.setFormatter(formatter)handler.addFilter(filter)logger.addHandler(handler)logger.info('A log message')
Logging to a single file from multiple processes¶
Although logging is thread-safe, and logging to a single file from multiplethreads in a single processis supported, logging to a single file frommultiple processes isnot supported, because there is no standard way toserialize access to a single file across multiple processes in Python. If youneed to log to a single file from multiple processes, one way of doing this isto have all the processes log to aSocketHandler
, and have aseparate process which implements a socket server which reads from the socketand logs to file. (If you prefer, you can dedicate one thread in one of theexisting processes to perform this function.)This section documents this approach in more detail andincludes a working socket receiver which can be used as a starting point for youto adapt in your own applications.
You could also write your own handler which uses theLock
class from themultiprocessing
module to serialize access to thefile from your processes. The stdlibFileHandler
and subclasses donot make use ofmultiprocessing
.
Alternatively, you can use aQueue
and aQueueHandler
to sendall logging events to one of the processes in your multi-process application.The following example script demonstrates how you can do this; in the examplea separate listener process listens for events sent by other processes and logsthem according to its own logging configuration. Although the example onlydemonstrates one way of doing it (for example, you may want to use a listenerthread rather than a separate listener process – the implementation would beanalogous) it does allow for completely different logging configurations forthe listener and the other processes in your application, and can be used asthe basis for code meeting your own specific requirements:
# You'll need these imports in your own codeimportloggingimportlogging.handlersimportmultiprocessing# Next two import lines for this demo onlyfromrandomimportchoice,randomimporttime## Because you'll want to define the logging configurations for listener and workers, the# listener and worker process functions take a configurer parameter which is a callable# for configuring logging for that process. These functions are also passed the queue,# which they use for communication.## In practice, you can configure the listener however you want, but note that in this# simple example, the listener does not apply level or filter logic to received records.# In practice, you would probably want to do this logic in the worker processes, to avoid# sending events which would be filtered out between processes.## The size of the rotated files is made small so you can see the results easily.deflistener_configurer():root=logging.getLogger()h=logging.handlers.RotatingFileHandler('mptest.log','a',300,10)f=logging.Formatter('%(asctime)s%(processName)-10s%(name)s%(levelname)-8s%(message)s')h.setFormatter(f)root.addHandler(h)# This is the listener process top-level loop: wait for logging events# (LogRecords)on the queue and handle them, quit when you get a None for a# LogRecord.deflistener_process(queue,configurer):configurer()whileTrue:try:record=queue.get()ifrecordisNone:# We send this as a sentinel to tell the listener to quit.breaklogger=logging.getLogger(record.name)logger.handle(record)# No level or filter logic applied - just do it!exceptException:importsys,tracebackprint('Whoops! Problem:',file=sys.stderr)traceback.print_exc(file=sys.stderr)# Arrays used for random selections in this demoLEVELS=[logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL]LOGGERS=['a.b.c','d.e.f']MESSAGES=['Random message #1','Random message #2','Random message #3',]# The worker configuration is done at the start of the worker process run.# Note that on Windows you can't rely on fork semantics, so each process# will run the logging configuration code when it starts.defworker_configurer(queue):h=logging.handlers.QueueHandler(queue)# Just the one handler neededroot=logging.getLogger()root.addHandler(h)# send all messages, for demo; no other level or filter logic applied.root.setLevel(logging.DEBUG)# This is the worker process top-level loop, which just logs ten events with# random intervening delays before terminating.# The print messages are just so you know it's doing something!defworker_process(queue,configurer):configurer(queue)name=multiprocessing.current_process().nameprint('Worker started:%s'%name)foriinrange(10):time.sleep(random())logger=logging.getLogger(choice(LOGGERS))level=choice(LEVELS)message=choice(MESSAGES)logger.log(level,message)print('Worker finished:%s'%name)# Here's where the demo gets orchestrated. Create the queue, create and start# the listener, create ten workers and start them, wait for them to finish,# then send a None to the queue to tell the listener to finish.defmain():queue=multiprocessing.Queue(-1)listener=multiprocessing.Process(target=listener_process,args=(queue,listener_configurer))listener.start()workers=[]foriinrange(10):worker=multiprocessing.Process(target=worker_process,args=(queue,worker_configurer))workers.append(worker)worker.start()forwinworkers:w.join()queue.put_nowait(None)listener.join()if__name__=='__main__':main()
A variant of the above script keeps the logging in the main process, in aseparate thread:
importloggingimportlogging.configimportlogging.handlersfrommultiprocessingimportProcess,Queueimportrandomimportthreadingimporttimedeflogger_thread(q):whileTrue:record=q.get()ifrecordisNone:breaklogger=logging.getLogger(record.name)logger.handle(record)defworker_process(q):qh=logging.handlers.QueueHandler(q)root=logging.getLogger()root.setLevel(logging.DEBUG)root.addHandler(qh)levels=[logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL]loggers=['foo','foo.bar','foo.bar.baz','spam','spam.ham','spam.ham.eggs']foriinrange(100):lvl=random.choice(levels)logger=logging.getLogger(random.choice(loggers))logger.log(lvl,'Message no.%d',i)if__name__=='__main__':q=Queue()d={'version':1,'formatters':{'detailed':{'class':'logging.Formatter','format':'%(asctime)s%(name)-15s%(levelname)-8s%(processName)-10s%(message)s'}},'handlers':{'console':{'class':'logging.StreamHandler','level':'INFO',},'file':{'class':'logging.FileHandler','filename':'mplog.log','mode':'w','formatter':'detailed',},'foofile':{'class':'logging.FileHandler','filename':'mplog-foo.log','mode':'w','formatter':'detailed',},'errors':{'class':'logging.FileHandler','filename':'mplog-errors.log','mode':'w','level':'ERROR','formatter':'detailed',},},'loggers':{'foo':{'handlers':['foofile']}},'root':{'level':'DEBUG','handlers':['console','file','errors']},}workers=[]foriinrange(5):wp=Process(target=worker_process,name='worker%d'%(i+1),args=(q,))workers.append(wp)wp.start()logging.config.dictConfig(d)lp=threading.Thread(target=logger_thread,args=(q,))lp.start()# At this point, the main process could do some useful work of its own# Once it's done that, it can wait for the workers to terminate...forwpinworkers:wp.join()# And now tell the logging thread to finish up, tooq.put(None)lp.join()
This variant shows how you can e.g. apply configuration for particular loggers- e.g. thefoo
logger has a special handler which stores all events in thefoo
subsystem in a filemplog-foo.log
. This will be used by the loggingmachinery in the main process (even though the logging events are generated inthe worker processes) to direct the messages to the appropriate destinations.
Using concurrent.futures.ProcessPoolExecutor¶
If you want to useconcurrent.futures.ProcessPoolExecutor
to startyour worker processes, you need to create the queue slightly differently.Instead of
queue=multiprocessing.Queue(-1)
you should use
queue=multiprocessing.Manager().Queue(-1)# also works with the examples above
and you can then replace the worker creation from this:
workers=[]foriinrange(10):worker=multiprocessing.Process(target=worker_process,args=(queue,worker_configurer))workers.append(worker)worker.start()forwinworkers:w.join()
to this (remembering to first importconcurrent.futures
):
withconcurrent.futures.ProcessPoolExecutor(max_workers=10)asexecutor:foriinrange(10):executor.submit(worker_process,queue,worker_configurer)
Deploying Web applications using Gunicorn and uWSGI¶
When deploying Web applications usingGunicorn oruWSGI (or similar), multiple workerprocesses are created to handle client requests. In such environments, avoid creatingfile-based handlers directly in your web application. Instead, use aSocketHandler
to log from the web application to a listener in a separateprocess. This can be set up using a process management tool such as Supervisor - seeRunning a logging socket listener in production for more details.
Using file rotation¶
Sometimes you want to let a log file grow to a certain size, then open a newfile and log to that. You may want to keep a certain number of these files, andwhen that many files have been created, rotate the files so that the number offiles and the size of the files both remain bounded. For this usage pattern, thelogging package provides aRotatingFileHandler
:
importglobimportloggingimportlogging.handlersLOG_FILENAME='logging_rotatingfile_example.out'# Set up a specific logger with our desired output levelmy_logger=logging.getLogger('MyLogger')my_logger.setLevel(logging.DEBUG)# Add the log message handler to the loggerhandler=logging.handlers.RotatingFileHandler(LOG_FILENAME,maxBytes=20,backupCount=5)my_logger.addHandler(handler)# Log some messagesforiinrange(20):my_logger.debug('i =%d'%i)# See what files are createdlogfiles=glob.glob('%s*'%LOG_FILENAME)forfilenameinlogfiles:print(filename)
The result should be 6 separate files, each with part of the log history for theapplication:
logging_rotatingfile_example.outlogging_rotatingfile_example.out.1logging_rotatingfile_example.out.2logging_rotatingfile_example.out.3logging_rotatingfile_example.out.4logging_rotatingfile_example.out.5
The most current file is alwayslogging_rotatingfile_example.out
,and each time it reaches the size limit it is renamed with the suffix.1
. Each of the existing backup files is renamed to increment the suffix(.1
becomes.2
, etc.) and the.6
file is erased.
Obviously this example sets the log length much too small as an extremeexample. You would want to setmaxBytes to an appropriate value.
Use of alternative formatting styles¶
When logging was added to the Python standard library, the only way offormatting messages with variable content was to use the %-formattingmethod. Since then, Python has gained two new formatting approaches:string.Template
(added in Python 2.4) andstr.format()
(added in Python 2.6).
Logging (as of 3.2) provides improved support for these two additionalformatting styles. TheFormatter
class been enhanced to take anadditional, optional keyword parameter namedstyle
. This defaults to'%'
, but other possible values are'{'
and'$'
, which correspondto the other two formatting styles. Backwards compatibility is maintained bydefault (as you would expect), but by explicitly specifying a style parameter,you get the ability to specify format strings which work withstr.format()
orstring.Template
. Here’s an example consolesession to show the possibilities:
>>>importlogging>>>root=logging.getLogger()>>>root.setLevel(logging.DEBUG)>>>handler=logging.StreamHandler()>>>bf=logging.Formatter('{asctime}{name}{levelname:8s}{message}',...style='{')>>>handler.setFormatter(bf)>>>root.addHandler(handler)>>>logger=logging.getLogger('foo.bar')>>>logger.debug('This is a DEBUG message')2010-10-28 15:11:55,341 foo.bar DEBUG This is a DEBUG message>>>logger.critical('This is a CRITICAL message')2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message>>>df=logging.Formatter('$asctime $name ${levelname} $message',...style='$')>>>handler.setFormatter(df)>>>logger.debug('This is a DEBUG message')2010-10-28 15:13:06,924 foo.bar DEBUG This is a DEBUG message>>>logger.critical('This is a CRITICAL message')2010-10-28 15:13:11,494 foo.bar CRITICAL This is a CRITICAL message>>>
Note that the formatting of logging messages for final output to logs iscompletely independent of how an individual logging message is constructed.That can still use %-formatting, as shown here:
>>>logger.error('This is an%s%s%s','other,','ERROR,','message')2010-10-28 15:19:29,833 foo.bar ERROR This is another, ERROR, message>>>
Logging calls (logger.debug()
,logger.info()
etc.) only takepositional parameters for the actual logging message itself, with keywordparameters used only for determining options for how to handle the actuallogging call (e.g. theexc_info
keyword parameter to indicate thattraceback information should be logged, or theextra
keyword parameterto indicate additional contextual information to be added to the log). Soyou cannot directly make logging calls usingstr.format()
orstring.Template
syntax, because internally the logging packageuses %-formatting to merge the format string and the variable arguments.There would be no changing this while preserving backward compatibility, sinceall logging calls which are out there in existing code will be using %-formatstrings.
There is, however, a way that you can use {}- and $- formatting to constructyour individual log messages. Recall that for a message you can use anarbitrary object as a message format string, and that the logging package willcallstr()
on that object to get the actual format string. Consider thefollowing two classes:
classBraceMessage:def__init__(self,fmt,/,*args,**kwargs):self.fmt=fmtself.args=argsself.kwargs=kwargsdef__str__(self):returnself.fmt.format(*self.args,**self.kwargs)classDollarMessage:def__init__(self,fmt,/,**kwargs):self.fmt=fmtself.kwargs=kwargsdef__str__(self):fromstringimportTemplatereturnTemplate(self.fmt).substitute(**self.kwargs)
Either of these can be used in place of a format string, to allow {}- or$-formatting to be used to build the actual “message” part which appears in theformatted log output in place of “%(message)s” or “{message}” or “$message”.It’s a little unwieldy to use the class names whenever you want to logsomething, but it’s quite palatable if you use an alias such as __ (doubleunderscore — not to be confused with _, the single underscore used as asynonym/alias forgettext.gettext()
or its brethren).
The above classes are not included in Python, though they’re easy enough tocopy and paste into your own code. They can be used as follows (assuming thatthey’re declared in a module calledwherever
):
>>>fromwhereverimportBraceMessageas__>>>print(__('Message with{0}{name}',2,name='placeholders'))Message with 2 placeholders>>>classPoint:pass...>>>p=Point()>>>p.x=0.5>>>p.y=0.5>>>print(__('Message with coordinates: ({point.x:.2f},{point.y:.2f})',...point=p))Message with coordinates: (0.50, 0.50)>>>fromwhereverimportDollarMessageas__>>>print(__('Message with $num $what',num=2,what='placeholders'))Message with 2 placeholders>>>
While the above examples useprint()
to show how the formatting works, youwould of course uselogger.debug()
or similar to actually log using thisapproach.
One thing to note is that you pay no significant performance penalty with thisapproach: the actual formatting happens not when you make the logging call, butwhen (and if) the logged message is actually about to be output to a log by ahandler. So the only slightly unusual thing which might trip you up is that theparentheses go around the format string and the arguments, not just the formatstring. That’s because the __ notation is just syntax sugar for a constructorcall to one of theXXXMessage
classes.
If you prefer, you can use aLoggerAdapter
to achieve a similar effectto the above, as in the following example:
importloggingclassMessage:def__init__(self,fmt,args):self.fmt=fmtself.args=argsdef__str__(self):returnself.fmt.format(*self.args)classStyleAdapter(logging.LoggerAdapter):deflog(self,level,msg,/,*args,stacklevel=1,**kwargs):ifself.isEnabledFor(level):msg,kwargs=self.process(msg,kwargs)self.logger.log(level,Message(msg,args),**kwargs,stacklevel=stacklevel+1)logger=StyleAdapter(logging.getLogger(__name__))defmain():logger.debug('Hello,{}','world!')if__name__=='__main__':logging.basicConfig(level=logging.DEBUG)main()
The above script should log the messageHello,world!
when run withPython 3.8 or later.
CustomizingLogRecord
¶
Every logging event is represented by aLogRecord
instance.When an event is logged and not filtered out by a logger’s level, aLogRecord
is created, populated with information about the event andthen passed to the handlers for that logger (and its ancestors, up to andincluding the logger where further propagation up the hierarchy is disabled).Before Python 3.2, there were only two places where this creation was done:
Logger.makeRecord()
, which is called in the normal process oflogging an event. This invokedLogRecord
directly to create aninstance.makeLogRecord()
, which is called with a dictionary containingattributes to be added to the LogRecord. This is typically invoked when asuitable dictionary has been received over the network (e.g. in pickle formvia aSocketHandler
, or in JSON form via anHTTPHandler
).
This has usually meant that if you need to do anything special with aLogRecord
, you’ve had to do one of the following.
Create your own
Logger
subclass, which overridesLogger.makeRecord()
, and set it usingsetLoggerClass()
before any loggers that you care about are instantiated.Add a
Filter
to a logger or handler, which does thenecessary special manipulation you need when itsfilter()
method is called.
The first approach would be a little unwieldy in the scenario where (say)several different libraries wanted to do different things. Each would attemptto set its ownLogger
subclass, and the one which did this last wouldwin.
The second approach works reasonably well for many cases, but does not allowyou to e.g. use a specialized subclass ofLogRecord
. Librarydevelopers can set a suitable filter on their loggers, but they would have toremember to do this every time they introduced a new logger (which they woulddo simply by adding new packages or modules and doing
logger=logging.getLogger(__name__)
at module level). It’s probably one too many things to think about. Developerscould also add the filter to aNullHandler
attached to theirtop-level logger, but this would not be invoked if an application developerattached a handler to a lower-level library logger — so output from thathandler would not reflect the intentions of the library developer.
In Python 3.2 and later,LogRecord
creation is done through afactory, which you can specify. The factory is just a callable you can set withsetLogRecordFactory()
, and interrogate withgetLogRecordFactory()
. The factory is invoked with the samesignature as theLogRecord
constructor, asLogRecord
is the default setting for the factory.
This approach allows a custom factory to control all aspects of LogRecordcreation. For example, you could return a subclass, or just add some additionalattributes to the record once created, using a pattern similar to this:
old_factory=logging.getLogRecordFactory()defrecord_factory(*args,**kwargs):record=old_factory(*args,**kwargs)record.custom_attribute=0xdecafbadreturnrecordlogging.setLogRecordFactory(record_factory)
This pattern allows different libraries to chain factories together, and aslong as they don’t overwrite each other’s attributes or unintentionallyoverwrite the attributes provided as standard, there should be no surprises.However, it should be borne in mind that each link in the chain adds run-timeoverhead to all logging operations, and the technique should only be used whenthe use of aFilter
does not provide the desired result.
Subclassing QueueHandler and QueueListener- a ZeroMQ example¶
SubclassQueueHandler
¶
You can use aQueueHandler
subclass to send messages to other kindsof queues, for example a ZeroMQ ‘publish’ socket. In the example below,thesocket is created separately and passed to the handler (as its ‘queue’):
importzmq# using pyzmq, the Python binding for ZeroMQimportjson# for serializing records portablyctx=zmq.Context()sock=zmq.Socket(ctx,zmq.PUB)# or zmq.PUSH, or other suitable valuesock.bind('tcp://*:5556')# or whereverclassZeroMQSocketHandler(QueueHandler):defenqueue(self,record):self.queue.send_json(record.__dict__)handler=ZeroMQSocketHandler(sock)
Of course there are other ways of organizing this, for example passing in thedata needed by the handler to create the socket:
classZeroMQSocketHandler(QueueHandler):def__init__(self,uri,socktype=zmq.PUB,ctx=None):self.ctx=ctxorzmq.Context()socket=zmq.Socket(self.ctx,socktype)socket.bind(uri)super().__init__(socket)defenqueue(self,record):self.queue.send_json(record.__dict__)defclose(self):self.queue.close()
SubclassQueueListener
¶
You can also subclassQueueListener
to get messages from other kindsof queues, for example a ZeroMQ ‘subscribe’ socket. Here’s an example:
classZeroMQSocketListener(QueueListener):def__init__(self,uri,/,*handlers,**kwargs):self.ctx=kwargs.get('ctx')orzmq.Context()socket=zmq.Socket(self.ctx,zmq.SUB)socket.setsockopt_string(zmq.SUBSCRIBE,'')# subscribe to everythingsocket.connect(uri)super().__init__(socket,*handlers,**kwargs)defdequeue(self):msg=self.queue.recv_json()returnlogging.makeLogRecord(msg)
Subclassing QueueHandler and QueueListener- apynng
example¶
In a similar way to the above section, we can implement a listener and handlerusingpynng, which is a Python binding toNNG, billed as a spiritual successor to ZeroMQ.The following snippets illustrate – you can test them in an environment which haspynng
installed. Just for variety, we present the listener first.
SubclassQueueListener
¶
# listener.pyimportjsonimportloggingimportlogging.handlersimportpynngDEFAULT_ADDR="tcp://localhost:13232"interrupted=FalseclassNNGSocketListener(logging.handlers.QueueListener):def__init__(self,uri,/,*handlers,**kwargs):# Have a timeout for interruptability, and open a# subscriber socketsocket=pynng.Sub0(listen=uri,recv_timeout=500)# The b'' subscription matches all topicstopics=kwargs.pop('topics',None)orb''socket.subscribe(topics)# We treat the socket as a queuesuper().__init__(socket,*handlers,**kwargs)defdequeue(self,block):data=None# Keep looping while not interrupted and no data received over the# socketwhilenotinterrupted:try:data=self.queue.recv(block=block)breakexceptpynng.Timeout:passexceptpynng.Closed:# sometimes happens when you hit Ctrl-CbreakifdataisNone:returnNone# Get the logging event sent from a publisherevent=json.loads(data.decode('utf-8'))returnlogging.makeLogRecord(event)defenqueue_sentinel(self):# Not used in this implementation, as the socket isn't really a# queuepasslogging.getLogger('pynng').propagate=Falselistener=NNGSocketListener(DEFAULT_ADDR,logging.StreamHandler(),topics=b'')listener.start()print('Press Ctrl-C to stop.')try:whileTrue:passexceptKeyboardInterrupt:interrupted=Truefinally:listener.stop()
SubclassQueueHandler
¶
# sender.pyimportjsonimportloggingimportlogging.handlersimporttimeimportrandomimportpynngDEFAULT_ADDR="tcp://localhost:13232"classNNGSocketHandler(logging.handlers.QueueHandler):def__init__(self,uri):socket=pynng.Pub0(dial=uri,send_timeout=500)super().__init__(socket)defenqueue(self,record):# Send the record as UTF-8 encoded JSONd=dict(record.__dict__)data=json.dumps(d)self.queue.send(data.encode('utf-8'))defclose(self):self.queue.close()logging.getLogger('pynng').propagate=Falsehandler=NNGSocketHandler(DEFAULT_ADDR)# Make sure the process ID is in the outputlogging.basicConfig(level=logging.DEBUG,handlers=[logging.StreamHandler(),handler],format='%(levelname)-8s%(name)10s%(process)6s%(message)s')levels=(logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL)logger_names=('myapp','myapp.lib1','myapp.lib2')msgno=1whileTrue:# Just randomly select some loggers and levels and log awaylevel=random.choice(levels)logger=logging.getLogger(random.choice(logger_names))logger.log(level,'Message no.%5d'%msgno)msgno+=1delay=random.random()*2+0.5time.sleep(delay)
You can run the above two snippets in separate command shells. If we run thelistener in one shell and run the sender in two separate shells, we should seesomething like the following. In the first sender shell:
$pythonsender.pyDEBUG myapp 613 Message no. 1WARNING myapp.lib2 613 Message no. 2CRITICAL myapp.lib2 613 Message no. 3WARNING myapp.lib2 613 Message no. 4CRITICAL myapp.lib1 613 Message no. 5DEBUG myapp 613 Message no. 6CRITICAL myapp.lib1 613 Message no. 7INFO myapp.lib1 613 Message no. 8(and so on)
In the second sender shell:
$pythonsender.pyINFO myapp.lib2 657 Message no. 1CRITICAL myapp.lib2 657 Message no. 2CRITICAL myapp 657 Message no. 3CRITICAL myapp.lib1 657 Message no. 4INFO myapp.lib1 657 Message no. 5WARNING myapp.lib2 657 Message no. 6CRITICAL myapp 657 Message no. 7DEBUG myapp.lib1 657 Message no. 8(and so on)
In the listener shell:
$pythonlistener.pyPress Ctrl-C to stop.DEBUG myapp 613 Message no. 1WARNING myapp.lib2 613 Message no. 2INFO myapp.lib2 657 Message no. 1CRITICAL myapp.lib2 613 Message no. 3CRITICAL myapp.lib2 657 Message no. 2CRITICAL myapp 657 Message no. 3WARNING myapp.lib2 613 Message no. 4CRITICAL myapp.lib1 613 Message no. 5CRITICAL myapp.lib1 657 Message no. 4INFO myapp.lib1 657 Message no. 5DEBUG myapp 613 Message no. 6WARNING myapp.lib2 657 Message no. 6CRITICAL myapp 657 Message no. 7CRITICAL myapp.lib1 613 Message no. 7INFO myapp.lib1 613 Message no. 8DEBUG myapp.lib1 657 Message no. 8(and so on)
As you can see, the logging from the two sender processes is interleaved in thelistener’s output.
An example dictionary-based configuration¶
Below is an example of a logging configuration dictionary - it’s taken fromthedocumentation on the Django project.This dictionary is passed todictConfig()
to put the configuration into effect:
LOGGING={'version':1,'disable_existing_loggers':False,'formatters':{'verbose':{'format':'{levelname}{asctime}{module}{process:d}{thread:d}{message}','style':'{',},'simple':{'format':'{levelname}{message}','style':'{',},},'filters':{'special':{'()':'project.logging.SpecialFilter','foo':'bar',},},'handlers':{'console':{'level':'INFO','class':'logging.StreamHandler','formatter':'simple',},'mail_admins':{'level':'ERROR','class':'django.utils.log.AdminEmailHandler','filters':['special']}},'loggers':{'django':{'handlers':['console'],'propagate':True,},'django.request':{'handlers':['mail_admins'],'level':'ERROR','propagate':False,},'myproject.custom':{'handlers':['console','mail_admins'],'level':'INFO','filters':['special']}}}
For more information about this configuration, you can see therelevantsectionof the Django documentation.
Using a rotator and namer to customize log rotation processing¶
An example of how you can define a namer and rotator is given in the followingrunnable script, which shows gzip compression of the log file:
importgzipimportloggingimportlogging.handlersimportosimportshutildefnamer(name):returnname+".gz"defrotator(source,dest):withopen(source,'rb')asf_in:withgzip.open(dest,'wb')asf_out:shutil.copyfileobj(f_in,f_out)os.remove(source)rh=logging.handlers.RotatingFileHandler('rotated.log',maxBytes=128,backupCount=5)rh.rotator=rotatorrh.namer=namerroot=logging.getLogger()root.setLevel(logging.INFO)root.addHandler(rh)f=logging.Formatter('%(asctime)s%(message)s')rh.setFormatter(f)foriinrange(1000):root.info(f'Message no.{i+1}')
After running this, you will see six new files, five of which are compressed:
$lsrotated.log*rotated.log rotated.log.2.gz rotated.log.4.gzrotated.log.1.gz rotated.log.3.gz rotated.log.5.gz$zcatrotated.log.1.gz2023-01-20 02:28:17,767 Message no. 9962023-01-20 02:28:17,767 Message no. 9972023-01-20 02:28:17,767 Message no. 998
A more elaborate multiprocessing example¶
The following working example shows how logging can be used with multiprocessingusing configuration files. The configurations are fairly simple, but serve toillustrate how more complex ones could be implemented in a real multiprocessingscenario.
In the example, the main process spawns a listener process and some workerprocesses. Each of the main process, the listener and the workers have threeseparate configurations (the workers all share the same configuration). We cansee logging in the main process, how the workers log to a QueueHandler and howthe listener implements a QueueListener and a more complex loggingconfiguration, and arranges to dispatch events received via the queue to thehandlers specified in the configuration. Note that these configurations arepurely illustrative, but you should be able to adapt this example to your ownscenario.
Here’s the script - the docstrings and the comments hopefully explain how itworks:
importloggingimportlogging.configimportlogging.handlersfrommultiprocessingimportProcess,Queue,Event,current_processimportosimportrandomimporttimeclassMyHandler:""" A simple handler for logging events. It runs in the listener process and dispatches events to loggers based on the name in the received record, which then get dispatched, by the logging system, to the handlers configured for those loggers. """defhandle(self,record):ifrecord.name=="root":logger=logging.getLogger()else:logger=logging.getLogger(record.name)iflogger.isEnabledFor(record.levelno):# The process name is transformed just to show that it's the listener# doing the logging to files and consolerecord.processName='%s (for%s)'%(current_process().name,record.processName)logger.handle(record)deflistener_process(q,stop_event,config):""" This could be done in the main process, but is just done in a separate process for illustrative purposes. This initialises logging according to the specified configuration, starts the listener and waits for the main process to signal completion via the event. The listener is then stopped, and the process exits. """logging.config.dictConfig(config)listener=logging.handlers.QueueListener(q,MyHandler())listener.start()ifos.name=='posix':# On POSIX, the setup logger will have been configured in the# parent process, but should have been disabled following the# dictConfig call.# On Windows, since fork isn't used, the setup logger won't# exist in the child, so it would be created and the message# would appear - hence the "if posix" clause.logger=logging.getLogger('setup')logger.critical('Should not appear, because of disabled logger ...')stop_event.wait()listener.stop()defworker_process(config):""" A number of these are spawned for the purpose of illustration. In practice, they could be a heterogeneous bunch of processes rather than ones which are identical to each other. This initialises logging according to the specified configuration, and logs a hundred messages with random levels to randomly selected loggers. A small sleep is added to allow other processes a chance to run. This is not strictly needed, but it mixes the output from the different processes a bit more than if it's left out. """logging.config.dictConfig(config)levels=[logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL]loggers=['foo','foo.bar','foo.bar.baz','spam','spam.ham','spam.ham.eggs']ifos.name=='posix':# On POSIX, the setup logger will have been configured in the# parent process, but should have been disabled following the# dictConfig call.# On Windows, since fork isn't used, the setup logger won't# exist in the child, so it would be created and the message# would appear - hence the "if posix" clause.logger=logging.getLogger('setup')logger.critical('Should not appear, because of disabled logger ...')foriinrange(100):lvl=random.choice(levels)logger=logging.getLogger(random.choice(loggers))logger.log(lvl,'Message no.%d',i)time.sleep(0.01)defmain():q=Queue()# The main process gets a simple configuration which prints to the console.config_initial={'version':1,'handlers':{'console':{'class':'logging.StreamHandler','level':'INFO'}},'root':{'handlers':['console'],'level':'DEBUG'}}# The worker process configuration is just a QueueHandler attached to the# root logger, which allows all messages to be sent to the queue.# We disable existing loggers to disable the "setup" logger used in the# parent process. This is needed on POSIX because the logger will# be there in the child following a fork().config_worker={'version':1,'disable_existing_loggers':True,'handlers':{'queue':{'class':'logging.handlers.QueueHandler','queue':q}},'root':{'handlers':['queue'],'level':'DEBUG'}}# The listener process configuration shows that the full flexibility of# logging configuration is available to dispatch events to handlers however# you want.# We disable existing loggers to disable the "setup" logger used in the# parent process. This is needed on POSIX because the logger will# be there in the child following a fork().config_listener={'version':1,'disable_existing_loggers':True,'formatters':{'detailed':{'class':'logging.Formatter','format':'%(asctime)s%(name)-15s%(levelname)-8s%(processName)-10s%(message)s'},'simple':{'class':'logging.Formatter','format':'%(name)-15s%(levelname)-8s%(processName)-10s%(message)s'}},'handlers':{'console':{'class':'logging.StreamHandler','formatter':'simple','level':'INFO'},'file':{'class':'logging.FileHandler','filename':'mplog.log','mode':'w','formatter':'detailed'},'foofile':{'class':'logging.FileHandler','filename':'mplog-foo.log','mode':'w','formatter':'detailed'},'errors':{'class':'logging.FileHandler','filename':'mplog-errors.log','mode':'w','formatter':'detailed','level':'ERROR'}},'loggers':{'foo':{'handlers':['foofile']}},'root':{'handlers':['console','file','errors'],'level':'DEBUG'}}# Log some initial events, just to show that logging in the parent works# normally.logging.config.dictConfig(config_initial)logger=logging.getLogger('setup')logger.info('About to create workers ...')workers=[]foriinrange(5):wp=Process(target=worker_process,name='worker%d'%(i+1),args=(config_worker,))workers.append(wp)wp.start()logger.info('Started worker:%s',wp.name)logger.info('About to create listener ...')stop_event=Event()lp=Process(target=listener_process,name='listener',args=(q,stop_event,config_listener))lp.start()logger.info('Started listener')# We now hang around for the workers to finish their work.forwpinworkers:wp.join()# Workers all done, listening can now stop.# Logging in the parent still works normally.logger.info('Telling listener to stop ...')stop_event.set()lp.join()logger.info('All done.')if__name__=='__main__':main()
Inserting a BOM into messages sent to a SysLogHandler¶
RFC 5424 requires that aUnicode message be sent to a syslog daemon as a set of bytes which have thefollowing structure: an optional pure-ASCII component, followed by a UTF-8 ByteOrder Mark (BOM), followed by Unicode encoded using UTF-8. (See therelevant section of the specification.)
In Python 3.1, code was added toSysLogHandler
to insert a BOM into the message, butunfortunately, it was implemented incorrectly, with the BOM appearing at thebeginning of the message and hence not allowing any pure-ASCII component toappear before it.
As this behaviour is broken, the incorrect BOM insertion code is being removedfrom Python 3.2.4 and later. However, it is not being replaced, and if youwant to produceRFC 5424-compliant messages which include a BOM, an optionalpure-ASCII sequence before it and arbitrary Unicode after it, encoded usingUTF-8, then you need to do the following:
Attach a
Formatter
instance to yourSysLogHandler
instance, with a format stringsuch as:'ASCII section\ufeffUnicode section'
The Unicode code point U+FEFF, when encoded using UTF-8, will beencoded as a UTF-8 BOM – the byte-string
b'\xef\xbb\xbf'
.Replace the ASCII section with whatever placeholders you like, but make surethat the data that appears in there after substitution is always ASCII (thatway, it will remain unchanged after UTF-8 encoding).
Replace the Unicode section with whatever placeholders you like; if the datawhich appears there after substitution contains characters outside the ASCIIrange, that’s fine – it will be encoded using UTF-8.
The formatted messagewill be encoded using UTF-8 encoding bySysLogHandler
. If you follow the above rules, you should be able to produceRFC 5424-compliant messages. If you don’t, logging may not complain, but yourmessages will not be RFC 5424-compliant, and your syslog daemon may complain.
Implementing structured logging¶
Although most logging messages are intended for reading by humans, and thus notreadily machine-parseable, there might be circumstances where you want to outputmessages in a structured format whichis capable of being parsed by a program(without needing complex regular expressions to parse the log message). This isstraightforward to achieve using the logging package. There are a number ofways in which this could be achieved, but the following is a simple approachwhich uses JSON to serialise the event in a machine-parseable manner:
importjsonimportloggingclassStructuredMessage:def__init__(self,message,/,**kwargs):self.message=messageself.kwargs=kwargsdef__str__(self):return'%s >>>%s'%(self.message,json.dumps(self.kwargs))_=StructuredMessage# optional, to improve readabilitylogging.basicConfig(level=logging.INFO,format='%(message)s')logging.info(_('message 1',foo='bar',bar='baz',num=123,fnum=123.456))
If the above script is run, it prints:
message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"}
Note that the order of items might be different according to the version ofPython used.
If you need more specialised processing, you can use a custom JSON encoder,as in the following complete example:
importjsonimportloggingclassEncoder(json.JSONEncoder):defdefault(self,o):ifisinstance(o,set):returntuple(o)elifisinstance(o,str):returno.encode('unicode_escape').decode('ascii')returnsuper().default(o)classStructuredMessage:def__init__(self,message,/,**kwargs):self.message=messageself.kwargs=kwargsdef__str__(self):s=Encoder().encode(self.kwargs)return'%s >>>%s'%(self.message,s)_=StructuredMessage# optional, to improve readabilitydefmain():logging.basicConfig(level=logging.INFO,format='%(message)s')logging.info(_('message 1',set_value={1,2,3},snowman='\u2603'))if__name__=='__main__':main()
When the above script is run, it prints:
message 1 >>> {"snowman": "\u2603", "set_value": [1, 2, 3]}
Note that the order of items might be different according to the version ofPython used.
Customizing handlers withdictConfig()
¶
There are times when you want to customize logging handlers in particular ways,and if you usedictConfig()
you may be able to do this withoutsubclassing. As an example, consider that you may want to set the ownership of alog file. On POSIX, this is easily done usingshutil.chown()
, but the filehandlers in the stdlib don’t offer built-in support. You can customize handlercreation using a plain function such as:
defowned_file_handler(filename,mode='a',encoding=None,owner=None):ifowner:ifnotos.path.exists(filename):open(filename,'a').close()shutil.chown(filename,*owner)returnlogging.FileHandler(filename,mode,encoding)
You can then specify, in a logging configuration passed todictConfig()
,that a logging handler be created by calling this function:
LOGGING={'version':1,'disable_existing_loggers':False,'formatters':{'default':{'format':'%(asctime)s%(levelname)s%(name)s%(message)s'},},'handlers':{'file':{# The values below are popped from this dictionary and# used to create the handler, set the handler's level and# its formatter.'()':owned_file_handler,'level':'DEBUG','formatter':'default',# The values below are passed to the handler creator callable# as keyword arguments.'owner':['pulse','pulse'],'filename':'chowntest.log','mode':'w','encoding':'utf-8',},},'root':{'handlers':['file'],'level':'DEBUG',},}
In this example I am setting the ownership using thepulse
user and group,just for the purposes of illustration. Putting it together into a workingscript,chowntest.py
:
importlogging,logging.config,os,shutildefowned_file_handler(filename,mode='a',encoding=None,owner=None):ifowner:ifnotos.path.exists(filename):open(filename,'a').close()shutil.chown(filename,*owner)returnlogging.FileHandler(filename,mode,encoding)LOGGING={'version':1,'disable_existing_loggers':False,'formatters':{'default':{'format':'%(asctime)s%(levelname)s%(name)s%(message)s'},},'handlers':{'file':{# The values below are popped from this dictionary and# used to create the handler, set the handler's level and# its formatter.'()':owned_file_handler,'level':'DEBUG','formatter':'default',# The values below are passed to the handler creator callable# as keyword arguments.'owner':['pulse','pulse'],'filename':'chowntest.log','mode':'w','encoding':'utf-8',},},'root':{'handlers':['file'],'level':'DEBUG',},}logging.config.dictConfig(LOGGING)logger=logging.getLogger('mylogger')logger.debug('A debug message')
To run this, you will probably need to run asroot
:
$sudopython3.3chowntest.py$catchowntest.log2013-11-05 09:34:51,128 DEBUG mylogger A debug message$ls-lchowntest.log-rw-r--r-- 1 pulse pulse 55 2013-11-05 09:34 chowntest.log
Note that this example uses Python 3.3 because that’s whereshutil.chown()
makes an appearance. This approach should work with any Python version thatsupportsdictConfig()
- namely, Python 2.7, 3.2 or later. With pre-3.3versions, you would need to implement the actual ownership change using e.g.os.chown()
.
In practice, the handler-creating function may be in a utility module somewherein your project. Instead of the line in the configuration:
'()':owned_file_handler,
you could use e.g.:
'()':'ext://project.util.owned_file_handler',
whereproject.util
can be replaced with the actual name of the packagewhere the function resides. In the above working script, using'ext://__main__.owned_file_handler'
should work. Here, the actual callableis resolved bydictConfig()
from theext://
specification.
This example hopefully also points the way to how you could implement othertypes of file change - e.g. setting specific POSIX permission bits - in thesame way, usingos.chmod()
.
Of course, the approach could also be extended to types of handler other than aFileHandler
- for example, one of the rotating file handlers,or a different type of handler altogether.
Using particular formatting styles throughout your application¶
In Python 3.2, theFormatter
gained astyle
keywordparameter which, while defaulting to%
for backward compatibility, allowedthe specification of{
or$
to support the formatting approachessupported bystr.format()
andstring.Template
. Note that thisgoverns the formatting of logging messages for final output to logs, and iscompletely orthogonal to how an individual logging message is constructed.
Logging calls (debug()
,info()
etc.) only takepositional parameters for the actual logging message itself, with keywordparameters used only for determining options for how to handle the logging call(e.g. theexc_info
keyword parameter to indicate that traceback informationshould be logged, or theextra
keyword parameter to indicate additionalcontextual information to be added to the log). So you cannot directly makelogging calls usingstr.format()
orstring.Template
syntax,because internally the logging package uses %-formatting to merge the formatstring and the variable arguments. There would be no changing this while preservingbackward compatibility, since all logging calls which are out there in existingcode will be using %-format strings.
There have been suggestions to associate format styles with specific loggers,but that approach also runs into backward compatibility problems because anyexisting code could be using a given logger name and using %-formatting.
For logging to work interoperably between any third-party libraries and yourcode, decisions about formatting need to be made at the level of theindividual logging call. This opens up a couple of ways in which alternativeformatting styles can be accommodated.
Using LogRecord factories¶
In Python 3.2, along with theFormatter
changes mentionedabove, the logging package gained the ability to allow users to set their ownLogRecord
subclasses, using thesetLogRecordFactory()
function.You can use this to set your own subclass ofLogRecord
, which does theRight Thing by overriding thegetMessage()
method. The baseclass implementation of this method is where themsg%args
formattinghappens, and where you can substitute your alternate formatting; however, youshould be careful to support all formatting styles and allow %-formatting asthe default, to ensure interoperability with other code. Care should also betaken to callstr(self.msg)
, just as the base implementation does.
Refer to the reference documentation onsetLogRecordFactory()
andLogRecord
for more information.
Using custom message objects¶
There is another, perhaps simpler way that you can use {}- and $- formatting toconstruct your individual log messages. You may recall (fromUsing arbitrary objects as messages) that when logging you can use an arbitraryobject as a message format string, and that the logging package will callstr()
on that object to get the actual format string. Consider thefollowing two classes:
classBraceMessage:def__init__(self,fmt,/,*args,**kwargs):self.fmt=fmtself.args=argsself.kwargs=kwargsdef__str__(self):returnself.fmt.format(*self.args,**self.kwargs)classDollarMessage:def__init__(self,fmt,/,**kwargs):self.fmt=fmtself.kwargs=kwargsdef__str__(self):fromstringimportTemplatereturnTemplate(self.fmt).substitute(**self.kwargs)
Either of these can be used in place of a format string, to allow {}- or$-formatting to be used to build the actual “message” part which appears in theformatted log output in place of “%(message)s” or “{message}” or “$message”.If you find it a little unwieldy to use the class names whenever you want to logsomething, you can make it more palatable if you use an alias such asM
or_
for the message (or perhaps__
, if you are using_
forlocalization).
Examples of this approach are given below. Firstly, formatting withstr.format()
:
>>>__=BraceMessage>>>print(__('Message with{0}{1}',2,'placeholders'))Message with 2 placeholders>>>classPoint:pass...>>>p=Point()>>>p.x=0.5>>>p.y=0.5>>>print(__('Message with coordinates: ({point.x:.2f},{point.y:.2f})',point=p))Message with coordinates: (0.50, 0.50)
Secondly, formatting withstring.Template
:
>>>__=DollarMessage>>>print(__('Message with $num $what',num=2,what='placeholders'))Message with 2 placeholders>>>
One thing to note is that you pay no significant performance penalty with thisapproach: the actual formatting happens not when you make the logging call, butwhen (and if) the logged message is actually about to be output to a log by ahandler. So the only slightly unusual thing which might trip you up is that theparentheses go around the format string and the arguments, not just the formatstring. That’s because the __ notation is just syntax sugar for a constructorcall to one of theXXXMessage
classes shown above.
Configuring filters withdictConfig()
¶
Youcan configure filters usingdictConfig()
, though itmight not be obvious at first glance how to do it (hence this recipe). SinceFilter
is the only filter class included in the standardlibrary, and it is unlikely to cater to many requirements (it’s only there as abase class), you will typically need to define your ownFilter
subclass with an overriddenfilter()
method. To do this,specify the()
key in the configuration dictionary for the filter,specifying a callable which will be used to create the filter (a class is themost obvious, but you can provide any callable which returns aFilter
instance). Here is a complete example:
importloggingimportlogging.configimportsysclassMyFilter(logging.Filter):def__init__(self,param=None):self.param=paramdeffilter(self,record):ifself.paramisNone:allow=Trueelse:allow=self.paramnotinrecord.msgifallow:record.msg='changed: '+record.msgreturnallowLOGGING={'version':1,'filters':{'myfilter':{'()':MyFilter,'param':'noshow',}},'handlers':{'console':{'class':'logging.StreamHandler','filters':['myfilter']}},'root':{'level':'DEBUG','handlers':['console']},}if__name__=='__main__':logging.config.dictConfig(LOGGING)logging.debug('hello')logging.debug('hello - noshow')
This example shows how you can pass configuration data to the callable whichconstructs the instance, in the form of keyword parameters. When run, the abovescript will print:
changed: hello
which shows that the filter is working as configured.
A couple of extra points to note:
If you can’t refer to the callable directly in the configuration (e.g. if itlives in a different module, and you can’t import it directly where theconfiguration dictionary is), you can use the form
ext://...
as describedinAccess to external objects. For example, you could have usedthe text'ext://__main__.MyFilter'
instead ofMyFilter
in the aboveexample.As well as for filters, this technique can also be used to configure customhandlers and formatters. SeeUser-defined objects for moreinformation on how logging supports using user-defined objects in itsconfiguration, and see the other cookbook recipeCustomizing handlers with dictConfig() above.
Customized exception formatting¶
There might be times when you want to do customized exception formatting - forargument’s sake, let’s say you want exactly one line per logged event, evenwhen exception information is present. You can do this with a custom formatterclass, as shown in the following example:
importloggingclassOneLineExceptionFormatter(logging.Formatter):defformatException(self,exc_info):""" Format an exception so that it prints on a single line. """result=super().formatException(exc_info)returnrepr(result)# or format into one line however you want todefformat(self,record):s=super().format(record)ifrecord.exc_text:s=s.replace('\n','')+'|'returnsdefconfigure_logging():fh=logging.FileHandler('output.txt','w')f=OneLineExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|','%d/%m/%Y %H:%M:%S')fh.setFormatter(f)root=logging.getLogger()root.setLevel(logging.DEBUG)root.addHandler(fh)defmain():configure_logging()logging.info('Sample message')try:x=1/0exceptZeroDivisionErrorase:logging.exception('ZeroDivisionError:%s',e)if__name__=='__main__':main()
When run, this produces a file with exactly two lines:
28/01/2015 07:21:23|INFO|Sample message|28/01/2015 07:21:23|ERROR|ZeroDivisionError: integer division or modulo by zero|'Traceback (most recent call last):\n File "logtest7.py", line 30, in main\n x = 1 / 0\nZeroDivisionError: integer division or modulo by zero'|
While the above treatment is simplistic, it points the way to how exceptioninformation can be formatted to your liking. Thetraceback
module may behelpful for more specialized needs.
Speaking logging messages¶
There might be situations when it is desirable to have logging messages renderedin an audible rather than a visible format. This is easy to do if you havetext-to-speech (TTS) functionality available in your system, even if it doesn’t havea Python binding. Most TTS systems have a command line program you can run, andthis can be invoked from a handler usingsubprocess
. It’s assumed herethat TTS command line programs won’t expect to interact with users or take along time to complete, and that the frequency of logged messages will be not sohigh as to swamp the user with messages, and that it’s acceptable to have themessages spoken one at a time rather than concurrently, The example implementationbelow waits for one message to be spoken before the next is processed, and thismight cause other handlers to be kept waiting. Here is a short example showingthe approach, which assumes that theespeak
TTS package is available:
importloggingimportsubprocessimportsysclassTTSHandler(logging.Handler):defemit(self,record):msg=self.format(record)# Speak slowly in a female English voicecmd=['espeak','-s150','-ven+f3',msg]p=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)# wait for the program to finishp.communicate()defconfigure_logging():h=TTSHandler()root=logging.getLogger()root.addHandler(h)# the default formatter just returns the messageroot.setLevel(logging.DEBUG)defmain():logging.info('Hello')logging.debug('Goodbye')if__name__=='__main__':configure_logging()sys.exit(main())
When run, this script should say “Hello” and then “Goodbye” in a female voice.
The above approach can, of course, be adapted to other TTS systems and evenother systems altogether which can process messages via external programs runfrom a command line.
Buffering logging messages and outputting them conditionally¶
There might be situations where you want to log messages in a temporary areaand only output them if a certain condition occurs. For example, you may want tostart logging debug events in a function, and if the function completes withouterrors, you don’t want to clutter the log with the collected debug information,but if there is an error, you want all the debug information to be output as wellas the error.
Here is an example which shows how you could do this using a decorator for yourfunctions where you want logging to behave this way. It makes use of thelogging.handlers.MemoryHandler
, which allows buffering of logged eventsuntil some condition occurs, at which point the buffered events areflushed
- passed to another handler (thetarget
handler) for processing. By default,theMemoryHandler
flushed when its buffer gets filled up or an event whoselevel is greater than or equal to a specified threshold is seen. You can use thisrecipe with a more specialised subclass ofMemoryHandler
if you want customflushing behavior.
The example script has a simple function,foo
, which just cycles throughall the logging levels, writing tosys.stderr
to say what level it’s aboutto log at, and then actually logging a message at that level. You can pass aparameter tofoo
which, if true, will log at ERROR and CRITICAL levels -otherwise, it only logs at DEBUG, INFO and WARNING levels.
The script just arranges to decoratefoo
with a decorator which will do theconditional logging that’s required. The decorator takes a logger as a parameterand attaches a memory handler for the duration of the call to the decoratedfunction. The decorator can be additionally parameterised using a target handler,a level at which flushing should occur, and a capacity for the buffer (number ofrecords buffered). These default to aStreamHandler
whichwrites tosys.stderr
,logging.ERROR
and100
respectively.
Here’s the script:
importloggingfromlogging.handlersimportMemoryHandlerimportsyslogger=logging.getLogger(__name__)logger.addHandler(logging.NullHandler())deflog_if_errors(logger,target_handler=None,flush_level=None,capacity=None):iftarget_handlerisNone:target_handler=logging.StreamHandler()ifflush_levelisNone:flush_level=logging.ERRORifcapacityisNone:capacity=100handler=MemoryHandler(capacity,flushLevel=flush_level,target=target_handler)defdecorator(fn):defwrapper(*args,**kwargs):logger.addHandler(handler)try:returnfn(*args,**kwargs)exceptException:logger.exception('call failed')raisefinally:super(MemoryHandler,handler).flush()logger.removeHandler(handler)returnwrapperreturndecoratordefwrite_line(s):sys.stderr.write('%s\n'%s)deffoo(fail=False):write_line('about to log at DEBUG ...')logger.debug('Actually logged at DEBUG')write_line('about to log at INFO ...')logger.info('Actually logged at INFO')write_line('about to log at WARNING ...')logger.warning('Actually logged at WARNING')iffail:write_line('about to log at ERROR ...')logger.error('Actually logged at ERROR')write_line('about to log at CRITICAL ...')logger.critical('Actually logged at CRITICAL')returnfaildecorated_foo=log_if_errors(logger)(foo)if__name__=='__main__':logger.setLevel(logging.DEBUG)write_line('Calling undecorated foo with False')assertnotfoo(False)write_line('Calling undecorated foo with True')assertfoo(True)write_line('Calling decorated foo with False')assertnotdecorated_foo(False)write_line('Calling decorated foo with True')assertdecorated_foo(True)
When this script is run, the following output should be observed:
Calling undecorated foo with Falseabout to log at DEBUG ...about to log at INFO ...about to log at WARNING ...Calling undecorated foo with Trueabout to log at DEBUG ...about to log at INFO ...about to log at WARNING ...about to log at ERROR ...about to log at CRITICAL ...Calling decorated foo with Falseabout to log at DEBUG ...about to log at INFO ...about to log at WARNING ...Calling decorated foo with Trueabout to log at DEBUG ...about to log at INFO ...about to log at WARNING ...about to log at ERROR ...Actually logged at DEBUGActually logged at INFOActually logged at WARNINGActually logged at ERRORabout to log at CRITICAL ...Actually logged at CRITICAL
As you can see, actual logging output only occurs when an event is logged whoseseverity is ERROR or greater, but in that case, any previous events at lowerseverities are also logged.
You can of course use the conventional means of decoration:
@log_if_errors(logger)deffoo(fail=False):...
Sending logging messages to email, with buffering¶
To illustrate how you can send log messages via email, so that a set number ofmessages are sent per email, you can subclassBufferingHandler
. In the following example, which you canadapt to suit your specific needs, a simple test harness is provided which allows youto run the script with command line arguments specifying what you typically need tosend things via SMTP. (Run the downloaded script with the-h
argument to see therequired and optional arguments.)
importloggingimportlogging.handlersimportsmtplibclassBufferingSMTPHandler(logging.handlers.BufferingHandler):def__init__(self,mailhost,port,username,password,fromaddr,toaddrs,subject,capacity):logging.handlers.BufferingHandler.__init__(self,capacity)self.mailhost=mailhostself.mailport=portself.username=usernameself.password=passwordself.fromaddr=fromaddrifisinstance(toaddrs,str):toaddrs=[toaddrs]self.toaddrs=toaddrsself.subject=subjectself.setFormatter(logging.Formatter("%(asctime)s%(levelname)-5s%(message)s"))defflush(self):iflen(self.buffer)>0:try:smtp=smtplib.SMTP(self.mailhost,self.mailport)smtp.starttls()smtp.login(self.username,self.password)msg="From:%s\r\nTo:%s\r\nSubject:%s\r\n\r\n"%(self.fromaddr,','.join(self.toaddrs),self.subject)forrecordinself.buffer:s=self.format(record)msg=msg+s+"\r\n"smtp.sendmail(self.fromaddr,self.toaddrs,msg)smtp.quit()exceptException:iflogging.raiseExceptions:raiseself.buffer=[]if__name__=='__main__':importargparseap=argparse.ArgumentParser()aa=ap.add_argumentaa('host',metavar='HOST',help='SMTP server')aa('--port','-p',type=int,default=587,help='SMTP port')aa('user',metavar='USER',help='SMTP username')aa('password',metavar='PASSWORD',help='SMTP password')aa('to',metavar='TO',help='Addressee for emails')aa('sender',metavar='SENDER',help='Sender email address')aa('--subject','-s',default='Test Logging email from Python logging module (buffering)',help='Subject of email')options=ap.parse_args()logger=logging.getLogger()logger.setLevel(logging.DEBUG)h=BufferingSMTPHandler(options.host,options.port,options.user,options.password,options.sender,options.to,options.subject,10)logger.addHandler(h)foriinrange(102):logger.info("Info index =%d",i)h.flush()h.close()
If you run this script and your SMTP server is correctly set up, you should find thatit sends eleven emails to the addressee you specify. The first ten emails will eachhave ten log messages, and the eleventh will have two messages. That makes up 102messages as specified in the script.
Formatting times using UTC (GMT) via configuration¶
Sometimes you want to format times using UTC, which can be done using a classsuch asUTCFormatter
, shown below:
importloggingimporttimeclassUTCFormatter(logging.Formatter):converter=time.gmtime
and you can then use theUTCFormatter
in your code instead ofFormatter
. If you want to do that via configuration, you canuse thedictConfig()
API with an approach illustrated bythe following complete example:
importloggingimportlogging.configimporttimeclassUTCFormatter(logging.Formatter):converter=time.gmtimeLOGGING={'version':1,'disable_existing_loggers':False,'formatters':{'utc':{'()':UTCFormatter,'format':'%(asctime)s%(message)s',},'local':{'format':'%(asctime)s%(message)s',}},'handlers':{'console1':{'class':'logging.StreamHandler','formatter':'utc',},'console2':{'class':'logging.StreamHandler','formatter':'local',},},'root':{'handlers':['console1','console2'],}}if__name__=='__main__':logging.config.dictConfig(LOGGING)logging.warning('The local time is%s',time.asctime())
When this script is run, it should print something like:
2015-10-17 12:53:29,501 The local time is Sat Oct 17 13:53:29 20152015-10-17 13:53:29,501 The local time is Sat Oct 17 13:53:29 2015
showing how the time is formatted both as local time and UTC, one for eachhandler.
Using a context manager for selective logging¶
There are times when it would be useful to temporarily change the loggingconfiguration and revert it back after doing something. For this, a contextmanager is the most obvious way of saving and restoring the logging context.Here is a simple example of such a context manager, which allows you tooptionally change the logging level and add a logging handler purely in thescope of the context manager:
importloggingimportsysclassLoggingContext:def__init__(self,logger,level=None,handler=None,close=True):self.logger=loggerself.level=levelself.handler=handlerself.close=closedef__enter__(self):ifself.levelisnotNone:self.old_level=self.logger.levelself.logger.setLevel(self.level)ifself.handler:self.logger.addHandler(self.handler)def__exit__(self,et,ev,tb):ifself.levelisnotNone:self.logger.setLevel(self.old_level)ifself.handler:self.logger.removeHandler(self.handler)ifself.handlerandself.close:self.handler.close()# implicit return of None => don't swallow exceptions
If you specify a level value, the logger’s level is set to that value in thescope of the with block covered by the context manager. If you specify ahandler, it is added to the logger on entry to the block and removed on exitfrom the block. You can also ask the manager to close the handler for you onblock exit - you could do this if you don’t need the handler any more.
To illustrate how it works, we can add the following block of code to theabove:
if__name__=='__main__':logger=logging.getLogger('foo')logger.addHandler(logging.StreamHandler())logger.setLevel(logging.INFO)logger.info('1. This should appear just once on stderr.')logger.debug('2. This should not appear.')withLoggingContext(logger,level=logging.DEBUG):logger.debug('3. This should appear once on stderr.')logger.debug('4. This should not appear.')h=logging.StreamHandler(sys.stdout)withLoggingContext(logger,level=logging.DEBUG,handler=h,close=True):logger.debug('5. This should appear twice - once on stderr and once on stdout.')logger.info('6. This should appear just once on stderr.')logger.debug('7. This should not appear.')
We initially set the logger’s level toINFO
, so message #1 appears andmessage #2 doesn’t. We then change the level toDEBUG
temporarily in thefollowingwith
block, and so message #3 appears. After the block exits, thelogger’s level is restored toINFO
and so message #4 doesn’t appear. In thenextwith
block, we set the level toDEBUG
again but also add a handlerwriting tosys.stdout
. Thus, message #5 appears twice on the console (onceviastderr
and once viastdout
). After thewith
statement’scompletion, the status is as it was before so message #6 appears (like message#1) whereas message #7 doesn’t (just like message #2).
If we run the resulting script, the result is as follows:
$pythonlogctx.py1. This should appear just once on stderr.3. This should appear once on stderr.5. This should appear twice - once on stderr and once on stdout.5. This should appear twice - once on stderr and once on stdout.6. This should appear just once on stderr.
If we run it again, but pipestderr
to/dev/null
, we see the following,which is the only message written tostdout
:
$pythonlogctx.py2>/dev/null5. This should appear twice - once on stderr and once on stdout.
Once again, but pipingstdout
to/dev/null
, we get:
$pythonlogctx.py>/dev/null1. This should appear just once on stderr.3. This should appear once on stderr.5. This should appear twice - once on stderr and once on stdout.6. This should appear just once on stderr.
In this case, the message #5 printed tostdout
doesn’t appear, as expected.
Of course, the approach described here can be generalised, for example to attachlogging filters temporarily. Note that the above code works in Python 2 as wellas Python 3.
A CLI application starter template¶
Here’s an example which shows how you can:
Use a logging level based on command-line arguments
Dispatch to multiple subcommands in separate files, all logging at the samelevel in a consistent way
Make use of simple, minimal configuration
Suppose we have a command-line application whose job is to stop, start orrestart some services. This could be organised for the purposes of illustrationas a fileapp.py
that is the main script for the application, with individualcommands implemented instart.py
,stop.py
andrestart.py
. Supposefurther that we want to control the verbosity of the application via acommand-line argument, defaulting tologging.INFO
. Here’s one way thatapp.py
could be written:
importargparseimportimportlibimportloggingimportosimportsysdefmain(args=None):scriptname=os.path.basename(__file__)parser=argparse.ArgumentParser(scriptname)levels=('DEBUG','INFO','WARNING','ERROR','CRITICAL')parser.add_argument('--log-level',default='INFO',choices=levels)subparsers=parser.add_subparsers(dest='command',help='Available commands:')start_cmd=subparsers.add_parser('start',help='Start a service')start_cmd.add_argument('name',metavar='NAME',help='Name of service to start')stop_cmd=subparsers.add_parser('stop',help='Stop one or more services')stop_cmd.add_argument('names',metavar='NAME',nargs='+',help='Name of service to stop')restart_cmd=subparsers.add_parser('restart',help='Restart one or more services')restart_cmd.add_argument('names',metavar='NAME',nargs='+',help='Name of service to restart')options=parser.parse_args()# the code to dispatch commands could all be in this file. For the purposes# of illustration only, we implement each command in a separate module.try:mod=importlib.import_module(options.command)cmd=getattr(mod,'command')except(ImportError,AttributeError):print('Unable to find the code for command\'%s\''%options.command)return1# Could get fancy here and load configuration from file or dictionarylogging.basicConfig(level=options.log_level,format='%(levelname)s%(name)s%(message)s')cmd(options)if__name__=='__main__':sys.exit(main())
And thestart
,stop
andrestart
commands can be implemented inseparate modules, like so for starting:
# start.pyimportlogginglogger=logging.getLogger(__name__)defcommand(options):logger.debug('About to start%s',options.name)# actually do the command processing here ...logger.info('Started the\'%s\' service.',options.name)
and thus for stopping:
# stop.pyimportlogginglogger=logging.getLogger(__name__)defcommand(options):n=len(options.names)ifn==1:plural=''services='\'%s\''%options.names[0]else:plural='s'services=', '.join('\'%s\''%namefornameinoptions.names)i=services.rfind(', ')services=services[:i]+' and '+services[i+2:]logger.debug('About to stop%s',services)# actually do the command processing here ...logger.info('Stopped the%s service%s.',services,plural)
and similarly for restarting:
# restart.pyimportlogginglogger=logging.getLogger(__name__)defcommand(options):n=len(options.names)ifn==1:plural=''services='\'%s\''%options.names[0]else:plural='s'services=', '.join('\'%s\''%namefornameinoptions.names)i=services.rfind(', ')services=services[:i]+' and '+services[i+2:]logger.debug('About to restart%s',services)# actually do the command processing here ...logger.info('Restarted the%s service%s.',services,plural)
If we run this application with the default log level, we get output like this:
$pythonapp.pystartfooINFO start Started the 'foo' service.$pythonapp.pystopfoobarINFO stop Stopped the 'foo' and 'bar' services.$pythonapp.pyrestartfoobarbazINFO restart Restarted the 'foo', 'bar' and 'baz' services.
The first word is the logging level, and the second word is the module orpackage name of the place where the event was logged.
If we change the logging level, then we can change the information sent to thelog. For example, if we want more information:
$pythonapp.py--log-levelDEBUGstartfooDEBUG start About to start fooINFO start Started the 'foo' service.$pythonapp.py--log-levelDEBUGstopfoobarDEBUG stop About to stop 'foo' and 'bar'INFO stop Stopped the 'foo' and 'bar' services.$pythonapp.py--log-levelDEBUGrestartfoobarbazDEBUG restart About to restart 'foo', 'bar' and 'baz'INFO restart Restarted the 'foo', 'bar' and 'baz' services.
And if we want less:
$pythonapp.py--log-levelWARNINGstartfoo$pythonapp.py--log-levelWARNINGstopfoobar$pythonapp.py--log-levelWARNINGrestartfoobarbaz
In this case, the commands don’t print anything to the console, since nothingatWARNING
level or above is logged by them.
A Qt GUI for logging¶
A question that comes up from time to time is about how to log to a GUIapplication. TheQt framework is a popularcross-platform UI framework with Python bindings usingPySide2orPyQt5 libraries.
The following example shows how to log to a Qt GUI. This introduces a simpleQtHandler
class which takes a callable, which should be a slot in the mainthread that does GUI updates. A worker thread is also created to show how youcan log to the GUI from both the UI itself (via a button for manual logging)as well as a worker thread doing work in the background (here, just loggingmessages at random levels with random short delays in between).
The worker thread is implemented using Qt’sQThread
class rather than thethreading
module, as there are circumstances where one has to useQThread
, which offers better integration with otherQt
components.
The code should work with recent releases of any ofPySide6
,PyQt6
,PySide2
orPyQt5
. You should be able to adapt the approach to earlierversions of Qt. Please refer to the comments in the code snippet for moredetailed information.
importdatetimeimportloggingimportrandomimportsysimporttime# Deal with minor differences between different Qt packagestry:fromPySide6importQtCore,QtGui,QtWidgetsSignal=QtCore.SignalSlot=QtCore.SlotexceptImportError:try:fromPyQt6importQtCore,QtGui,QtWidgetsSignal=QtCore.pyqtSignalSlot=QtCore.pyqtSlotexceptImportError:try:fromPySide2importQtCore,QtGui,QtWidgetsSignal=QtCore.SignalSlot=QtCore.SlotexceptImportError:fromPyQt5importQtCore,QtGui,QtWidgetsSignal=QtCore.pyqtSignalSlot=QtCore.pyqtSlotlogger=logging.getLogger(__name__)## Signals need to be contained in a QObject or subclass in order to be correctly# initialized.#classSignaller(QtCore.QObject):signal=Signal(str,logging.LogRecord)## Output to a Qt GUI is only supposed to happen on the main thread. So, this# handler is designed to take a slot function which is set up to run in the main# thread. In this example, the function takes a string argument which is a# formatted log message, and the log record which generated it. The formatted# string is just a convenience - you could format a string for output any way# you like in the slot function itself.## You specify the slot function to do whatever GUI updates you want. The handler# doesn't know or care about specific UI elements.#classQtHandler(logging.Handler):def__init__(self,slotfunc,*args,**kwargs):super().__init__(*args,**kwargs)self.signaller=Signaller()self.signaller.signal.connect(slotfunc)defemit(self,record):s=self.format(record)self.signaller.signal.emit(s,record)## This example uses QThreads, which means that the threads at the Python level# are named something like "Dummy-1". The function below gets the Qt name of the# current thread.#defctname():returnQtCore.QThread.currentThread().objectName()## Used to generate random levels for logging.#LEVELS=(logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL)## This worker class represents work that is done in a thread separate to the# main thread. The way the thread is kicked off to do work is via a button press# that connects to a slot in the worker.## Because the default threadName value in the LogRecord isn't much use, we add# a qThreadName which contains the QThread name as computed above, and pass that# value in an "extra" dictionary which is used to update the LogRecord with the# QThread name.## This example worker just outputs messages sequentially, interspersed with# random delays of the order of a few seconds.#classWorker(QtCore.QObject):@Slot()defstart(self):extra={'qThreadName':ctname()}logger.debug('Started work',extra=extra)i=1# Let the thread run until interrupted. This allows reasonably clean# thread termination.whilenotQtCore.QThread.currentThread().isInterruptionRequested():delay=0.5+random.random()*2time.sleep(delay)try:ifrandom.random()<0.1:raiseValueError('Exception raised:%d'%i)else:level=random.choice(LEVELS)logger.log(level,'Message after delay of%3.1f:%d',delay,i,extra=extra)exceptValueErrorase:logger.exception('Failed:%s',e,extra=extra)i+=1## Implement a simple UI for this cookbook example. This contains:## * A read-only text edit window which holds formatted log messages# * A button to start work and log stuff in a separate thread# * A button to log something from the main thread# * A button to clear the log window#classWindow(QtWidgets.QWidget):COLORS={logging.DEBUG:'black',logging.INFO:'blue',logging.WARNING:'orange',logging.ERROR:'red',logging.CRITICAL:'purple',}def__init__(self,app):super().__init__()self.app=appself.textedit=te=QtWidgets.QPlainTextEdit(self)# Set whatever the default monospace font is for the platformf=QtGui.QFont('nosuchfont')ifhasattr(f,'Monospace'):f.setStyleHint(f.Monospace)else:f.setStyleHint(f.StyleHint.Monospace)# for Qt6te.setFont(f)te.setReadOnly(True)PB=QtWidgets.QPushButtonself.work_button=PB('Start background work',self)self.log_button=PB('Log a message at a random level',self)self.clear_button=PB('Clear log window',self)self.handler=h=QtHandler(self.update_status)# Remember to use qThreadName rather than threadName in the format string.fs='%(asctime)s%(qThreadName)-12s%(levelname)-8s%(message)s'formatter=logging.Formatter(fs)h.setFormatter(formatter)logger.addHandler(h)# Set up to terminate the QThread when we exitapp.aboutToQuit.connect(self.force_quit)# Lay out all the widgetslayout=QtWidgets.QVBoxLayout(self)layout.addWidget(te)layout.addWidget(self.work_button)layout.addWidget(self.log_button)layout.addWidget(self.clear_button)self.setFixedSize(900,400)# Connect the non-worker slots and signalsself.log_button.clicked.connect(self.manual_update)self.clear_button.clicked.connect(self.clear_display)# Start a new worker thread and connect the slots for the workerself.start_thread()self.work_button.clicked.connect(self.worker.start)# Once started, the button should be disabledself.work_button.clicked.connect(lambda:self.work_button.setEnabled(False))defstart_thread(self):self.worker=Worker()self.worker_thread=QtCore.QThread()self.worker.setObjectName('Worker')self.worker_thread.setObjectName('WorkerThread')# for qThreadNameself.worker.moveToThread(self.worker_thread)# This will start an event loop in the worker threadself.worker_thread.start()defkill_thread(self):# Just tell the worker to stop, then tell it to quit and wait for that# to happenself.worker_thread.requestInterruption()ifself.worker_thread.isRunning():self.worker_thread.quit()self.worker_thread.wait()else:print('worker has already exited.')defforce_quit(self):# For use when the window is closedifself.worker_thread.isRunning():self.kill_thread()# The functions below update the UI and run in the main thread because# that's where the slots are set up@Slot(str,logging.LogRecord)defupdate_status(self,status,record):color=self.COLORS.get(record.levelno,'black')s='<pre><font color="%s">%s</font></pre>'%(color,status)self.textedit.appendHtml(s)@Slot()defmanual_update(self):# This function uses the formatted message passed in, but also uses# information from the record to format the message in an appropriate# color according to its severity (level).level=random.choice(LEVELS)extra={'qThreadName':ctname()}logger.log(level,'Manually logged!',extra=extra)@Slot()defclear_display(self):self.textedit.clear()defmain():QtCore.QThread.currentThread().setObjectName('MainThread')logging.getLogger().setLevel(logging.DEBUG)app=QtWidgets.QApplication(sys.argv)example=Window(app)example.show()ifhasattr(app,'exec'):rc=app.exec()else:rc=app.exec_()sys.exit(rc)if__name__=='__main__':main()
Logging to syslog with RFC5424 support¶
AlthoughRFC 5424 dates from 2009, most syslog servers are configured by default touse the olderRFC 3164, which hails from 2001. Whenlogging
was added to Pythonin 2003, it supported the earlier (and only existing) protocol at the time. SinceRFC5424 came out, as there has not been widespread deployment of it in syslogservers, theSysLogHandler
functionality has not beenupdated.
RFC 5424 contains some useful features such as support for structured data, and if youneed to be able to log to a syslog server with support for it, you can do so with asubclassed handler which looks something like this:
importdatetimeimportlogging.handlersimportreimportsocketimporttimeclassSysLogHandler5424(logging.handlers.SysLogHandler):tz_offset=re.compile(r'([+-]\d{2})(\d{2})$')escaped=re.compile(r'([\]"\\])')def__init__(self,*args,**kwargs):self.msgid=kwargs.pop('msgid',None)self.appname=kwargs.pop('appname',None)super().__init__(*args,**kwargs)defformat(self,record):version=1asctime=datetime.datetime.fromtimestamp(record.created).isoformat()m=self.tz_offset.match(time.strftime('%z'))has_offset=Falseifmandtime.timezone:hrs,mins=m.groups()ifint(hrs)orint(mins):has_offset=Trueifnothas_offset:asctime+='Z'else:asctime+=f'{hrs}:{mins}'try:hostname=socket.gethostname()exceptException:hostname='-'appname=self.appnameor'-'procid=record.processmsgid='-'msg=super().format(record)sdata='-'ifhasattr(record,'structured_data'):sd=record.structured_data# This should be a dict where the keys are SD-ID and the value is a# dict mapping PARAM-NAME to PARAM-VALUE (refer to the RFC for what these# mean)# There's no error checking here - it's purely for illustration, and you# can adapt this code for use in production environmentsparts=[]defreplacer(m):g=m.groups()return'\\'+g[0]forsdid,dvinsd.items():part=f'[{sdid}'fork,vindv.items():s=str(v)s=self.escaped.sub(replacer,s)part+=f'{k}="{s}"'part+=']'parts.append(part)sdata=''.join(parts)returnf'{version}{asctime}{hostname}{appname}{procid}{msgid}{sdata}{msg}'
You’ll need to be familiar with RFC 5424 to fully understand the above code, and itmay be that you have slightly different needs (e.g. for how you pass structural datato the log). Nevertheless, the above should be adaptable to your speciric needs. Withthe above handler, you’d pass structured data using something like this:
sd={'foo@12345':{'bar':'baz','baz':'bozz','fizz':r'buzz'},'foo@54321':{'rab':'baz','zab':'bozz','zzif':r'buzz'}}extra={'structured_data':sd}i=1logger.debug('Message%d',i,extra=extra)
How to treat a logger like an output stream¶
Sometimes, you need to interface to a third-party API which expects a file-likeobject to write to, but you want to direct the API’s output to a logger. Youcan do this using a class which wraps a logger with a file-like API.Here’s a short script illustrating such a class:
importloggingclassLoggerWriter:def__init__(self,logger,level):self.logger=loggerself.level=leveldefwrite(self,message):ifmessage!='\n':# avoid printing bare newlines, if you likeself.logger.log(self.level,message)defflush(self):# doesn't actually do anything, but might be expected of a file-like# object - so optional depending on your situationpassdefclose(self):# doesn't actually do anything, but might be expected of a file-like# object - so optional depending on your situation. You might want# to set a flag so that later calls to write raise an exceptionpassdefmain():logging.basicConfig(level=logging.DEBUG)logger=logging.getLogger('demo')info_fp=LoggerWriter(logger,logging.INFO)debug_fp=LoggerWriter(logger,logging.DEBUG)print('An INFO message',file=info_fp)print('A DEBUG message',file=debug_fp)if__name__=="__main__":main()
When this script is run, it prints
INFO:demo:An INFO messageDEBUG:demo:A DEBUG message
You could also useLoggerWriter
to redirectsys.stdout
andsys.stderr
by doing something like this:
importsyssys.stdout=LoggerWriter(logger,logging.INFO)sys.stderr=LoggerWriter(logger,logging.WARNING)
You should do thisafter configuring logging for your needs. In the aboveexample, thebasicConfig()
call does this (using thesys.stderr
valuebefore it is overwritten by aLoggerWriter
instance). Then, you’d get this kind of result:
>>>print('Foo')INFO:demo:Foo>>>print('Bar',file=sys.stderr)WARNING:demo:Bar>>>
Of course, the examples above show output according to the format used bybasicConfig()
, but you can use a different formatter when youconfigure logging.
Note that with the above scheme, you are somewhat at the mercy of buffering andthe sequence of write calls which you are intercepting. For example, with thedefinition ofLoggerWriter
above, if you have the snippet
sys.stderr=LoggerWriter(logger,logging.WARNING)1/0
then running the script results in
WARNING:demo:Traceback (most recent call last):WARNING:demo: File "/home/runner/cookbook-loggerwriter/test.py", line 53, in <module>WARNING:demo:WARNING:demo:main()WARNING:demo: File "/home/runner/cookbook-loggerwriter/test.py", line 49, in mainWARNING:demo:WARNING:demo:1 / 0WARNING:demo:ZeroDivisionErrorWARNING:demo::WARNING:demo:division by zero
As you can see, this output isn’t ideal. That’s because the underlying codewhich writes tosys.stderr
makes multiple writes, each of which results in aseparate logged line (for example, the last three lines above). To get aroundthis problem, you need to buffer things and only output log lines when newlinesare seen. Let’s use a slightly better implementation ofLoggerWriter
:
classBufferingLoggerWriter(LoggerWriter):def__init__(self,logger,level):super().__init__(logger,level)self.buffer=''defwrite(self,message):if'\n'notinmessage:self.buffer+=messageelse:parts=message.split('\n')ifself.buffer:s=self.buffer+parts.pop(0)self.logger.log(self.level,s)self.buffer=parts.pop()forpartinparts:self.logger.log(self.level,part)
This just buffers up stuff until a newline is seen, and then logs completelines. With this approach, you get better output:
WARNING:demo:Traceback (most recent call last):WARNING:demo: File "/home/runner/cookbook-loggerwriter/main.py", line 55, in <module>WARNING:demo: main()WARNING:demo: File "/home/runner/cookbook-loggerwriter/main.py", line 52, in mainWARNING:demo: 1/0WARNING:demo:ZeroDivisionError: division by zero
Patterns to avoid¶
Although the preceding sections have described ways of doing things you mightneed to do or deal with, it is worth mentioning some usage patterns which areunhelpful, and which should therefore be avoided in most cases. The followingsections are in no particular order.
Opening the same log file multiple times¶
On Windows, you will generally not be able to open the same file multiple timesas this will lead to a “file is in use by another process” error. However, onPOSIX platforms you’ll not get any errors if you open the same file multipletimes. This could be done accidentally, for example by:
Adding a file handler more than once which references the same file (e.g. bya copy/paste/forget-to-change error).
Opening two files that look different, as they have different names, but arethe same because one is a symbolic link to the other.
Forking a process, following which both parent and child have a reference tothe same file. This might be through use of the
multiprocessing
module,for example.
Opening a file multiple times mightappear to work most of the time, but canlead to a number of problems in practice:
Logging output can be garbled because multiple threads or processes try towrite to the same file. Although logging guards against concurrent use of thesame handler instance by multiple threads, there is no such protection ifconcurrent writes are attempted by two different threads using two differenthandler instances which happen to point to the same file.
An attempt to delete a file (e.g. during file rotation) silently fails,because there is another reference pointing to it. This can lead to confusionand wasted debugging time - log entries end up in unexpected places, or arelost altogether. Or a file that was supposed to be moved remains in place,and grows in size unexpectedly despite size-based rotation being supposedlyin place.
Use the techniques outlined inLogging to a single file from multiple processes to circumvent suchissues.
Using loggers as attributes in a class or passing them as parameters¶
While there might be unusual cases where you’ll need to do this, in generalthere is no point because loggers are singletons. Code can always access agiven logger instance by name usinglogging.getLogger(name)
, so passinginstances around and holding them as instance attributes is pointless. Notethat in other languages such as Java and C#, loggers are often static classattributes. However, this pattern doesn’t make sense in Python, where themodule (and not the class) is the unit of software decomposition.
Adding handlers other thanNullHandler
to a logger in a library¶
Configuring logging by adding handlers, formatters and filters is theresponsibility of the application developer, not the library developer. If youare maintaining a library, ensure that you don’t add handlers to any of yourloggers other than aNullHandler
instance.
Creating a lot of loggers¶
Loggers are singletons that are never freed during a script execution, and socreating lots of loggers will use up memory which can’t then be freed. Ratherthan create a logger per e.g. file processed or network connection made, usetheexisting mechanisms for passing contextualinformation into your logs and restrict the loggers created to those describingareas within your application (generally modules, but occasionally slightlymore fine-grained than that).
Other resources¶
See also
- Module
logging
API reference for the logging module.
- Module
logging.config
Configuration API for the logging module.
- Module
logging.handlers
Useful handlers included with the logging module.