Django 中使用 logging 模块记录系统日志

原文地址:https://docs.djangoproject.com/en/1.4/topics/logging/

说明:本文针对1.4版本的 Django 框架中 logging 模块进行翻译,并非全文翻译,不过笔者除将使用过程中感到比较重要的部分翻译出来外,会尽力多翻译一些。如文中有不当之处,请大家多多提意见。

Logging

New in Django 1.3:  Please see the release notes

A quick logging primer

Django uses Python’s builtin logging module to perform system logging. The usage of this module is discussed in detail in Python’s own documentation. However, if you’ve never used Python’s logging framework (or even if you have), here’s a quick primer.

Django 使用 Python 内置的 logging 模块实现系统日志。关于该模块的详细讨论可以参考 Python 的官方文档。本文对于没有接触过 Python 日志框架的用户提供快速引到使用。

The cast of players

A Python logging configuration consists of four parts:

Loggers

A logger is the entry point into the logging system. Each logger is a named bucket to which messages can be written for processing.

A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:

  • DEBUG: Low level system information for debugging purposes
  • INFO: General system information
  • WARNING: Information describing a minor problem that has occurred.
  • ERROR: Information describing a major problem that has occurred.
  • CRITICAL: Information describing a critical problem that has occurred.

Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.

When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.

Once a logger has determined that a message needs to be processed, it is passed to a Handler.

logger 是进入日志系统的入口,每个 logger 都是带有名字的通道,为线程中消息的写入服务。

logger 规定是有日志级别的。这一日志级别说明 logger 要处理的消息的严重性。Python 定义了如下日志级别:DEBUG, INFO, WARNING, ERROR, CRITICAL。

每一个写到 logger 中的消息称为日志记录。每一个日志记录同样有日志级别。一个日志记录通同样可以包含有用的元信息来描述被记录的事件。甚至包括 stack trace 或 error code 等信息。

当消息送到 logger 中时,其日志级别会与 logger 的日志级别进行比对。如果消息的级别等于或超过 logger 本身的级别,那么消息将会进入下一步处理。若不是,消息将被忽略。

logger 决定一个消息需要被处理之后,它会将消息发送给 Handler。

Handlers

The handler is the engine that determines what happens to each message in a logger. It describes a particular logging behavior, such as writing a message to the screen, to a file, or to a network socket.

Like loggers, handlers also have a log level. If the log level of a log record doesn’t meet or exceed the level of the handler, the handler will ignore the message.

A logger can have multiple handlers, and each handler can have a different log level. In this way, it is possible to provide different forms of notification depending on the importance of a message. For example, you could install one handler that forwards ERROR and CRITICAL messages to a paging service, while a second handler logs all messages (including ERROR and CRITICAL messages) to a file for later analysis.

handler 是决定 logger 中每个消息需要如何处理的引擎。它描述特定的记录行为,比如把消息打印到屏幕,输出到文件,或者通过网络套接字发送。

与 logger 一样,handler 也用过有日志级别。如果一个日志记录的日志级别不等于或不大于 handler 的级别,handler 会忽略此消息。

一个 logger 可以拥有多个 handler,每个 handler 可以有(与其它 handler )不同的日志级别。这样,便可以依据消息重要程度的不同来提供不同的通知形式。例如,你可以设置一个 handler 来转发 ERROR 和 CRITICAL 级别的消息到页面服务,而另一个 handler 记录所有的信息(包括 ERROR 和 CRITICAL)到文件用于分析。

Filters

A filter is used to provide additional control over which log records are passed from logger to handler.

By default, any log message that meets log level requirements will be handled. However, by installing a filter, you can place additional criteria on the logging process. For example, you could install a filter that only allows ERROR messages from a particular source to be emitted.

Filters can also be used to modify the logging record prior to being emitted. For example, you could write a filter that downgrades ERROR log records to WARNING records if a particular set of criteria are met.

Filters can be installed on loggers or on handlers; multiple filters can be used in a chain to perform multiple filtering actions.

filter 用来对那些从 logger 传至 hadnler 的日志记录提供额外的控制。

默认任何日志消息其级别等于或大于要求日志级别的都会被处理。不过,通过使用 filter,可以添加其它条件来处理日志记录。例如,可以设置一个 filter 来过滤只允许特定源头发来的 ERROR 消息。

Filter 可以用来修改一个日志记录优先被发出。例如,可以写一个 filter 把符合某中条件的日志记录从 ERROR 级别降级为 WARNING级别。

