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.

30 lines
1.1KB

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from os import makedirs
  4. from os.path import join, exists
  5. from loguru import logger
  6. from util.RWUtils import getConfigs
  7. # 初始化日志配置
  8. def init_log(base_dir):
  9. log_config = getConfigs(base_dir, 'config/logger.yml')
  10. # 判断日志文件是否存在,不存在创建
  11. base_path = join(base_dir, log_config["base_path"])
  12. if not exists(base_path):
  13. makedirs(base_path)
  14. # 移除日志设置
  15. logger.remove(handler_id=None)
  16. # 打印日志到文件
  17. if bool(log_config["enable_file_log"]):
  18. logger.add(join(base_path, log_config["log_name"]),
  19. rotation=log_config["rotation"],
  20. retention=log_config["retention"],
  21. format=log_config["log_fmt"],
  22. level=log_config["level"],
  23. enqueue=True,
  24. encoding=log_config["encoding"])
  25. # 控制台输出
  26. if bool(log_config["enable_stderr"]):
  27. logger.add(sys.stderr, format=log_config["log_fmt"], level=log_config["level"], enqueue=True)