Django logging構成
4076 ワード
Djangoでのloggerログ構成
1.settings.py
転載先:https://www.cnblogs.com/Guishuzhe/p/9607809.html
1.settings.py
#
BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
'[%(levelname)s][%(message)s]'
},
'simple': {
'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
},
'collect': {
'format': '%(message)s'
}
},
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'filters': ['require_debug_true'], # Django debug True
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'SF': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler', # ,
'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"), #
'maxBytes': 1024 * 1024 * 50, # 50M
'backupCount': 3, # 3 xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3
'formatter': 'standard',
'encoding': 'utf-8',
},
'TF': {
'level': 'INFO',
'class': 'logging.handlers.TimedRotatingFileHandler', # ,
'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"), #
'backupCount': 3, # 3 xx.log --> xx.log.2018-08-23_00-00-00 --> xx.log.2018-08-24_00-00-00 --> ...
'when': 'D', # , S/ M/ H/ D/ W0-W6/ (0= ) midnight/
'formatter': 'standard',
'encoding': 'utf-8',
},
'error': {
'level': 'ERROR',
'class': 'logging.handlers.RotatingFileHandler', # ,
'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"), #
'maxBytes': 1024 * 1024 * 5, # 50M
'backupCount': 5,
'formatter': 'standard',
'encoding': 'utf-8',
},
'collect': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler', # ,
'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
'maxBytes': 1024 * 1024 * 50, # 50M
'backupCount': 5,
'formatter': 'collect',
'encoding': "utf-8"
}
},
'loggers': {
'': { # logger
'handlers': ['SF', 'console', 'error'], # 'console'
'level': 'DEBUG',
'propagate': True,
},
'collect': { # 'collect' logger
'handlers': ['console', 'collect'],
'level': 'INFO',
}
},
}
2.views.py
import logging
# logger ,
logger = logging.getLogger(__name__)
転載先:https://www.cnblogs.com/Guishuzhe/p/9607809.html