Filter 可以被设置到 logger 或 handler 上,多个 filter 可以组成链式过滤实现多种过滤方式。

Formatters

Ultimately, a log record needs to be rendered as text. Formatters describe the exact format of that text. A formatter usually consists of a Python formatting string; however, you can also write custom formatters to implement specific formatting behavior.

最终一个日志记录会被以文本方式渲染。Forrmatter 描述了文本的格式。一个 formatter 通常包含 Python 格式化字符串;当然,也可也自定义一个 formatter 来实现特定的格式化方式。

Using logging

Once you have configured your loggers, handlers, filters and formatters, you need to place logging calls into your code. Using the logging framework is very simple. Here’s an example:

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

And that’s it! Every time the bad_mojo condition is activated, an error log record will be written.

Naming loggers

The call to logging.getLogger() obtains (creating, if necessary) an instance of a logger. The logger instance is identified by a name. This name is used to identify the logger for configuration purposes.

By convention, the logger name is usually __name__, the name of the python module that contains the logger. This allows you to filter and handle logging calls on a per-module basis. However, if you have some other way of organizing your logging messages, you can provide any dot-separated name to identify your logger:

# Get an instance of a specific named logger
logger = logging.getLogger('project.interesting.stuff')

The dotted paths of logger names define a hierarchy. The project.interesting logger is considered to be a parent of the project.interesting.stuff logger; the project logger is a parent of the project.interesting logger.

Why is the hierarchy important? Well, because loggers can be set to propagate their logging calls to their parents. In this way, you can define a single set of handlers at the root of a logger tree, and capture all logging calls in the subtree of loggers. A logging handler defined in the project namespace will catch all logging messages issued on the project.interesting andproject.interesting.stuff loggers.

This propagation can be controlled on a per-logger basis. If you don’t want a particular logger to propagate to it’s parents, you can turn off this behavior.

Making logging calls

The logger instance contains an entry method for each of the default log levels:

  • logger.critical()
  • logger.error()
  • logger.warning()
  • logger.info()
  • logger.debug()

There are two other logging calls available:

  • logger.log(): Manually emits a logging message with a specific log level.
  • logger.exception(): Creates an ERROR level logging message wrapping the current exception stack frame.

Configuring logging

Of course, it isn’t enough to just put logging calls into your code. You also need to configure the loggers, handlers, filters and formatters to ensure that logging output is output in a useful way.

Python’s logging library provides several techniques to configure logging, ranging from a programmatic interface to configuration files. By default, Django uses the dictConfig format.

Note

logging.dictConfig is a builtin library in Python 2.7. In order to make this library available for users of earlier Python versions, Django includes a copy as part of django.utils.log. If you have Python 2.7, the system native library will be used; if you have Python 2.6 or earlier, Django’s copy will be used.

In order to configure logging, you use LOGGING to define a dictionary of logging settings. These settings describes the loggers, handlers, filters and formatters that you want in your logging setup, and the log levels and other properties that you want those components to have.

Logging is configured as soon as settings have been loaded (either manually using configure() or when at least one setting is accessed). Since the loading of settings is one of the first things that Django does, you can be certain that loggers are always ready for use in your project code.

仅仅在代码中调用日志模块还不够,我们需要定义 loggers, hadnlers, filters 和 formatters 来保证日志记录输出。

Python 自己的 logging 库提供了很多方式来定义 logging ,从程序接口到配置文件。默认 Django 使用 dictConfig 格式。

为了定义 logging,需要使用 LOGGING 来定义一个包含设置信息的字典。这些设置包含了 loggers, handlers, filters 和 formaters 以及各自各个组件的日志级别等所有想要包含的信息。

Logging 同 settings 一起被载入。由于载入 settings 设置是 Django 一开始就要做的事情,可以保证在项目的各个部分都可以使用配置好的日志模块。

An example

The full documentation for dictConfig format is the best source of information about logging configuration dictionaries. However, to give you a taste of what is possible, here is an example of a fairly complex logging setup, configured using logging.dictConfig():

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'special': {
            '()': 'project.logging.SpecialFilter',
            'foo': 'bar',
        }
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['special']
        }
    },
    'loggers': {
        'django': {
            'handlers':['null'],
            'propagate': True,
            'level':'INFO',
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'myproject.custom': {
            'handlers': ['console', 'mail_admins'],
            'level': 'INFO',
            'filters': ['special']
        }
    }
}

