aliyun_log.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. """
  3. 公共方法,包含:生成log / 删除log
  4. """
  5. import json
  6. from datetime import date, timedelta
  7. from datetime import datetime
  8. from typing import Optional
  9. from aliyun.log import PutLogsRequest, LogClient, LogItem
  10. proxies = {"http": None, "https": None}
  11. class AliyunLogger:
  12. # 统一获取当前时间 <class 'datetime.datetime'> 2022-04-14 20:13:51.244472
  13. now = datetime.now()
  14. # 昨天 <class 'str'> 2022-04-13
  15. yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")
  16. # 今天 <class 'datetime.date'> 2022-04-14
  17. today = date.today()
  18. # 明天 <class 'str'> 2022-04-15
  19. tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
  20. # 写入阿里云日志
  21. @staticmethod
  22. def logging(video_id: str,
  23. title: str,
  24. video_url: str,
  25. data: Optional[str] = None):
  26. """
  27. 写入阿里云日志
  28. 测试库: https://sls.console.aliyun.com/lognext/project/crawler-log-dev/logsearch/crawler-log-dev
  29. 正式库: https://sls.console.aliyun.com/lognext/project/crawler-log-prod/logsearch/crawler-log-prod
  30. """
  31. accessKeyId = "LTAIWYUujJAm7CbH"
  32. accessKey = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
  33. project = "crawler-log-prod"
  34. logstore = "video_tag_info"
  35. endpoint = "cn-hangzhou.log.aliyuncs.com"
  36. try:
  37. contents = [
  38. ("video_id", video_id),
  39. ("video_title", title),
  40. ("video_url", video_url),
  41. ("data", data),
  42. ]
  43. # 创建 LogClient 实例
  44. client = LogClient(endpoint, accessKeyId, accessKey)
  45. log_group = []
  46. log_item = LogItem()
  47. log_item.set_contents(contents)
  48. log_group.append(log_item)
  49. # 写入日志
  50. request = PutLogsRequest(
  51. project=project,
  52. logstore=logstore,
  53. topic="",
  54. source="",
  55. logitems=log_group,
  56. compress=False,
  57. )
  58. client.put_logs(request)
  59. except Exception as e:
  60. print("写入日志失败")