You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
import logging.config
import time
from app.conf.Setting import settings
# 日志格式
format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
log_dir = os.path.join(settings.BASE_PATH, settings.LOGS_DIR) # log文件的目录
log_name = time.strftime("%Y-%m-%d") + ".log" # log文件名
# log文件的全路径
logfile_path = os.path.join(log_dir, log_name)
# log配置字典
LOGGING_DICT = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': format
},
'simple': {
'format': format
},
},
'filters': {},
'handlers': {
# 打印到终端的日志
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'simple'
},
# 打印到文件的日志,收集info及以上的日志
'default': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard',
'filename': logfile_path, # 日志文件
'maxBytes': 1024 * 1024 * 20, # 日志大小 5M
'backupCount': 5,
'encoding': 'utf-8'
},
},
'loggers': {
'': {
# 这里把上面定义的两个handler都加上即log数据既写入文件又打印到屏幕
'handlers': ['default', 'console'],
'level': 'DEBUG',
'propagate': True, # 向上更高level的logger传递
},
},
}
logging.config.dictConfig(LOGGING_DICT)
# 导入上面定义的logging配置
logger = logging.getLogger()