简体   繁体   中英

How to log different classes into different files?

Let's say I have different classes defined like this

class PS1:
    def __init__(self):
        pass

    def do_sth(self):
        logging.info('PS1 do sth')

class PS2:
    def __init__(self):
        pass

    def do_sth(self):
        logging.info('PS2 do sth')

How can I make PS1 log into PS1.log and PS2 log into PS2.log? I know I can set up loggers and file handlers and do something likes ps1_logger.info instead of logging.info . However, my current code base already has hundreds of logging.info , do I have to change them all?

When you call logging methods on the module level you are creating logs directly at the root logger, so you no longer can separate them by logger. The only chance left is to separate your logs by Handler with a filter. Since you don't want to update your logging calls to add any information about where the call happens you have to inspect the stack to find the calling class. The code below does this, but I highly recommend not using this solution in production and refactor your code to not send everything to the root logger. Look into logging adapters and/or the extra keyword argument for an alternative solution that modifies the logging calls.

import logging
import inspect


root = logging.getLogger()

ps1_handler = logging.FileHandler('ps1.log')
ps2_handler = logging.FileHandler('ps2.log')

def make_class_filter(class_name):
    def filter(record):
        for fi in inspect.stack():
            if 'self' in fi[0].f_locals and class_name in str(fi[0].f_locals['self']):
                return True 
        return False
    return filter

ps1_handler.addFilter(make_class_filter('PS1'))
ps2_handler.addFilter(make_class_filter('PS2'))

root.addHandler(ps1_handler)
root.addHandler(ps2_handler)

class PS1:
    def do_sth(self):
        logging.warning('PS1 do sth')

class PS2:
    def do_sth(self):
        logging.warning('PS2 do sth')


PS1().do_sth()
PS2().do_sth()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM