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
whathefoxsay
V2EX  ›  Python

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

  •  
  •   whathefoxsay · 2015-05-04 12:05:06 +08:00 · 3855 次点击
    这是一个创建于 3287 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的方式:
    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 条回复    2015-05-04 18:24:50 +08:00
    hhkbp2
        1
    hhkbp2  
       2015-05-04 12:27:17 +08:00
    写个函数把要变化的参数抽取出来,是不是就可以了
    lyief
        2
    lyief  
       2015-05-04 13:05:14 +08:00
    建议把logger的配置写成文件,“易变参数写成配置文件”是我觉得很优雅的工程实践。
    des
        3
    des  
       2015-05-04 13:21:43 +08:00 via Android
    楼主混知乎的吧?
    liboyue
        4
    liboyue  
       2015-05-04 13:34:15 +08:00
    优雅的 => 优雅地
    goodryb
        5
    goodryb  
       2015-05-04 13:36:42 +08:00
    logging模块是可以通过配置文件来初始化,这样代码基本可以保持不动
    有需求改配置文件就可以了
    whathefoxsay
        7
    whathefoxsay  
    OP
       2015-05-04 18:24:50 +08:00
    @staticor 看到了使用dict来组织日志的配置文件,但是是基于django的,我想在我的系统里也有使用结构的配置方式,要自己写一个加载程序吗。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   767 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 22:00 · PVG 06:00 · LAX 15:00 · JFK 18:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.