python - Best way to add custom information to for logging -


i've been reading python logging documentation, , has lot of functionality...for everything.

the problem i'm facing i'm not dutch , i'm not sure right way is.

i running events in simulator, , prefix every log message timestamp of simulated time (probably length formatter too, keep looking good). change in subclass of logger or handler, don't think right way.

i think right way use loggeradapter or filter. right, , if so, 1 should prefer?

surely if need prefix every log message timestamp, need provide appropriate format string formatter? mentioned here in documentation.

update: loggeradapter more appropriate situations contextual information moderately long-lived. example, in network server application handles multiple client connections, connection details (e.g. client ip address) might useful context, create loggeradapter instance when new client connection created. doesn't sound simulation scenario.

a filter, on other hand, used if use simulation time, e.g.

class simulationfilter(logging.filter):     def __init__(self, context):         """         set context passed in allows access         simulation times.         """         self.context = context      def filter(self, record):         "add sim_time field record, formatted wish"         record.sim_time = '%s' % self.context.get_sim_time()         return true 

and add %(sim_time)s in formatter's format string.

alternatively, if know simulation time whenever make logging call, e.g.

logger.debug('message %s', arguments, extra={'sim_time': sim_time}) 

and likewise have %(sim_time)s in formatter's format string.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -