Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to inject a variable into python logger Filter

Tags:

python

logging

I want to add session_id to every log message. So I can do it in every request this way.

log.info('[%s] my message', self.session_id);

or even a handler. But it requires to pass %s and session variable to log method every time when I log something. Well, to skip the dirty job I can create a filter:

class ContextFilter(logging.Filter):
  def filter(self, record):
    record.my_session = 'LOL'
    return True

I'm using django + tornado so I modify LOGGING variable in settings.py:

'filters': {
  'user_filter': {
    '()': 'my_package.ContextFilter',
   }
},

'formatters': {
'verbose': {
        'format':  '[%(asctime)s %(my_session)s] %(message)s',
    },
},

And when if I write something like that:

class A(object):

  def __init__(self):
    self.session_id = random(1024)

  def process(self):
    log.info('hello')

I would get:

[11:23 LOL] hello.

Fine, now I want to inject A.session_id to filter instead of LOL. Note that session_id is defined in scope of A every time I log something. Sure I can wrap every single logger method (trace, info, debug ...) and add there session functionality but this will break line number.

I would do something like a static variable and a static setter in ContextFilter, but unfortunately I'm using using tornado (request are run in the same thread), which means I can't event use thread local context. Is filter the only workaround to get a custom variable for log entry?

like image 989
deathangel908 Avatar asked Nov 26 '25 20:11

deathangel908


1 Answers

Just found the answer. I should use LoggingAdapter instead of Filter.

class A(object):

  def __init__(self):
    self.logger = logging.LoggerAdapter(log, {'session_id': random(1024)})

  def process(self):
    self.logger.info('hello')
like image 136
deathangel908 Avatar answered Nov 29 '25 09:11

deathangel908



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!