手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

django配置log日志方法

时间:2024-10-13 07:33:53

1、如果放在前台执行,日志格式如下

django配置log日志方法

2、还有一种就是放在后尊怠哉砑台执行nohup python manage.py runserver 0.0.0.0:8081 &

django配置log日志方法

3、日志全部打在nohup.out文件中

django配置log日志方法

4、找到settings文件加入如下内容import timecur_path = os.path.dirname(os.p锾攒揉敫ath.realpath(__file__)) # log_path是存放日志的路径log_path = os.path.join(os.path.dirname(cur_path), 'logs')if not os.path.exists(log_path): os.mkdir(log_path) # 如果不存在这个logs文件夹,就自动创建一个LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { # 日志格式 'standard': { 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] ' '[%(levelname)s]- %(message)s'}, 'simple': { # 简单格式 'format': '%(levelname)s %(message)s' }, }, # 过滤 'filters': { }, # 定义具体处理日志的方式 'handlers': { # 默认记录所有日志 'default': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(log_path, 'all-{}.log'.format(time.strftime('%Y-%m-%d'))), 'maxBytes': 1024 * 1024 * 5, # 文件大小 'backupCount': 5, # 备份数 'formatter': 'standard', # 输出格式 'encoding': 'utf-8', # 设置默认编码,否则打印出来汉字乱码 }, # 输出错误日志 'error': { 'level': 'ERROR', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(log_path, 'error-{}.log'.format(time.strftime('%Y-%m-%d'))), 'maxBytes': 1024 * 1024 * 5, # 文件大小 'backupCount': 5, # 备份数 'formatter': 'standard', # 输出格式 'encoding': 'utf-8', # 设置默认编码 }, # 控制台输出 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard' }, # 输出info日志 'info': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(log_path, 'info-{}.log'.format(time.strftime('%Y-%m-%d'))), 'maxBytes': 1024 * 1024 * 5, 'backupCount': 5, 'formatter': 'standard', 'encoding': 'utf-8', # 设置默认编码 }, }, # 配置用哪几种 handlers 来处理日志 'loggers': { # 类型 为 django 处理所有类型的日志, 默认调用 'django': { 'handlers': ['default', 'console'], 'level': 'INFO', 'propagate': False }, # log 调用时需要当作参数传入 'log': { 'handlers': ['error', 'info', 'console', 'default'], 'level': 'INFO', 'propagate': True }, }}

django配置log日志方法

5、脚本中加入如下内容

django配置log日志方法

6、会生成log目录并产生如下日志文件

django配置log日志方法

7、日志输出成功

django配置log日志方法
© 手抄报圈