trans_log.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2024/5/16
  3. # @Author : 魏志亮
  4. import datetime
  5. import logging
  6. import sys
  7. from os import *
  8. from utils.conf.read_conf import read_conf, yaml_conf
  9. def set_trance_id(trace_id):
  10. """设置当前线程的链路ID"""
  11. environ['trace_id'] = trace_id
  12. class ContextFilter(logging.Filter):
  13. """一个自定义的日志过滤器,用于在日志记录中添加链路ID"""
  14. def filter(self, record):
  15. record.trace_id = ''
  16. if 'trace_id' in environ.keys():
  17. record.trace_id = environ['trace_id']
  18. return True
  19. logger = logging.getLogger("etl_tools")
  20. logger.setLevel(logging.INFO)
  21. stout_handle = logging.StreamHandler(sys.stdout)
  22. stout_handle.setFormatter(
  23. logging.Formatter("%(asctime)s-%(trace_id)s: %(message)s"))
  24. stout_handle.setLevel(logging.INFO)
  25. stout_handle.addFilter(ContextFilter())
  26. logger.addHandler(stout_handle)
  27. config_path = path.abspath(__file__).split("utils")[0] + 'conf' + sep + 'etl_config_dev.yaml'
  28. config = yaml_conf(environ.get('ETL_CONF', config_path))
  29. log_path_dir = read_conf(config, 'log_path_dir', "/data/logs")
  30. log_path = log_path_dir + sep + r'etl_tools_' + (environ['env'] if 'env' in environ else 'dev')
  31. file_path = path.join(log_path)
  32. if not path.exists(file_path):
  33. makedirs(file_path, exist_ok=True)
  34. file_name = file_path + sep + str(datetime.date.today()) + '.log'
  35. file_handler = logging.FileHandler(file_name, encoding='utf-8')
  36. file_handler.setFormatter(
  37. logging.Formatter("%(asctime)s-%(trace_id)s: %(message)s"))
  38. file_handler.setLevel(logging.INFO)
  39. file_handler.addFilter(ContextFilter())
  40. logger.addHandler(file_handler)
  41. def trans_print(*args):
  42. logger.info(" ".join([str(a) for a in args]))