避免使用logger = logging.getLogger(__ name__) [英] Avoid `logger=logging.getLogger(__name__)`

查看:5082
本文介绍了避免使用logger = logging.getLogger(__ name__)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们按照Django文档告诉我们的那样设置日志记录:

We set up logging like the django docs told us:

https://docs.djangoproject.com/zh/2.1 /topics/logging/#using-logging

# import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

def my_view(request, arg1, arg):
    ...
    if bad_mojo:
        # Log an error message
        logger.error('Something went wrong!')

我想避免在每个要记录的Python文件中使用这一行:

I want to avoid this line in every Python file which wants to log:

logger = logging.getLogger(__name__)

我想简单点:

logging.error('Something went wrong!')

但是我们要保留一个功能:我们想在日志输出中看到Python文件名.

But we want to keep one feature: We want to see the Python file name in the logging output.

到目前为止,我们使用以下格式:

Up to now we use this format:

'%(asctime)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s'

示例输出:

2016-01-11 12:12:31 myapp.foo +68: ERROR Something went wrong

如何避免logger = logging.getLogger(__name__)?

推荐答案

您可以使用logging.basicConfig定义通过logging可用的默认接口,如下所示:

You can use logging.basicConfig to define the default interface available through logging as follows:

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s',
                    )

现在,只要您在应用程序中的任何位置执行以下操作,都将使用此定义:

This definition will now be used whenever you do the following anywhere in your application:

import logging
logging.error(...)

虽然__name__不可用,但等效的(和其他选项)可通过默认的 LogRecord属性可用于错误字符串格式设置-包括modulefilenamepathname.以下是此操作的两个脚本演示:

While __name__ is not available, the equivalent (and other options) are available through the default LogRecord attributes that can be used for error string formatting - including module, filename and pathname. The following is a two-script demonstration of this in action:

scripta.py

scripta.py

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(module)s %(name)s.%(funcName)s +%(lineno)s: %(levelname)-8s [%(process)d] %(message)s',
                    )

from scriptb import my_view

my_view()

scriptb.py

scriptb.py

import logging

def my_view():
    # Log an error message
    logging.error('Something went wrong!')

scripta.py中使用添加的module参数定义了日志记录定义.在scriptb.py中,我们只需要导入logging即可访问此定义的默认值.运行scripta.py时,将生成以下输出:

The logging definition is defined in scripta.py, with the added module parameter. In scriptb.py we simply need to import logging to get access to this defined default. When running scripta.py the following output is generated:

2016-01-14 13:22:24,640 scriptb root.my_view +9: ERROR    [14144] Something went wrong!

其中显示了记录错误的模块(scriptb).

Which shows the module (scriptb) where the logging of the error occurs.

根据此答案,您可以通过关闭Django来继续使用从Django进行日志记录的每个模块配置如下处理和设置根处理程序:

According to this answer you can continue to use any per-module configuration of logging from Django, by turning off Django handling and setting up the root handler as follows:

# settings.py - django config
LOGGING_CONFIG = None # disables Django handling of logging
LOGGING = {...}  # your standard Django logging configuration

import logging.config
logging.config.dictConfig(LOGGING)

这篇关于避免使用logger = logging.getLogger(__ name__)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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