log.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package log
  2. import (
  3. "os"
  4. "time"
  5. "github.com/natefinch/lumberjack"
  6. "go.uber.org/zap"
  7. "go.uber.org/zap/zapcore"
  8. )
  9. const CallerSkipNum = 1
  10. var (
  11. s *zap.SugaredLogger
  12. )
  13. func zapEncoderConfig() zapcore.EncoderConfig {
  14. return zapcore.EncoderConfig{
  15. TimeKey: "timestamp",
  16. LevelKey: "level",
  17. NameKey: "logger",
  18. CallerKey: "caller",
  19. MessageKey: "message",
  20. StacktraceKey: "stacktrace",
  21. LineEnding: "\n",
  22. EncodeLevel: zapcore.LowercaseLevelEncoder,
  23. EncodeTime: func(t time.Time, e zapcore.PrimitiveArrayEncoder) {
  24. e.AppendString(t.Format("2006-01-02 15:04:05"))
  25. },
  26. EncodeDuration: zapcore.SecondsDurationEncoder,
  27. EncodeCaller: zapcore.FullCallerEncoder,
  28. }
  29. }
  30. func init() {
  31. config := zap.Config{
  32. Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
  33. Encoding: "json",
  34. EncoderConfig: zapEncoderConfig(),
  35. InitialFields: map[string]interface{}{"service": "pink"},
  36. OutputPaths: []string{"stdout"},
  37. ErrorOutputPaths: []string{"stdout"},
  38. }
  39. logger, err := config.Build(zap.AddCallerSkip(CallerSkipNum))
  40. if err != nil {
  41. panic(err)
  42. }
  43. s = logger.Sugar()
  44. }
  45. func NewLoggerSugar(serviceName, logFile string, level int32) error {
  46. hook := &lumberjack.Logger{
  47. Filename: logFile, // 日志文件路径
  48. MaxSize: 128, // 每个日志文件保存的大小 单位:M
  49. MaxAge: 7, // 文件最多保存多少天
  50. MaxBackups: 30, // 日志文件最多保存多少个备份
  51. Compress: false, // 是否压缩
  52. }
  53. fileWriter := zapcore.AddSync(hook)
  54. writes := []zapcore.WriteSyncer{fileWriter}
  55. if zapcore.Level(level) == zapcore.DebugLevel {
  56. writes = append(writes, zapcore.AddSync(os.Stdout))
  57. }
  58. lowPriority := zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
  59. return lev >= zap.DebugLevel
  60. })
  61. zcore := zapcore.NewCore(
  62. zapcore.NewJSONEncoder(zapEncoderConfig()),
  63. zapcore.NewMultiWriteSyncer(writes...),
  64. lowPriority,
  65. )
  66. fields := zap.Fields(zap.String("service_name", serviceName))
  67. logger := zap.New(zcore, zap.AddCallerSkip(CallerSkipNum), fields)
  68. s = logger.Sugar()
  69. return nil
  70. }
  71. // Debug uses fmt.Sprint to construct and log a message.
  72. func Debug(args ...interface{}) {
  73. s.Debug(args)
  74. }
  75. // Info uses fmt.Sprint to construct and log a message.
  76. func Info(args ...interface{}) {
  77. s.Info(args)
  78. }
  79. // Warn uses fmt.Sprint to construct and log a message.
  80. func Warn(args ...interface{}) {
  81. s.Warn(args)
  82. }
  83. // Error uses fmt.Sprint to construct and log a message.
  84. func Error(args ...interface{}) {
  85. s.Error(args)
  86. }
  87. // DPanic uses fmt.Sprint to construct and log a message. In development, the
  88. // logger then panics. (See DPanicLevel for details.)
  89. func DPanic(args ...interface{}) {
  90. s.DPanic(args)
  91. }
  92. // Panic uses fmt.Sprint to construct and log a message, then panics.
  93. func Panic(args ...interface{}) {
  94. s.Panic(args)
  95. }
  96. // Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
  97. func Fatal(args ...interface{}) {
  98. s.Fatal(args)
  99. }
  100. // Debugf uses fmt.Sprintf to log a templated message.
  101. func Debugf(template string, args ...interface{}) {
  102. s.Debugf(template, args...)
  103. }
  104. // Infof uses fmt.Sprintf to log a templated message.
  105. func Infof(template string, args ...interface{}) {
  106. s.Infof(template, args...)
  107. }
  108. // Warnf uses fmt.Sprintf to log a templated message.
  109. func Warnf(template string, args ...interface{}) {
  110. s.Warnf(template, args...)
  111. }
  112. // Errorf uses fmt.Sprintf to log a templated message.
  113. func Errorf(template string, args ...interface{}) {
  114. s.Errorf(template, args...)
  115. }
  116. // DPanicf uses fmt.Sprintf to log a templated message. In development, the
  117. // logger then panics. (See DPanicLevel for details.)
  118. func DPanicf(template string, args ...interface{}) {
  119. s.DPanicf(template, args...)
  120. }
  121. // Panicf uses fmt.Sprintf to log a templated message, then panics.
  122. func Panicf(template string, args ...interface{}) {
  123. s.Panicf(template, args...)
  124. }
  125. // Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
  126. func Fatalf(template string, args ...interface{}) {
  127. s.Fatalf(template, args...)
  128. }