|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- # import logging
- # import sys
- # import os
- # from datetime import datetime, timedelta
-
- # LOG_DIR = "./logs" # 日志文件目录
- # LOG_RETENTION_DAYS = 7 # 日志保留天数
-
- # def _remove_old_logs(log_dir, retention_days):
- # """删除超过保留天数的日志文件"""
- # if not os.path.exists(log_dir):
- # os.makedirs(log_dir)
- # now = datetime.now()
- # for filename in os.listdir(log_dir):
- # file_path = os.path.join(log_dir, filename)
- # if os.path.isfile(file_path) and filename.startswith("run_") and filename.endswith(".log"):
- # # 提取文件日期
- # try:
- # log_date_str = filename[4:14]
- # log_date = datetime.strptime(log_date_str, "%Y-%m-%d")
- # if now - log_date > timedelta(days=retention_days):
- # os.remove(file_path)
- # print(f"删除旧日志: {filename}")
- # except ValueError:
- # continue
-
- # def _reset_logger(log):
- # """重置日志配置,移除旧的 Handler 并添加新的 Handler"""
- # for handler in log.handlers:
- # handler.close()
- # log.removeHandler(handler)
- # del handler
- # log.handlers.clear()
- # log.propagate = False
-
- # # 控制台输出的日志处理器
- # console_handle = logging.StreamHandler(sys.stdout)
- # console_handle.setFormatter(
- # logging.Formatter(
- # "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
- # datefmt="%Y-%m-%d %H:%M:%S",
- # )
- # )
-
- # # 生成带有当前日期的日志文件路径
- # date_str = datetime.now().strftime("%Y-%m-%d")
- # log_file_path = os.path.join(LOG_DIR, f"run_{date_str}.log")
-
- # # 文件日志处理器
- # file_handle = logging.FileHandler(log_file_path, encoding="utf-8")
- # file_handle.setFormatter(
- # logging.Formatter(
- # "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
- # datefmt="%Y-%m-%d %H:%M:%S",
- # )
- # )
-
- # # 将处理器添加到日志
- # log.addHandler(file_handle)
- # log.addHandler(console_handle)
-
- # # 删除旧的日志文件
- # _remove_old_logs(LOG_DIR, LOG_RETENTION_DAYS)
-
- # def setup_logging():
- # """设置日志配置"""
- # log = logging.getLogger("log")
- # _reset_logger(log)
- # log.setLevel(logging.INFO) # 日志级别
- # return log
-
- # def setup_logging():
- # """设置日志配置"""
- # log = logging.getLogger() # 获取 Flask 默认的日志记录器
- # _reset_logger(log)
- # log.setLevel(logging.INFO) # 设置日志级别为 INFO
- # return log
-
- # # 创建日志实例
- # logger = setup_logging()
-
- # def log_exception(sender, exception, **extra):
- # """记录异常日志"""
- # sender.logger.debug('处理过程发生异常: %s', exception)
-
-
-
- import logging
- import sys
- import os
- from datetime import datetime, timedelta
-
- # 日志文件目录
- LOG_DIR = "./logs"
- # 日志保留天数
- LOG_RETENTION_DAYS = 7
-
- def _remove_old_logs(log_dir, retention_days):
- """删除超过保留天数的日志文件"""
- if not os.path.exists(log_dir):
- os.makedirs(log_dir)
- now = datetime.now()
- for filename in os.listdir(log_dir):
- file_path = os.path.join(log_dir, filename)
- if os.path.isfile(file_path) and filename.startswith("run_") and filename.endswith(".log"):
- # 提取文件日期
- try:
- log_date_str = filename[4:14]
- log_date = datetime.strptime(log_date_str, "%Y-%m-%d")
- if now - log_date > timedelta(days=retention_days):
- os.remove(file_path)
- print(f"删除旧日志: {filename}")
- except ValueError:
- continue
-
- def _reset_logger(log):
- """重置日志配置,移除旧的 Handler 并添加新的 Handler"""
- for handler in log.handlers:
- handler.close()
- log.removeHandler(handler)
- del handler
- log.handlers.clear()
- log.propagate = False
-
- # 控制台输出的日志处理器
- console_handle = logging.StreamHandler(sys.stdout)
- console_handle.setFormatter(
- logging.Formatter(
- "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
- datefmt="%Y-%m-%d %H:%M:%S",
- )
- )
-
- # 生成带有当前日期的日志文件路径
- date_str = datetime.now().strftime("%Y-%m-%d")
- log_file_path = os.path.join(LOG_DIR, f"run_{date_str}.log")
-
- # 文件日志处理器
- file_handle = logging.FileHandler(log_file_path, encoding="utf-8")
- file_handle.setFormatter(
- logging.Formatter(
- "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
- datefmt="%Y-%m-%d %H:%M:%S",
- )
- )
-
- # 将处理器添加到日志
- log.addHandler(file_handle)
- log.addHandler(console_handle)
-
- # 删除旧的日志文件
- _remove_old_logs(LOG_DIR, LOG_RETENTION_DAYS)
-
- def setup_logging():
- """设置日志配置"""
- log = logging.getLogger() # 获取默认的日志记录器
- _reset_logger(log)
- log.setLevel(logging.INFO) # 设置日志级别为 INFO
- return log
-
- # 创建日志实例
- logger = setup_logging()
-
- def log_exception(sender, exception, **extra):
- """记录异常日志"""
- logger.error(f"处理过程发生异常: {exception}", exc_info=True)
-
- # FastAPI 日志配置示例
- def configure_fastapi_logging():
- """配置 FastAPI 的日志记录"""
- fastapi_logger = logging.getLogger("uvicorn")
- _reset_logger(fastapi_logger)
- fastapi_logger.setLevel(logging.INFO)
-
- # Celery 日志配置
- def configure_celery_logging():
- """配置 Celery 的日志记录"""
- celery_logger = logging.getLogger("celery")
- _reset_logger(celery_logger)
- celery_logger.setLevel(logging.INFO)
-
- # Celery RedBeat 日志配置
- def configure_redbeat_logging():
- """配置 Celery RedBeat 的日志记录"""
- redbeat_logger = logging.getLogger("redbeat")
- _reset_logger(redbeat_logger)
- redbeat_logger.setLevel(logging.INFO)
-
-
- # 配置 FastAPI 日志
- configure_fastapi_logging()
-
- # 配置 Celery 日志
- configure_celery_logging()
-
-
- # 配置 Redbeat 日志
- configure_redbeat_logging()
|