task_log.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package logger
  2. import (
  3. "time"
  4. "git.shuncheng.lu/bigthing/gocommon/pkg/conf"
  5. "git.shuncheng.lu/bigthing/gocommon/pkg/internal/json"
  6. )
  7. //
  8. //******************task.log文件
  9. //日志格式举例:2019-08-06 23:50:22,798 INFO - {"name":"city_efficient_job","project":"ebike-geo", "action": 1, "code": 0, "msg": "msg", "event_time": 1565278582322, "event_id": 1565278582322}
  10. //#{project}: 项目名称
  11. //#{name}: 任务名称,英文名
  12. //#{action}: 当前操作类型, 0, 开始执行, 1 结束执行
  13. //#{code}: 任务结果: 0 执行成功, 1 失败(option, action==1时必填)
  14. //#{msg}: 任务扩展信息(option)
  15. //#{event_time}: 精确到毫秒级的时间戳
  16. //#{event_id}: 任务开始的时间戳, 精确到毫秒级
  17. //日志分割逻辑
  18. //接天分割
  19. //task.log # 当前的日志
  20. //task.log.2019-08-03 # 2019-08-03 号的日志
  21. ///data/log/{project_name}/task.log.2019-08-03 #日志存放目录
  22. type taskLogContent struct {
  23. Name string `json:"name"`
  24. Project string `json:"project"`
  25. Action int `json:"action"`
  26. Code int `json:"code"`
  27. Msg string `json:"msg"`
  28. EventTime int64 `json:"event_time"`
  29. EventId int64 `json:"event_id"`
  30. }
  31. var (
  32. TaskInfo = func(name string, action int, code int, msg string, eventId int64) {
  33. nowTime := time.Now().UnixNano() / 1e6
  34. content := taskLogContent{
  35. Name: name,
  36. Project: conf.GetProjectName(),
  37. Action: action,
  38. Code: code,
  39. Msg: msg,
  40. EventTime: nowTime,
  41. EventId: eventId}
  42. s, _ := json.Marshal(content)
  43. err := _fileLogWriter.Tasklog().taskOutput(LevelInfo, string(s))
  44. if err != nil {
  45. consoleLogger.Errorf("write Info log to file fail, err: %s", err.Error())
  46. }
  47. }
  48. TaskWarn = func(name string, action int, code int, msg string) {
  49. nowTime := time.Now().UnixNano() / 1e6
  50. content := taskLogContent{
  51. Name: name,
  52. Project: conf.GetProjectName(),
  53. Action: action,
  54. Code: code,
  55. Msg: msg,
  56. EventTime: nowTime,
  57. EventId: nowTime}
  58. s, _ := json.Marshal(content)
  59. err := _fileLogWriter.Tasklog().taskOutput(LevelWarning, string(s))
  60. if err != nil {
  61. consoleLogger.Errorf("write Warn log to file fail, err: %s", err.Error())
  62. }
  63. }
  64. TaskError = func(name string, action int, code int, msg string) {
  65. nowTime := time.Now().UnixNano() / 1e6
  66. content := taskLogContent{
  67. Name: name,
  68. Project: conf.GetProjectName(),
  69. Action: action,
  70. Code: code,
  71. Msg: msg,
  72. EventTime: nowTime,
  73. EventId: nowTime}
  74. s, _ := json.Marshal(content)
  75. err := _fileLogWriter.Tasklog().taskOutput(LevelError, string(s))
  76. if err != nil {
  77. consoleLogger.Errorf("write Error log to file fail, err: %s", err.Error())
  78. }
  79. }
  80. )