This logging configuration does the following things:

  • Identifies the configuration as being in ‘dictConfig version 1’ format. At present, this is the only dictConfig format version.

  • Disables all existing logging configurations.

  • Defines two formatters:

    • simple, that just outputs the log level name (e.g., DEBUG) and the log message.

      The format string is a normal Python formatting string describing the details that are to be output on each logging line. The full list of detail that can be output can be found in the formatter documentation.

    • verbose, that outputs the log level name, the log message, plus the time, process, thread and module that generate the log message.

  • Defines one filter – project.logging.SpecialFilter, using the alias special. If this filter required additional arguments at time of construction, they can be provided as additional keys in the filter configuration dictionary. In this case, the argument foo will be given a value of bar when instantiating the SpecialFilter.

  • Defines three handlers:

    • null, a NullHandler, which will pass any DEBUG (or higher) message to /dev/null.
    • console, a StreamHandler, which will print any DEBUG (or higher) message to stderr. This handler uses the simple output format.
    • mail_admins, an AdminEmailHandler, which will email any ERROR (or higher) message to the site admins. This handler uses thespecial filter.
  • Configures three loggers:

    • django, which passes all messages at INFO or higher to the null handler.
    • django.request, which passes all ERROR messages to the mail_admins handler. In addition, this logger is marked to notpropagate messages. This means that log messages written to django.request will not be handled by the django logger.
    • myproject.custom, which passes all messages at INFO or higher that also pass the special filter to two handlers – the console, and mail_admins. This means that all INFO level messages (or higher) will be printed to the console; ERROR and CRITICALmessages will also be output via email.

Custom handlers and circular imports

If your settings.py specifies a custom handler class and the file defining that class also imports settings.py a circular import will occur.

For example, if settings.py contains the following config for LOGGING:

LOGGING = {
  'version': 1,
  'handlers': {
    'custom_handler': {
      'level': 'INFO',
      'class': 'myproject.logconfig.MyHandler',
    }
  }
}

and myproject/logconfig.py has the following line before the MyHandler definition:

from django.conf import settings

then the dictconfig module will raise an exception like the following:

ValueError: Unable to configure handler 'custom_handler':
Unable to configure handler 'custom_handler':
'module' object has no attribute 'logconfig'

Custom logging configuration

If you don’t want to use Python’s dictConfig format to configure your logger, you can specify your own configuration scheme.

The LOGGING_CONFIG setting defines the callable that will be used to configure Django’s loggers. By default, it points at Python’s logging.dictConfig() method. However, if you want to use a different configuration process, you can use any other callable that takes a single argument. The contents of LOGGING will be provided as the value of that argument when logging is configured.

Disabling logging configuration

If you don’t want to configure logging at all (or you want to manually configure logging using your own approach), you can set LOGGING_CONFIG to None. This will disable the configuration process.

Note

Setting LOGGING_CONFIG to None only means that the configuration process is disabled, not logging itself. If you disable the configuration process, Django will still make logging calls, falling back to whatever default logging behavior is defined.

Django’s logging extensions

Django provides a number of utilities to handle the unique requirements of logging in Web server environment.

Django 提供了一些公共的日志记录方式来处理在 Web 服务器环境中特别的需求。

Loggers

Django provides three built-in loggers.

Django 内部提供了三个 logger。

django

django is the catch-all logger. No messages are posted directly to this logger.

django.request

Log messages related to the handling of requests. 5XX responses are raised as ERROR messages; 4XX responses are raised as WARNING messages.

Messages to this logger have the following extra context:

  • status_code: The HTTP response code associated with the request.
  • request: The request object that generated the logging message.
django.db.backends

Messages relating to the interaction of code with the database. For example, every SQL statement executed by a request is logged at the DEBUG level to this logger.

Messages to this logger have the following extra context:

  • duration: The time taken to execute the SQL statement.
  • sql: The SQL statement that was executed.
  • params: The parameters that were used in the SQL call.

For performance reasons, SQL logging is only enabled when settings.DEBUG is set to True, regardless of the logging level or handlers that are installed.

Django 设置了三个内置的 loggers。

django 是用来捕捉所有输出的记录器。没有消息会直接发送到这个记录器上。

django.request 记录与 request 请求处理相关的消息。5XX 的返回会触发 ERROR 消息的记录;4XX 的返回会触发 WARNING 消息的记录。这一记录器的拥有下面两个额外内容:status_code 和 request。

