金字塔日志记录
我有一个金字塔应用程序,我希望日志到达 stderr 和 stdout。标准输出应为“INFO”级别及以下。 stderr 应该是“WARN”或更高。我将如何更改我的 .ini 文件来执行此操作?
目前我正在这样记录,这被认为是正确的方法吗?
log = logger.getLogger(__name__) log.info("update ...") log.error("MAYDAY MAYDAY... BOOM!!!")
目前我正在使用默认日志记录,就是这样。
[loggers] keys = root, app [handlers] keys = console [formatters] keys = generic [logger_root] level = WARN handlers = console [logger_app] level = WARN handlers = qualname = app [handler_console] class = StreamHandler args = (sys.stderr,) 85 level = NOTSET formatter = generic [formatter_generic] format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
I have a pyramid application and I want the logs to got to stderr and stdout. stdout should be "INFO" level and below. stderr should be "WARN" and higher. How would I change my .ini file to do this?
Currently I am logging like this, is this considered the correct way?
log = logger.getLogger(__name__) log.info("update ...") log.error("MAYDAY MAYDAY... BOOM!!!")
Currently I am using the default logging, which is this.
[loggers] keys = root, app [handlers] keys = console [formatters] keys = generic [logger_root] level = WARN handlers = console [logger_app] level = WARN handlers = qualname = app [handler_console] class = StreamHandler args = (sys.stderr,) 85 level = NOTSET formatter = generic [formatter_generic] format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将多个处理程序添加到根,以逗号分隔。如果您想在正常的“仅接受高于此日志记录级别的消息”标准(即仅调试消息)之外进行过滤,那么您需要使用日志过滤器之类的东西来根据特定级别接受/拒绝记录:
http://docs.python.org/library/logging.html#filter- object
您当前使用
log =logging.getLogger(__name__)
进行日志记录的方法是完全有效的,并且是组织日志记录层次结构的便捷方法。You can add multiple handlers to the root, comma-delimited. If you want to filter outside of the normal "only accept messages above this logging level" criterion (i.e. only debug messages) then you need to use something like a logging filter to accept/reject records based on their specific levels:
http://docs.python.org/library/logging.html#filter-objects
Your current method of logging using
log = logging.getLogger(__name__)
is perfectly valid and a convenient way to organize the logging hierarchy.