third_log.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package logger
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. //
  7. //******************third.log文件
  8. //日志格式举例:2018-01-01 10:25:57:404|https://oep.songguo7.com/ebike-op-oep/ebike/get_bike_qr_code|200|1313|5|0|traceid
  9. // HTTP & HTTPS & thrift & ws等协议请求
  10. // 格式:【时间(精确到毫秒)】|【请求url】|【http状态码】|【响应时间(毫秒为单位)】|【response大小(若有则打印,若无默认为0)】|【exception状态0:正常 1:异常】|【traceid唯一ID,如果接入nginx可以从nginx 的http header中获取】
  11. //#{Time}: 时间
  12. //#{Url}: 完整url地址
  13. //#{StatusCode}: http状态码
  14. //#{CostTime}: 响应时间
  15. //#{ResBodyNum}: response大小
  16. //#{IsException}: 异常状态0:正常 1:异常
  17. //#{TraceId}: trace_id
  18. //日志分割逻辑
  19. //接天分割
  20. //task.log # 当前的日志
  21. //task.log.2019-08-03 # 2019-08-03 号的日志
  22. ///data/log/{project_name}/task.log.2019-08-03 #日志存放目录
  23. var (
  24. ThirdLog = func(url string, statusCode int, costTime int64, resBodyNum int64, traceId string) {
  25. var isException int
  26. if statusCode != 200 {
  27. isException = 1
  28. }
  29. if !strings.HasPrefix(url, "http") {
  30. url = "http://" + url
  31. }
  32. err := _fileLogWriter.Thirdlog().thirdOutput(fmt.Sprintf("|%s|%d|%d|%d|%d|%s",
  33. url,
  34. statusCode,
  35. costTime,
  36. resBodyNum,
  37. isException,
  38. traceId,
  39. ))
  40. if err != nil {
  41. consoleLogger.Errorf("write third log to file fail, err: %s", err.Error())
  42. }
  43. }
  44. )