describes how this extra information can be automatically added to log messages formatter objects to change the log message formatyou must first create formatter objectformatter([fmt [datefmt]]creates new formatter object fmt provides format string for messages within fmtyou can place various expansions as previously described for the basicconfig(function datefmt is date format string compatible with the time strftime(function if omittedthe date format is set to the iso format to take effectformatter objects must be attached to handler objects this is done using the setformatter(method of handler instance setformatter(formatsets the message formatter object used to create log messages on the handler instance format must be an instance of formatter here is an example that illustrates how to customize the log message format on handlerimport logging import sys set the message format format logging formatter("%(levelname)- %(asctime) %(message) "create handler that prints critical level messages to stderr crit_hand logging streamhandler(sys stderrcrit_hand setlevel(logging criticalcrit_hand setformatter(formatin this examplea custom formatter is set on the crit_hand handler if logging message such as "creeping death detected is processed by this handlerthe following log message is producedcritical : : , creeping death detected adding extra context to messages in certain applicationsit is useful to add additional context information to log messages this extra information can be provided in one of two ways firstall of the basic logging operations ( log critical()log warning()etc have keyword parameter extra that is used to supply dictionary of additional fields for use in message format strings these fields are merged in with the context data previously described for formatter objects here is an exampleimport loggingsocket logging basicconfigformat "%(hostname) %(levelname)- %(asctime) %(message)ssome extra context netinfo 'hostnamesocket gethostname()'ipsocket gethostbyname(socket gethostname()log logging getlogger('app'issue log message with the extra context data lib fl ff