flask.logger比更通用的python日志记录模块有什么优势? [英] What is the advantage of flask.logger over the more generic python logging module?

查看:1023
本文介绍了flask.logger比更通用的python日志记录模块有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask构建一个网站,现在我正在为它添加一些日志记录,我发现这些文档。基本的例子如下:

 如果不是app.debug:
导入日志$ b $ from themodule import TheHandlerYouWant
file_handler = TheHandlerYouWant(...)
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
pre>

之后,您可以使用 app.logger.error('发生错误')进行登录。这工作正常,但除了我没有看到比常规python日志记录模块有什么优势的事实,我也看到了一个主要的缺点:如果我想记录的请求上下文之外(例如运行一些cron作业的代码)我得到错误,因为我在请求上下文之外使用应用程序。



所以我的主要问题;为什么我会使用Flask记录器?它是有史以来建立的原因是什么?

解决方案Flask记录器使用通用Python日志记录,它只是一个 logging.getLogger(name)和其他的一样。

记录器存在,所以Flask可以记录内部模块的工作。例如,它将在调试模式下记录500次错误的回溯。该配置示例显示如何在不处于调试模式时启用这些在生产中仍然有用的日志。

拥有一个内部记录器对于Flask来说并不是唯一的,它是使用Python记录的标准方式。每个模块都定义了自己的记录器,但记录的配置只能由链中的最后一个链接处理:你的项目。



不是说你应该自己使用记录器。虽然使用它是有效的,但是最好通过为自己的消息创建一个单独的记录器来获得更好的服务。


I'm building a website using Flask and I'm now in the process of adding some logging to it for which I found these docs. The basic example is as follows:

if not app.debug:
    import logging
    from themodule import TheHandlerYouWant
    file_handler = TheHandlerYouWant(...)
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)

after which you can log using app.logger.error('An error occurred'). This works fine, but apart from the fact that I do not see any advantage over the regular python logging module I also see a major downside: if I want to log outside of a request context (when for example running some code with a cron job) I get errors because I'm using app outside of the request context.

So my main question; why would I use the Flask logger at all? What is the reason that it was ever built?

解决方案

The Flask logger uses the "generic" Python logging, it's just a logging.getLogger(name) like any other.

The logger exists so that Flask can log the internal workings of the module. For example, it will log tracebacks on 500 errors during debug mode. The configuration example is there to show how to enable these logs, which are still useful in production, when you are not in debug mode.

Having an internal logger is not unique to Flask, it's the standard way to use logging in Python. Every module defines it's own logger, but the configuration of the logging is only handled by the last link in the chain: your project.

It's not an indication that you should use the logger yourself. Although it's valid to use it, you'd probably be better served by creating a separate logger for your own messages.

这篇关于flask.logger比更通用的python日志记录模块有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