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

发布于 2024-12-17 13:28:28 字数 1609 浏览 0 评论 0原文

我正在尝试使用 python 2.7.2 和金字塔 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>]

这很奇怪。

测试.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

I am trying to do simple file logging config in pyramid, with python 2.7.2 and pyramid 1.2.3

It isn't working at all how I expected. It looks like even though i'm bootstrapping into pyramid, it isn't loading the right logs. Specific problems below within 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
>>> []

That's weird! no handlers? Apparently it's not the log I am thinking of.

print log

>>> logging.Logger

shouldn't it be a RootLogger?

print log.parent

>>> [<logging.RootLogger>]

hmm so the parent of getLogger('root') is the real RootLogger? let's see what its handlers are:

print log.parent.handlers

>>> [<logging.StreamHandler>]

that's weird.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-12-24 13:28:28

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')

Pyramid doesn't load the logging configuration when bootstrapping. If you want to make an argument that it should, please open a ticket at https://github.com/Pylons/pyramid/issues

Regardless, it's pretty simple to load the logging information yourself:

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文