django根据不同app配置相应的log文件

django根据不同app配置相应的log文件

settings.py

# django logging
LOG_PATH = "/var/log/blog/"
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '[%(levelname)s] %(asctime)s [%(filename)s:%(lineno)d] %(message)s'
        },
    },
    'handlers': {
        'app01': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'standard',
            'filename': '%s/app01.log' % LOG_PATH,
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 3,
        },
        'app02': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'standard',
            'filename': '%s/app02.log' % LOG_PATH,
            'maxBytes': 1024 * 1024 * 10,
            'backupCount': 3,
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard',
        }
    },
    'loggers': {
        'app01': {
            'handlers': ['app01', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'app02': {
            'handlers': ['app02', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

在app01的views.py中进行调用

import logging
log = logging.getLogger("app01")

log.error('Hello')

在/var/log/blog/app01.log中就会有字符串'Hello'打印出来。

猜你喜欢

转载自www.cnblogs.com/guotianbao/p/10132542.html