business_log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package logger
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "git.shuncheng.lu/bigthing/gocommon/pkg/trace"
  7. )
  8. /**
  9. 业务日志暴露接口
  10. */
  11. var (
  12. Fatal = func(s string) {
  13. err := _fileLogWriter.Log(LevelFatal).Output(LevelFatal, s)
  14. if err != nil {
  15. consoleLogger.Errorf("write Fatal log to file fail, err: %s", err.Error())
  16. }
  17. os.Exit(1)
  18. }
  19. Fatalf = func(format string, v ...interface{}) {
  20. err := _fileLogWriter.Log(LevelFatal).Output(LevelFatal, fmt.Sprintf(format, v...))
  21. if err != nil {
  22. consoleLogger.Errorf("write Fatalf log to file fail, err: %s", err.Error())
  23. }
  24. os.Exit(1)
  25. }
  26. Fatalc = func(ctx context.Context, format string, v ...interface{}) {
  27. err := _fileLogWriter.Log(LevelFatal).Output(LevelFatal, fmt.Sprintf(fmt.Sprintf("[trace_id=%v] %s", GetTraceId(ctx), format), v...))
  28. if err != nil {
  29. consoleLogger.Errorf("write Fatalc log to file fail, err: %s", err.Error())
  30. }
  31. os.Exit(1)
  32. }
  33. Error = func(s string) {
  34. err := _fileLogWriter.Log(LevelError).Output(LevelError, s)
  35. if err != nil {
  36. consoleLogger.Errorf("write Error log to file fail, err: %s", err.Error())
  37. }
  38. }
  39. Errorf = func(format string, v ...interface{}) {
  40. err := _fileLogWriter.Log(LevelError).Output(LevelError, fmt.Sprintf(format, v...))
  41. if err != nil {
  42. consoleLogger.Errorf("write Errorf log to file fail, err: %s", err.Error())
  43. }
  44. }
  45. Errorc = func(ctx context.Context, format string, v ...interface{}) {
  46. out := fmt.Sprintf(format, v...)
  47. trace.GinLogError(ctx, out)
  48. err := _fileLogWriter.Log(LevelError).Output(LevelError, "[trace_id="+GetTraceId(ctx)+"] "+out)
  49. if err != nil {
  50. consoleLogger.Errorf("write Errorc log to file fail, err: %s", err.Error())
  51. }
  52. }
  53. Warn = func(s string) {
  54. err := _fileLogWriter.Log(LevelWarning).Output(LevelWarning, s)
  55. if err != nil {
  56. consoleLogger.Errorf("write Warn log to file fail, err: %s", err.Error())
  57. }
  58. }
  59. Warnf = func(format string, v ...interface{}) {
  60. err := _fileLogWriter.Log(LevelWarning).Output(LevelWarning, fmt.Sprintf(format, v...))
  61. if err != nil {
  62. consoleLogger.Errorf("write Warnf log to file fail, err: %s", err.Error())
  63. }
  64. }
  65. Warnc = func(ctx context.Context, format string, v ...interface{}) {
  66. out := fmt.Sprintf(format, v...)
  67. //trace.GinLogInfo(ctx, out)
  68. err := _fileLogWriter.Log(LevelWarning).Output(LevelWarning, "[trace_id="+GetTraceId(ctx)+"] "+out)
  69. if err != nil {
  70. consoleLogger.Errorf("write Warnc log to file fail, err: %s", err.Error())
  71. }
  72. }
  73. Info = func(s string) {
  74. err := _fileLogWriter.Log(LevelInfo).Output(LevelInfo, s)
  75. if err != nil {
  76. consoleLogger.Errorf("write Info log to file fail, err: %s", err.Error())
  77. }
  78. }
  79. Infof = func(format string, v ...interface{}) {
  80. err := _fileLogWriter.Log(LevelInfo).Output(LevelInfo, fmt.Sprintf(format, v...))
  81. if err != nil {
  82. consoleLogger.Errorf("write Infof log to file fail, err: %s", err.Error())
  83. }
  84. }
  85. Infoc = func(ctx context.Context, format string, v ...interface{}) {
  86. out := fmt.Sprintf(format, v...)
  87. //trace.GinLogInfo(ctx, out) // 由于整个数据体传递信息记录成本太高,所有info不做记录
  88. err := _fileLogWriter.Log(LevelInfo).Output(LevelInfo, "[trace_id="+GetTraceId(ctx)+"] "+out)
  89. if err != nil {
  90. consoleLogger.Errorf("write Infoc log to file fail, err: %s", err.Error())
  91. }
  92. }
  93. Debug = func(s string) {
  94. err := _fileLogWriter.Log(LevelDebug).Output(LevelDebug, s)
  95. if err != nil {
  96. consoleLogger.Errorf("write Debug log to file fail, err: %s", err.Error())
  97. }
  98. }
  99. Debugf = func(format string, v ...interface{}) {
  100. err := _fileLogWriter.Log(LevelDebug).Output(LevelDebug, fmt.Sprintf(format, v...))
  101. if err != nil {
  102. consoleLogger.Errorf("write Debugf log to file fail, err: %s", err.Error())
  103. }
  104. }
  105. Debugc = func(ctx context.Context, format string, v ...interface{}) {
  106. err := _fileLogWriter.Log(LevelDebug).Output(LevelDebug, fmt.Sprintf(fmt.Sprintf("[trace_id=%v] %s", GetTraceId(ctx), format), v...))
  107. if err != nil {
  108. consoleLogger.Errorf("write Debugc log to file fail, err: %s", err.Error())
  109. }
  110. }
  111. )