Django:仅记录我项目的应用程序 [英] Django: logging only for my project's apps

查看:59
本文介绍了Django:仅记录我项目的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,我可以通过创建将捕获所有日志的记录器" 启用在 LOGGING 配置中登录settings.py的功能.但是,如果我只想查看项目应用程序的日志记录而不是Django内部记录怎么办?

By default I can enable logging in settings.py in the LOGGING configuration by creating a logger "" which will catch all logs. But what if I only want to see logging from my project's apps as opposed to Django internals?

我可以想象在我的每个Django应用模块中显式地创建一个记录器,并按照某种约定对其进行命名,例如 logging.getLogger("myproject." + __file __).然后,我可以创建一个名为"myproject"的记录器(在SETTINGS中),将所有这些记录器输出.我不希望对项目名称进行硬编码,因此我会在 ___ file ___ 上执行一些os.path逻辑,以将任意名称的完整名称空间提取到文件中.

I can imagine explicitly getting a logger in each of my Django app modules and naming it by some convention, e.g. logging.getLogger("myproject." + __file__). Then I can create a logger called 'myproject' (in SETTINGS) which picks up all of these to output. I'd prefer not to hardcode my project name, so I'd do some os.path logic on ___file___ to extract the full namespace up to the file at any arbitrary depth.

在这一点上,我停下来想知道还有没有更简单的方法?

At this point, I stop and wonder is there an easier way?

推荐答案

不确定我是否完全理解您的问题,因为答案似乎太简单了.

Not sure if I fully understood your question, because the answer seems too simple.

假设您已在 LOGGING 中定义了项目应用程序的处理程序,例如:

Assuming you have defined in LOGGING a handler for your project's apps, for example like this:

'handlers': {
    'handler_for_my_apps': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': 'debug.log',
    },

并给定您的应用程序 app1 app2 等,因此,通过定义记录器,您可以获取这些应用程序中的所有日志,而无需Django的内部日志:

and given your apps app1, app2, and so, you could have all the logs from those apps without any Django's internal logs by defining the loggers:

'loggers': {
    'app1': {
        'handlers': ['handler_for_my_apps'],
        'level': 'DEBUG',
    },
    'app2': {
        'handlers': ['handler_for_my_apps'],
        'level': 'DEBUG',
    },

在同一文件中将没有Django日志,除非您当然定义了带有处理程序 handler_for_my_apps 的名为 django 的记录器.

There will be no Django logs in the same file, unless of course you defined a logger named django with a handler handler_for_my_apps.

在您的应用中,您可以按照文档建议使用 logging.getLogger(__ name __)获取记录器.

In your apps you can get the logger using logging.getLogger(__name__) as recommended by the docs.

除非我误解了你的问题...

Unless I misunderstood your question...

这篇关于Django:仅记录我项目的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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