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.

34 lines
1.2KB

  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.json")
  10. # 判断日志文件是否存在,不存在创建
  11. base_path = join(base_dir, log_config.get("base_path"))
  12. if not exists(base_path):
  13. print(base_dir, base_path)
  14. makedirs(base_path)
  15. # 移除日志设置
  16. logger.remove(handler_id=None)
  17. # 打印日志到文件
  18. if bool(log_config.get("enable_file_log")):
  19. logger.add(join(base_path, log_config.get("log_name")),
  20. rotation=log_config.get("rotation"),
  21. retention=log_config.get("retention"),
  22. format=log_config.get("log_fmt"),
  23. level=log_config.get("level"),
  24. enqueue=True,
  25. encoding=log_config.get("encoding"))
  26. # 控制台输出
  27. if bool(log_config.get("enable_stderr")):
  28. logger.add(sys.stderr,
  29. format=log_config.get("log_fmt"),
  30. level=log_config.get("level"),
  31. enqueue=True)