Python logging to multiple handlers, at different log levels? Python logging to multiple handlers, at different log levels? python python

Python logging to multiple handlers, at different log levels?


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, loggingfrom logging.config import dictConfigFORMAT = "%(asctime)s {app} [%(thread)d] %(levelname)-5s %(name)s - %(message)s. [file=%(filename)s:%(lineno)d]"DATE_FORMAT = Nonedef 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': '{}/log1.log'.format(log_dir),                'formatter': 'standard',            },            'file2': {                'class': 'logging.handlers.TimedRotatingFileHandler',                'when': 'midnight',                'utc': True,                'backupCount': 5,                'level': level,                'filename': '{}/log2.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=LEVELlogger = logging.getLogger(__name__)second_logger = logging.getLogger('second_log')second_logger.propagate = False


An approach to update your handler:

import loggingfrom rootmodule.mymodule import myloggerdef 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.