【问题标题】:Django: how to log every app to a separate fileDjango:如何将每个应用程序记录到单独的文件中
【发布时间】:2018-12-08 11:17:26
【问题描述】:

我的 django 日志定义在 settings.py 文件中,如下所示:

LOG_DIR = '/var/log/myapp/'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level':'DEBUG' if DEBUG else 'WARNING',
            'class':'logging.NullHandler',
        },
        'logfile': {
            'level':'DEBUG' if DEBUG else 'WARNING',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': LOG_DIR + "/application.log",
            'maxBytes': 1024 * 1024 * 10, #Max 10MB
            'backupCount': 3,
            'formatter': 'standard',
        },
        'console':{
            'level':'INFO',
            'class':'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers':['console'],
            'propagate': True,
            'level':'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG' if DEBUG else 'WARNING',
            'propagate': False,
        },
        '': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

现在,我在这个项目中有几个应用程序,我必须以一种简单的方式组织它们的日志记录,彼此创建一个单独的日志,我的意思是:

  • 项目一般日志
  • My_App1 日志
  • My_App2 日志
  • My_App3 日志

这可以通过 Django 轻松实现吗?

【问题讨论】:

  • 您将不得不添加更多指向单独处理程序的记录器,或者编写一个可以为每个应用程序使用单独文件的处理程序。

标签: python django logging


【解决方案1】:

在“记录器”下,您需要为每个应用添加一个条目:

'app1': {
            'handlers': ['app1'],
        },

同样在“处理程序”下:

'app1': {
        'level':'DEBUG' if DEBUG else 'WARNING',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': LOG_DIR + "/app1.log",
        'maxBytes': 1024 * 1024 * 10, #Max 10MB
        'backupCount': 3,
        'formatter': 'standard',
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2011-10-20
    • 2012-03-26
    • 2015-02-14
    • 2013-08-13
    • 2017-01-29
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多