logger.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package fslog
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "go.uber.org/zap/zapcore"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "io"
  8. "sync"
  9. )
  10. // 默认配置,准备了控制台、json文件两种输出方式
  11. var consoleEncoder zapcore.Encoder
  12. var jsonEncoder zapcore.Encoder
  13. var consoleSync zapcore.WriteSyncer
  14. var outputSync zapcore.WriteSyncer
  15. type outFileConfig struct {
  16. filename string
  17. maxSize int
  18. maxAge int
  19. maxBackups int
  20. localTime bool
  21. Compress bool
  22. }
  23. type Logger struct {
  24. // 互斥量
  25. // 用于内部不可并发逻辑使用
  26. lock sync.Mutex
  27. // 日志打印用
  28. logger zap.SugaredLogger
  29. // 当前日志打印级别
  30. // 借助zap内部级别设置机制,该机制
  31. // 内部使用乐观锁,协程安全
  32. lv zap.AtomicLevel
  33. // zapcore.Core 映射,存储不同来源/用途的core创建
  34. cores map[coreType][]zapcore.Core
  35. }
  36. func NewLogger() *Logger {
  37. logger := new(Logger)
  38. logger.lv = zap.NewAtomicLevelAt(zap.DebugLevel)
  39. logger.setCore(console, zapcore.NewCore(consoleEncoder, consoleSync, logger.lv))
  40. logger.setCore(output, zapcore.NewCore(jsonEncoder, outputSync, logger.lv))
  41. logger.flush()
  42. return logger
  43. }
  44. // With 拼接自定义信息
  45. // 主要用于打印当前环境快照信息(变量或其他自定义信息)
  46. // 打印后,该信息会跟随日志一起打印
  47. func (l *Logger) With(k string, v any) *Logger {
  48. newL := l.clone()
  49. newL.logger = *newL.logger.With(zap.Any(k, v))
  50. return newL
  51. }
  52. // Debug 格式化打印调试级别日志
  53. // 不同于zap内部可变参数逻辑,该可变参数是用于,字符串格式化的
  54. func (l *Logger) Debug(msg string, vs ...any) {
  55. if len(vs) == 0 {
  56. l.logger.Debug(msg)
  57. return
  58. }
  59. l.logger.Debugf(fmt.Sprintf(msg, vs...))
  60. }
  61. // Info 格式化打印信息级别日志
  62. // 不同于zap内部可变参数逻辑,该可变参数是用于,字符串格式化的
  63. func (l *Logger) Info(msg string, vs ...any) {
  64. if len(vs) == 0 {
  65. l.logger.Info(msg)
  66. return
  67. }
  68. l.logger.Infof(msg, vs...)
  69. }
  70. // Warn 格式化打印警告级别日志
  71. // 不同于zap内部可变参数逻辑,该可变参数是用于,字符串格式化的
  72. func (l *Logger) Warn(msg string, vs ...any) {
  73. if len(vs) == 0 {
  74. l.logger.Warn(msg)
  75. return
  76. }
  77. l.logger.Warnf(msg, vs...)
  78. }
  79. // Error 打印错误级别日志
  80. // 该方法具有两种传参形式:
  81. // 1. error类型:会直接格式化打印%+v日志
  82. // 2. 信息(格式化)
  83. // 3. error+格式化信息:error会作为 With 格式存在,且依旧以%+v格式输出
  84. func (l *Logger) Error(vs ...any) {
  85. if len(vs) == 0 {
  86. return
  87. }
  88. err, ok := vs[0].(error)
  89. if ok {
  90. if len(vs) == 1 {
  91. l.logger.Errorf("%+v", err)
  92. return
  93. }
  94. withed := l.logger.With(zap.String("err", fmt.Sprintf("%+v", err)))
  95. msg, ok := vs[1].(string)
  96. if ok {
  97. withed.Errorf(msg, vs[2:])
  98. return
  99. }
  100. withed.Error(vs[1:])
  101. return
  102. }
  103. msg, ok := vs[0].(string)
  104. if ok {
  105. if len(vs) > 1 {
  106. l.logger.Errorf(msg, vs[1:]...)
  107. return
  108. }
  109. if len(vs) == 1 {
  110. l.logger.Error(vs[0])
  111. return
  112. }
  113. }
  114. }
  115. // Lv 获取当前日志打印级别
  116. func (l *Logger) Lv() zap.AtomicLevel {
  117. return l.lv
  118. }
  119. // SetLv 设置当前日志打印级别
  120. func (l *Logger) SetLv(lv Level) {
  121. l.lv.SetLevel(lv.zap())
  122. }
  123. func (l *Logger) NewFileOutput(opts ...FileOutputOpt) {
  124. cfg := new(outFileConfig)
  125. for _, opt := range opts {
  126. opt(cfg)
  127. }
  128. l.newFileOut(*cfg)
  129. }
  130. func (l *Logger) NewOutput(writer io.Writer) {
  131. l.newOut(writer)
  132. }
  133. func (l *Logger) SetCore(core ...zapcore.Core) {
  134. l.setCore(third, core...)
  135. l.flush()
  136. }
  137. func (l *Logger) Flush() {
  138. err := l.logger.Sync()
  139. if err != nil {
  140. Warn("flush log error: %s", err.Error())
  141. }
  142. }
  143. func (l *Logger) newFileOut(cfg outFileConfig) {
  144. l.newOut(&lumberjack.Logger{
  145. Filename: cfg.filename,
  146. MaxSize: cfg.maxSize,
  147. MaxAge: cfg.maxAge,
  148. MaxBackups: cfg.maxBackups,
  149. LocalTime: cfg.localTime,
  150. Compress: cfg.Compress,
  151. })
  152. }
  153. func (l *Logger) newOut(writer io.Writer) {
  154. l.setCore(output, zapcore.NewCore(jsonEncoder, zapcore.AddSync(writer), l.lv))
  155. l.flush()
  156. }
  157. // 保存内部core(不负责刷新 logger)
  158. func (l *Logger) setCore(ct coreType, core ...zapcore.Core) {
  159. if l.cores == nil {
  160. l.cores = make(map[coreType][]zapcore.Core)
  161. }
  162. l.cores[ct] = append(l.cores[ct], core...)
  163. }
  164. // 刷新内部 logger
  165. // 刷新互斥,触发刷新后,刷新完成前依旧按照旧的配置执行
  166. func (l *Logger) flush() {
  167. l.lock.Lock()
  168. defer l.lock.Unlock()
  169. cores := make([]zapcore.Core, 0, len(l.cores))
  170. for _, core := range l.cores {
  171. cores = append(cores, core...)
  172. }
  173. l.logger = *zap.New(
  174. zapcore.NewTee(cores...),
  175. zap.AddCaller(),
  176. ).Sugar()
  177. }
  178. func (l *Logger) clone() *Logger {
  179. newL := &Logger{cores: l.cores, lv: l.lv}
  180. newL.flush()
  181. return newL
  182. }