推荐学习书目
› 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
whathefoxsay
V2EX  ›  Python

如何优雅的使用 python logging 模块。。。。

  •  
  •   whathefoxsay · May 4, 2015 · 5136 views
    This topic created in 4161 days ago, the information mentioned may be changed or developed.
    我的方式:
    handler = handlers.RotatingFileHandler(LOG,'a',MAX_SIZE)
    level = logger.INFO
    logger = logging.getLogger(__name__)
    logger.setLevel(level)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setLevel(level)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    现在想将日志的配置和初始化分开,在之前django的配置中见过以比较优雅的使用dict来配置log, 请教是否有模板可以使用?
    7 replies  •  2015-05-04 18:24:50 +08:00
    hhkbp2
        1
    hhkbp2  
       May 4, 2015
    写个函数把要变化的参数抽取出来,是不是就可以了
    lyief
        2
    lyief  
       May 4, 2015
    建议把logger的配置写成文件,“易变参数写成配置文件”是我觉得很优雅的工程实践。
    des
        3
    des  
       May 4, 2015 via Android
    楼主混知乎的吧?
    liboyue
        4
    liboyue  
       May 4, 2015
    优雅的 => 优雅地
    goodryb
        5
    goodryb  
       May 4, 2015
    logging模块是可以通过配置文件来初始化,这样代码基本可以保持不动
    有需求改配置文件就可以了
    whathefoxsay
        7
    whathefoxsay  
    OP
       May 4, 2015
    @staticor 看到了使用dict来组织日志的配置文件,但是是基于django的,我想在我的系统里也有使用结构的配置方式,要自己写一个加载程序吗。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   956 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 22:45 · PVG 06:45 · LAX 15:45 · JFK 18:45
    ♥ Do have faith in what you're doing.