想要用django的logging日志功能,获取对数据库的操作日志及捕获异常该怎么做。(官方的日志拓展功能包含了这部分,但如何使用并没看明白)
官方文档:https://docs.djangoproject.com/zh-hans/3.2/topics/logging/
想要用django的logging日志功能,获取对数据库的操作日志及捕获异常该怎么做。(官方的日志拓展功能包含了这部分,但如何使用并没看明白)
官方文档:https://docs.djangoproject.com/zh-hans/3.2/topics/logging/
下面是存储django日志的一个配置,你放置在setting.py即可,文件夹不用创建,会自动创建,你也可以指定日志文件夹
import time
cur_path = os.path.dirname(os.path.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'},
# 'myconsole': {
# 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s]- %(message)s'},
# 'simple': { # 简单格式
# 'format': '%(levelname)s %(message)s'
# },
# },
# # 过滤
# 'filters': {
# 'require_debug_false': {
# '()': 'django.utils.log.RequireDebugFalse',
# },
# 'require_debug_true': {
# '()': 'django.utils.log.RequireDebugTrue',
# },
# },
# # 定义具体处理日志的方式
# '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',
# 'filters': ['require_debug_true'],
# 'class': 'logging.StreamHandler',
# 'stream': 'ext://sys.stdout',
# # 'stream': open(os.path.join(log_path, 'print-{}.log'.format(time.strftime('%Y-%m-%d'))), 'a'),
# # 虽然成功了,但是并没有将所有内容全部写入文件,目前还不清楚为什么
# 'formatter': 'standard', # 制定输出的格式,注意 在上面的formatters配置里面选择一个,否则会报错
# # 'encoding': 'utf-8', # 设置默认编码
# },
# # 输出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', # 设置默认编码
# }, # 输出警告日志
# 'warning': {
# 'level': 'WARNING',
# 'class': 'logging.handlers.RotatingFileHandler',
# 'filename': os.path.join(log_path, 'warning-{}.log'.format(time.strftime('%Y-%m-%d'))),
# 'maxBytes': 1024 * 1024 * 5,
# 'backupCount': 5,
# 'formatter': 'standard',
# 'encoding': 'utf-8', # 设置默认编码
# }, # 输出严重错误日志
# 'critical': {
# 'level': 'CRITICAL',
# 'class': 'logging.handlers.RotatingFileHandler',
# 'filename': os.path.join(log_path, 'critical-{}.log'.format(time.strftime('%Y-%m-%d'))),
# 'maxBytes': 1024 * 1024 * 5,
# 'backupCount': 5,
# 'formatter': 'myconsole',
# 'encoding': 'utf-8', # 设置默认编码
# }, # 输出警告日志
# },
# # 配置用哪几种 handlers 来处理日志
# 'loggers': {
# # 类型 为 django 处理所有类型的日志, 默认调用
# 'django': {
# 'handlers': ['console', 'error', 'info', 'warning', 'critical', 'console'],
# 'level': 'INFO',
# 'propagate': False
# },
# # log 调用时需要当作参数传入
# 'log': {
# 'handlers': ['error', 'info', 'console', 'default'],
# 'level': 'INFO',
# 'propagate': True
# },
# }
# }
loggers中的django配置为django运行时调用的配置文件,handlers中的所有项都会被记录到文件中,log为自定义日志,你可以调用django的日志系统,写自定义日志,
有帮助请采纳,谢谢