python logging
# coonfig 版本 # create config BASE_DIR = os.path.dirname(__file__) BASE_LOG_DIR = os.path.join(BASE_DIR, "log") if not os.path.exists(BASE_LOG_DIR): os.mkdir(BASE_LOG_DIR) log_config = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { 'django': { 'handlers': ['TimedRotatingFileHandler'], 'level': 'DEBUG', "propagate": True } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', "level": "DEBUG", "formatter": "simple" }, 'filehander': { 'class': 'logging.FileHandler', 'filename': 'log.log', "level": "WARNING", "formatter": "verbose" }, 'RotatingFileHandler': { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'ERROR', 'filename': os.path.join(BASE_LOG_DIR, "project_error.log"), # 日志文件 'maxBytes': 1024 * 1024 * 1024 * 1, # 1GB 'backupCount': 3, # 备份数为3 xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3 'formatter': 'verbose', 'encoding': 'utf-8', }, # 备份代码出错的地方 'TimedRotatingFileHandler': { 'class': 'logging.handlers.TimedRotatingFileHandler', 'level': 'INFO', 'filename': os.path.join(BASE_LOG_DIR, "project_info.log"), # 日志文件 'backupCount': 30, # 备份数为 xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3 'when': 'D', 'formatter': 'simple', 'encoding': 'utf-8', }, # 备份日常的信息 }, 'formatters': { 'verbose': { 'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' '[%(levelname)s][%(message)s]', 'datefmt': '%Y-%m-%d %H:%M:%S' }, 'simple': { 'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' } } } logging.config.dictConfig(log_config) # create logger logger = logging.getLogger('django') # 'application' code logger.debug('debug message') logger.info('info message') logger.warning('warn message') logger.error('error message') logger.critical('critical message')
#DJANGO BASE_LOG_DIR = os.path.join(BASE_DIR, "log") if not os.path.exists(BASE_LOG_DIR): os.mkdir(BASE_LOG_DIR) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { 'django.request': { # 所有的请求信息 'handlers': ['console'], 'level': 'ERROR', 'propagate': False, }, 'django.db.backends': { 'handlers': ['RotatingFileHandler'], 'level': 'ERROR', 'propagate': False, } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', "level": "DEBUG", "formatter": "simple" }, 'filehander': { 'class': 'logging.FileHandler', 'filename': 'log.log', "level": "WARNING", "formatter": "verbose" }, 'RotatingFileHandler': { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'ERROR', 'filename': os.path.join(BASE_LOG_DIR, "project_error.log"), # 日志文件 'maxBytes': 1024 * 1024 * 1024 * 1, # 1GB 'backupCount': 3, # 备份数为3 xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3 'formatter': 'verbose', 'encoding': 'utf-8', }, # 备份代码出错的地方 'TimedRotatingFileHandler': { 'class': 'logging.handlers.TimedRotatingFileHandler', 'level': 'INFO', 'filename': os.path.join(BASE_LOG_DIR, "project_info.log"), # 日志文件 'backupCount': 30, # 备份数为 xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3 'when': 'D', 'formatter': 'simple', 'encoding': 'utf-8', }, # 备份日常的信息 }, 'formatters': { 'verbose': { 'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' '[%(levelname)s][%(message)s]', 'datefmt': '%Y-%m-%d %H:%M:%S' }, 'simple': { 'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' } } }