引导金字塔中日志设置的问题

时间:2011-11-24 07:09:12

标签: python pyramid

我正在尝试在金字塔中进行简单的文件记录配置,使用python 2.7.2和pyramid 1.2.3

它完全不符合我的预期。看起来即使我正在进入金字塔,它也没有加载正确的日志。 test.py中的具体问题如下:

test.py

from pyramid.paster import bootstrap
env = bootstrap('../test.ini')

import logging
logging.basicConfig()  #this apparently makes no difference
log=logging.getLogger('root') #same thing if I do getLogger(__name__) - it doesn't find the proj one.
print log.handlers
>>> []

这太奇怪了!没有处理程序?显然这不是我想的日志。

print log

>>> logging.Logger

不应该是RootLogger吗?

print log.parent

>>> [<logging.RootLogger>]

嗯所以getLogger('root')的父级是真正的RootLogger?让我们看看它的处理程序是什么:

print log.parent.handlers

>>> [<logging.StreamHandler>]

这很奇怪。

test.ini:

[app:proj]
use = egg:proj
sqlalchemy.url = mysql+mysqldb://proj_staging:pw@localhost/proj_staging?charset=utf8
# This apparently helps avoid mysql gone away errors
sqlalchemy.pool_recycle = 3600

[pipeline:main]
pipeline = proj

[loggers]
keys = root, proj

[handlers]
keys = console, file

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = file
qualname=root

[logger_proj]
level = DEBUG
handlers = 
qualname = proj

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[handler_file]
class = FileHandler
args=('../logs/proj.log','a')
formatter = generic
level = INFO

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

1 个答案:

答案 0 :(得分:9)

Pyramid在引导时不加载日志记录配置。如果您想进行论证,请在https://github.com/Pylons/pyramid/issues

打开一张票

无论如何,自己加载日志信息非常简单:

import logging
import logging.config
from pyramid.paster import bootstrap
logging.config.fileConfig('../test.ini')
env = bootstrap('../test.ini')

log = logging.getLogger(__name__)
log.debug('foo')