sls_log.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/31 15:27
  3. # @Author : XuJiakai
  4. # @File : sls_log
  5. # @Software: PyCharm
  6. import logging.config, os
  7. import platform
  8. __version__ = '0.1.0'
  9. def __is_test_env():
  10. return os.environ.get('SPIDER_TEST', False) or platform.system() == "Windows"
  11. def __get_endpoint():
  12. if __is_test_env():
  13. return "cn-shanghai.log.aliyuncs.com"
  14. else:
  15. return "cn-shanghai-intranet.log.aliyuncs.com"
  16. conf = {'version': 1,
  17. 'disable_existing_loggers': False,
  18. 'formatters': {'rawformatter': {'class': 'logging.Formatter',
  19. 'format': '%(message)s'}
  20. },
  21. 'handlers': {'sls_handler': {'()':
  22. 'aliyun.log.QueuedLogHandler',
  23. 'level': 'INFO',
  24. 'formatter': 'rawformatter',
  25. # custom args:
  26. 'end_point': os.environ.get('ALIYUN_LOG_ENDPOINT', __get_endpoint()),
  27. 'access_key_id': os.environ.get('ALIYUN_LOG_ACCESSID', 'LTAI4GKTLebfci8tYSM1NFCN'),
  28. 'access_key': os.environ.get('ALIYUN_LOG_ACCESSKEY', 'v59Sy885c9GfXZ4me9caIxYHh9CPVc'),
  29. 'project': os.environ.get('ALIYUN_LOG_PROJECT', 'data-clean'),
  30. 'log_store': os.environ.get('ALIYUN_LOG_STORE', "clean"),
  31. 'extract_json': True,
  32. 'extract_json_prefix': 'dc_',
  33. }
  34. },
  35. 'loggers': {'sls': {'handlers': ['sls_handler', ],
  36. 'level': 'INFO',
  37. 'propagate': False}
  38. }
  39. }
  40. logging.config.dictConfig(conf)
  41. def get_logger():
  42. return logging.getLogger("sls")