public.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package logger
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. "os"
  6. )
  7. var hookCore []zapcore.Core
  8. type L struct {
  9. log *zap.Logger
  10. }
  11. func (l *L) Debug(msg string, field ...zap.Field) {
  12. l.log.Debug(msg, field...)
  13. }
  14. func (l *L) Warn(msg string, field ...zap.Field) {
  15. l.log.Warn(msg, field...)
  16. }
  17. func (l *L) Info(msg string, field ...zap.Field) {
  18. l.log.Info(msg, field...)
  19. }
  20. func (l *L) Error(msg string, field ...zap.Field) {
  21. l.log.Error(msg, field...)
  22. }
  23. func (l *L) Fatal(msg string, field ...zap.Field) {
  24. l.log.Fatal(msg, field...)
  25. }
  26. //func (l *L) Errorf(format string, f ...interface{}) {
  27. // defaultLogger.log.Error(fmt.Sprintf(format, f...))
  28. //}
  29. //
  30. //func (l *L) Warnf(format string, f ...interface{}) {
  31. // defaultLogger.log.Warn(fmt.Sprintf(format, f...))
  32. //}
  33. //
  34. //func (l *L) Infof(format string, f ...interface{}) {
  35. // defaultLogger.log.Info(fmt.Sprintf(format, f...))
  36. //}
  37. //
  38. //func (l *L) Debugf(format string, f ...interface{}) {
  39. // defaultLogger.log.Debug(fmt.Sprintf(format, f...))
  40. //}
  41. //func DebugAsJson(value interface{}) {
  42. // defaultLogger.Debug("debugAsJson", zap.Any("object", value))
  43. //}
  44. func Err(err error) zap.Field {
  45. return zap.Error(err)
  46. }
  47. func String(key string, val string) zap.Field {
  48. return zap.String(key, val)
  49. }
  50. func Any(key string, value interface{}) zap.Field {
  51. return zap.Any(key, value)
  52. }
  53. func Binary(key string, val []byte) zap.Field {
  54. return zap.Binary(key, val)
  55. }
  56. func Bool(key string, val bool) zap.Field {
  57. return zap.Bool(key, val)
  58. }
  59. func ByteString(key string, val []byte) zap.Field {
  60. return zap.ByteString(key, val)
  61. }
  62. func Float64(key string, val float64) zap.Field {
  63. return zap.Float64(key, val)
  64. }
  65. func Float32(key string, val float32) zap.Field {
  66. return zap.Float32(key, val)
  67. }
  68. func NewEncoderConfig() zapcore.EncoderConfig {
  69. encoderConfig := zap.NewProductionEncoderConfig()
  70. encoderConfig.CallerKey = "line"
  71. encoderConfig.TimeKey = "time"
  72. encoderConfig.StacktraceKey = ""
  73. encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  74. return encoderConfig
  75. }
  76. func newHook(core zapcore.Core) {
  77. hookCore = []zapcore.Core{
  78. core,
  79. }
  80. }
  81. //针对这个日志可以配置输出位置
  82. func consoleHook(path string) zapcore.Core {
  83. encoderConfig := NewEncoderConfig()
  84. encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  85. level := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
  86. return lv <= zap.FatalLevel
  87. })
  88. consoleWriter := zapcore.AddSync(os.Stdout)
  89. return zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), consoleWriter, level)
  90. }
  91. func New(path string) *L {
  92. newHook(consoleHook(path))
  93. return &L{log: zap.New(zapcore.NewTee(hookCore...), zap.AddCaller(), zap.AddCallerSkip(2))}
  94. }
  95. func Int(key string, val int) zap.Field {
  96. return Int64(key, int64(val))
  97. }
  98. func Int64(key string, val int64) zap.Field {
  99. return zap.Int64(key, val)
  100. }
  101. func Int8(key string, val int8) zap.Field {
  102. return zap.Int8(key, val)
  103. }
  104. func Uint(key string, val uint) zap.Field {
  105. return Uint64(key, uint64(val))
  106. }
  107. func Uint64(key string, val uint64) zap.Field {
  108. return zap.Uint64(key, val)
  109. }
  110. func Uint8(key string, val uint8) zap.Field {
  111. return zap.Uint8(key, val)
  112. }
  113. func SetLevel(level string) zap.AtomicLevel {
  114. switch level {
  115. case "debug":
  116. return zap.NewAtomicLevelAt(zap.DebugLevel)
  117. case "info":
  118. return zap.NewAtomicLevelAt(zap.InfoLevel)
  119. case "warn":
  120. return zap.NewAtomicLevelAt(zap.WarnLevel)
  121. case "error":
  122. return zap.NewAtomicLevelAt(zap.ErrorLevel)
  123. case "fatal":
  124. return zap.NewAtomicLevelAt(zap.FatalLevel)
  125. case "panic":
  126. return zap.NewAtomicLevelAt(zap.PanicLevel)
  127. default:
  128. return zap.NewAtomicLevelAt(zap.DebugLevel)
  129. }
  130. }