How can python use configuration files to configure logging, to easily print multiple logger to different files?

that"s all I know so far:

handlers:
    file_a:
        formatter: standard
        filename: /var/log/a.log
    file_b:
        formatter: standard
        filename: /var/log/b.log
loggers:
    logger_a:
        level: DEBUG
        handlers: [file_a]
    logger_b:
        level: DEBUG
        handlers: [file_b]

is there no way to determine the file name based on the name of logger?
for example:

handlers:
    multi_file:
        filename: /var/log/{name}.log

at present, what is available is to wrap the logging.getLogger method. Is there no easier way to configure its fileHandler, when taking logger?

Mar.18,2021

realized the way I said, which felt too bulky.

class MyFileHandlerFactory(logging.handlers.RotatingFileHandler):
    def __init__(self, filename, **kwargs):
        FileUtils.ready_file_dir(filename)
        super(MyFileHandlerFactory, self).__init__(filename, **kwargs)


def wrap_logger():
    _raw_getLogger = logging.getLogger

    def getLogger(name=None):
        logger = _raw_getLogger(name)
        if name is None:
            return logger
        for i in range(len(logger.handlers)):
            old_handler = logger.handlers[i]
            if not isinstance(old_handler, logging.handlers.RotatingFileHandler):
                continue
            base_file_name = old_handler.baseFilename
            if '{logger_name}' not in base_file_name:
                continue
            new_handler = MyFileHandlerFactory(base_file_name.format(logger_name=logger.name.replace('.', '_')),
                                               mode=old_handler.mode,
                                               maxBytes=old_handler.maxBytes,
                                               backupCount=old_handler.backupCount,
                                               encoding=old_handler.encoding,
                                               delay=old_handler.delay)
            new_handler.setFormatter(old_handler.formatter)
            new_handler.setLevel(old_handler.level)
            new_handler.set_name(logger.name + '.' + old_handler.name)
            logger.handlers[i] = new_handler
        return logger

    if logging.getLogger.__module__ != 'logging':
        -sharp 
        return
    logging.getLogger = getLogger


def init():
    import yaml
    with open(config.get_resource_path('logger.yml')) as f_conf:
        dict_conf = yaml.load(f_conf)
    logging.config.dictConfig(dict_conf)
    wrap_logger()

anyway, my experience is that you don't have to write wrapper, because you don't know if the person who maintains the code later can understand the nesting you write.

I think we can simply inherit the Manager class of logging to implement it:

import logging


class CustomManger(logging.Manager):
    def getLogger(self, name, format=None):
        logger = super(CustomManger, self).getLogger(name)
        -sharp Formatter
        filename = '{}.log'.format(name)
        logger.addHandler(logging.FileHandler(filename))
        return logger


manager = CustomManger(logging.root)
if __name__ == '__main__':
        
    logger_a = manager.getLogger('a')
    logger_b = manager.getLogger('b')
    logger_a.error('111')
    logger_b.error('222')
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b321b7-2be16.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b321b7-2be16.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?