简体   繁体   中英

Confused about python logging with config file (only logs what is set for root_logger in config file)

I (now) have the following code:

import logging
from logging.config import fileConfig


def function1():
    logger = logging.getLogger('function1')
    logger.debug('Debug')
    logger.info('Info')
    logger.warning('Warn')
    logger.critical('Critical')


def main():
    fileConfig('test_log.conf')
    logger = logging.getLogger('main')
    logger.debug('Debug')
    logger.info('Info')
    logger.warning('Warn')
    logger.critical('Critical')
    function1()


if __name__ == '__main__':
    main()

And I have the following config file:

[loggers]
keys:root,log1

[handlers]
keys:consoleHandler,rotatingFileHandler

[formatters]
keys:logFormatter

[logger_root]
level:INFO
handlers:rotatingFileHandler,consoleHandler

[logger_log1]
level:DEBUG
handlers:rotatingFileHandler, consoleHandler
qualname:main
propagate=1

[handler_consoleHandler]
class:StreamHandler
formatter:logFormatter
args:(sys.stdout,)

[handler_rotatingFileHandler]
class=logging.handlers.RotatingFileHandler
level=NOTSET
args=('logs/Log.log', 'w', 5000)
formatter=logFormatter

[formatter_logFormatter]
format:%(asctime)s - %(name)s -%(levelname)s - %(message)s

When I run, I get the following output:

2020-03-23 19:16:30,529 - main -DEBUG - Debug
2020-03-23 19:16:30,529 - main -DEBUG - Debug
2020-03-23 19:16:30,530 - main -INFO - Info
2020-03-23 19:16:30,530 - main -INFO - Info
2020-03-23 19:16:30,530 - main -WARNING - Warn
2020-03-23 19:16:30,530 - main -WARNING - Warn
2020-03-23 19:16:30,530 - main -CRITICAL - Critical
2020-03-23 19:16:30,530 - main -CRITICAL - Critical
2020-03-23 19:16:30,531 - function1 -INFO - Info
2020-03-23 19:16:30,531 - function1 -WARNING - Warn
2020-03-23 19:16:30,531 - function1 -CRITICAL - Critical

I have given each of the loggers names and I still have the same problem

original post:

I have the following code:

def main():
    import logging
    from logging.config import fileConfig
    fileConfig('logging.conf')
    logger = logging.getLogger()
    logger.debug('Debug')
    logger.info('Info')
    logger.warning('Warn')
    logger.critical('Critical')

if __name__ == '__main__':
    main()

And I have the following config file:

[loggers]
keys:root,log1

[handlers]
keys:consoleHandler,rotatingFileHandler

[formatters]
keys:logFormatter

[logger_root]
level:INFO
handlers:rotatingFileHandler,consoleHandler

[logger_log1]
level:DEBUG
handlers:rotatingFileHandler, consoleHandler
qualname:main
propagate=1

[handler_consoleHandler]
class:StreamHandler
formatter:logFormatter
args:(sys.stdout,)

[handler_rotatingFileHandler]
class=logging.handlers.RotatingFileHandler
level=NOTSET
args=('logs/Log.log', 'w', 5000)
formatter=logFormatter

[formatter_logFormatter]
format:%(asctime)s - %(name)s -%(levelname)s - %(message)s

When I run, I get the following output:

2020-03-23 18:13:54,950 - tedCheck.main -WARNING - Warn
2020-03-23 18:13:54,950 - tedCheck.main -CRITICAL - Critical

What I was expecting was that no matter what the root_logger was set to, I could over-ride it in the specific log section for the function/method of the config file.

What good is having the section if it is going to do whatever the root_logger is set to? My guess is that my config file is hosed somehow.

This line gets the root logger in the hierarchy, specifically:

logger = logging.getLogger()

If you don't want the root logger, then you need to pass a name

logger = logging.getLogger(name="main")

Note that with your existing configuration, a propagating log1 and console/file handlers configured for root as well, you will likely see duplicate log events.

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