django.db.backends 记录代码中与数据库交互有关的消息。比如,每个 request 请求触发的 SQL 语句执行时都会被以 DEBUG 级别的消息交给 logger 处理。这一记录器拥有下面的额外内容:duration, sql, params.

出于性能的考虑,SQL 记录器只有在 settings.DEBUG 设置为 True 时才有效,此时不管日志级别的任何设置。

Handlers

Django provides one log handler in addition to those provided by the Python logging module.

Django 额外提供了一个 handler 来对应 Python logging 模块。

class  AdminEmailHandler( [ include_html=False ])

This handler sends an email to the site admins for each log message it receives.

If the log record contains a request attribute, the full details of the request will be included in the email.

If the log record contains stack trace information, that stack trace will be included in the email.

The include_html argument of AdminEmailHandler is used to control whether the traceback email includes an HTML attachment containing the full content of the debug Web page that would have been produced if DEBUG were True. To set this value in your configuration, include it in the handler definition for django.utils.log.AdminEmailHandler, like this:

'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'class': 'django.utils.log.AdminEmailHandler',
        'include_html': True,
    }
},

Note that this HTML version of the email contains a full traceback, with names and values of local variables at each level of the stack, plus the values of your Django settings. This information is potentially very sensitive, and you may not want to send it over email. Consider using something such as django-sentry to get the best of both worlds – the rich information of full tracebacks plus the security of not sending the information over email. You may also explicitly designate certain sensitive information to be filtered out of error reports – learn more on Filtering error reports.

Django 为 python 的 logging 模块提供了一个 handler。这个 handler 对所有日志消息的处理方式为向管理员发送一封邮件。如果日志记录中包含 request 属性,邮件中将包含 request 请求的全部详细信息。如果日志记录中包含堆栈追踪信息,那么邮件中将发送堆栈追踪信息。如果 DEBUG 设置为 True,这一 handler 的 include_html 参数用来控制邮件中是否包含 HTML ,以包含完整的 Web 页面 debug 内容。如果要在设置中使用这个值,需要在 handler 的定义中包含 django.utils.log.AdminEmailHandler,如代码中所示。

注意邮件的 HTML 中有全部的回溯信息,包含了各种级别的变量名称和值,加上 Django settings 的信息。这里可能有非常敏感的信息,你可能并不希望通过邮件发送它。

Filters

Django provides two log filters in addition to those provided by the Python logging module.

Django 也提供了两个 filter 。

class  CallbackFilter( callback)
New in Django 1.4:  Please see the release notes

This filter accepts a callback function (which should accept a single argument, the record to be logged), and calls it for each record that passes through the filter. Handling of that record will not proceed if the callback returns False.

For instance, to filter out UnreadablePostError (raised when a user cancels an upload) from the admin emails, you would create a filter function:

from django.http import UnreadablePostError

def skip_unreadable_post(record):
    if record.exc_info:
        exc_type, exc_value = record.exc_info[:2]
        if isinstance(exc_value, UnreadablePostError):
            return False
    return True

and then add it to your logging config:

'filters': {
    'skip_unreadable_posts': {
        '()': 'django.utils.log.CallbackFilter',
        'callback': skip_unreadable_post,
    }
},
'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'filters': ['skip_unreadable_posts'],
        'class': 'django.utils.log.AdminEmailHandler'
    }
},
这个过滤器接受回调函数(所以需要一个参数,即需要记录的日志),每个通过过滤器的记录都会调用它。如果函数返回 False,那么记录的 handler 不会处理它。 class  RequireDebugFalse
New in Django 1.4:  Please see the release notes

This filter will only pass on records when settings.DEBUG is False.

This filter is used as follows in the default LOGGING configuration to ensure that the AdminEmailHandler only sends error emails to admins when DEBUG is False:

'filters': {
     'require_debug_false': {
         '()': 'django.utils.log.RequireDebugFalse',
     }
 },
 'handlers': {
     'mail_admins': {
         'level': 'ERROR',
         'filters': ['require_debug_false'],
         'class': 'django.utils.log.AdminEmailHandler'
     }
 },

这个过滤器的作用是仅在 settlings.DEBUG 值设置为 False 的情况下允许记录发送。这个过滤器通常如下面例子中的用法,在默认 LOGGING 设置中保证 AdminEmailHandler 仅在 DEBUG 为 False 时才发送邮件。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值