scriptB0y
V2EX  ›  问与答

Python 项目配置 logging 的正确姿势是什么?

  •  
  •   scriptB0y · May 16, 2017 · 3478 views
    This topic created in 3296 days ago, the information mentioned may be changed or developed.

    我在一个项目使用一个 logging_config.py 对 logging 做了配置,如何在其他模块中使用这个配置?

    比如说配置是这样的。

    import logging
    import logging.config
    
    logger = logging.getLogger(__name__)
    
    # load config from file
    # logging.config.fileConfig('logging.ini', disable_existing_loggers=False)
    # or, for dictConfig
    logging.config.dictConfig({
        'version': 1,
        'disable_existing_loggers': False,  # this fixes the problem
        'formatters': {
            'standard': {
                'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
            },
        },
        'handlers': {
            'default': {
                'level':'INFO',
                'class':'logging.StreamHandler',
            },
        },
        'loggers': {
            '': {
                'handlers': ['default'],
                'level': 'INFO',
                'propagate': True
            }
        }
    })
    

    那在其他模块使用这个配置之前,不是要先运行一个这个模块的代码吗?如果每个模块(程序不只有一个入口)都要 import logging_cofig,那不是就 import but not used 了 we

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6029 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 143ms · UTC 02:58 · PVG 10:58 · LAX 19:58 · JFK 22:58
    ♥ Do have faith in what you're doing.