Python logging to multiple handlers, at different log levels?

前端 未结 2 1223
孤独总比滥情好
孤独总比滥情好 2020-12-30 00:42

Folks,

I\'m scratching my head on a python logging config that I can\'t get right.

Let\'s say I have the following package installed:

mypacka         


        
相关标签:
2条回答
  • 2020-12-30 00:53

    Use dictConfig. Here is an example of logging to multiple files with separate handles in dictConfig. This isn't exactly what you are looking for, but you could modify this example and simply change the level of each handler that you are looking to use.

    import os, logging
    from logging.config import dictConfig
    
    FORMAT = "%(asctime)s {app} [%(thread)d] %(levelname)-5s %(name)s - %(message)s. [file=%(filename)s:%(lineno)d]"
    DATE_FORMAT = None
    
    
    def setup_logging(name, level="INFO", fmt=FORMAT):
        formatted = fmt.format(app=name)
        log_dir = r'C:/log_directory'
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
    
        logging_config = {
            "version": 1,
            'disable_existing_loggers': False,
            "formatters": {
                'standard': {
                    'format': formatted
                }
            },
            "handlers": {
                'default': {
                    'class': 'logging.StreamHandler',
                    'formatter': 'standard',
                    'level': level,
                    'stream': 'ext://sys.stdout'
                },
                'file': {
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'when': 'midnight',
                    'utc': True,
                    'backupCount': 5,
                    'level': level,
                    'filename': '{}/app_manager.log'.format(log_dir),
                    'formatter': 'standard',
                },
                'file2': {
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'when': 'midnight',
                    'utc': True,
                    'backupCount': 5,
                    'level': level,
                    'filename': '{}/unified_log.log'.format(log_dir),
                    'formatter': 'standard',
                }
            },
            "loggers": {
                "": {
                    'handlers': ['default', 'file'],
                    'level': level
                },
                "second_log": {
                    'handlers': ['default', 'file2'],
                    'level': level
                }
            }
        }
    
        dictConfig(logging_config)
    
    log.setup_logging(name="log-name", level=LEVEL
    logger = logging.getLogger(__name__)
    second_logger = logging.getLogger('second_log')
    second_logger.propagate = False
    
    0 讨论(0)
  • 2020-12-30 01:01

    An approach to update your handler:

    import logging
    
    from rootmodule.mymodule import mylogger
    
    def update_handler_level(logger,  handler_type, level="INFO"):
        # if not root logger user logger.parent
        for handler in logger.handlers or logger.parent.handlers:
            if isinstance(handler, handler_type):
                print(handler.level)
                handler.setLevel(getattr(logging, level, "INFO"))
                print(handler.level)
    
    mylogger.debug('test')
    update_handler_level(mylogger, logging.StreamHandler)
    mylogger.debug('test')
    

    My logging.cfg is pretty similar than your except the fact that the logger name si set in a constant module (a can do module rename without breaking the logging configuration)

    To update from command line, you must have a mapping between your opts value and logging.Handler sub class name.

    0 讨论(0)
提交回复
热议问题