V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
scriptB0y
V2EX  ›  问与答

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

  •  
  •   scriptB0y · 2017-05-16 12:53:18 +08:00 · 2853 次点击
    这是一个创建于 2536 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在一个项目使用一个 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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1590 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 17:05 · PVG 01:05 · LAX 10:05 · JFK 13:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.