V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
NeverBelieveMe
V2EX  ›  Python

log 使用方式错误,求指导

  •  
  •   NeverBelieveMe · 2020-02-26 17:15:12 +08:00 · 1121 次点击
    这是一个创建于 1534 天前的主题,其中的信息可能已经有所发展或是发生改变。
    A.py
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter("%(asctime)s - %(message)s"))
    logging.getLogger().addHandler(handler)


    B.py
    from A import logger
    C.py
    from A import logger

    我现在在项目中是这样使用的。之前的帖子里有人说不能这样重复引用,单独用 logger = logging.getLogger(__name__)。但是这样,logger 就需要重新配置了。所以不清楚怎么用是对的。
    gwy15
        1
    gwy15  
       2020-02-26 17:18:08 +08:00   ❤️ 1
    单独 logging.getLogger(__name__) 是对的,因为包名存在前缀,所以配置根 logger 就可以了( propagate=True )
    NeverBelieveMe
        2
    NeverBelieveMe  
    OP
       2020-02-26 17:23:23 +08:00
    @gwy15 明白了,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1269 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.