trans_log.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2024/5/16
  3. # @Author : 魏志亮
  4. import datetime
  5. import logging
  6. import sys
  7. from os import path, sep, makedirs
  8. from utils.conf.read_conf import read_conf, yaml_conf
  9. logger = logging.getLogger("energy_online_data")
  10. logger.setLevel(logging.INFO)
  11. stout_handle = logging.StreamHandler(sys.stdout)
  12. stout_handle.setFormatter(
  13. logging.Formatter("%(asctime)s: %(message)s"))
  14. stout_handle.setLevel(logging.INFO)
  15. logger.addHandler(stout_handle)
  16. config_path = path.abspath(__file__).split("utils")[0] + 'conf' + sep + 'config.yaml'
  17. config = yaml_conf(config_path)
  18. log_path_dir = read_conf(config, 'log_path_dir', "/data/logs")
  19. log_path = log_path_dir + sep + r'energy_online_data'
  20. file_path = path.join(log_path)
  21. if not path.exists(file_path):
  22. makedirs(file_path, exist_ok=True)
  23. file_name = str(file_path + sep + str(datetime.date.today()) + '.log')
  24. file_handler = logging.FileHandler(file_name, encoding='utf-8')
  25. file_handler.setFormatter(
  26. logging.Formatter("%(asctime)s: %(message)s"))
  27. file_handler.setLevel(logging.INFO)
  28. logger.addHandler(file_handler)
  29. # def logger.info(*args):
  30. # logger.info(" ".join([str(a) for a in args]))