1
0

zap.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package zap
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "sync"
  8. "go.uber.org/zap"
  9. "go.uber.org/zap/zapcore"
  10. "gogs.baozhida.cn/zoie/OAuth-core/logger"
  11. )
  12. type zaplog struct {
  13. cfg zap.Config
  14. zap *zap.Logger
  15. opts logger.Options
  16. sync.RWMutex
  17. fields map[string]interface{}
  18. }
  19. func (l *zaplog) Init(opts ...logger.Option) error {
  20. //var err error
  21. for _, o := range opts {
  22. o(&l.opts)
  23. }
  24. zapConfig := zap.NewProductionConfig()
  25. if zconfig, ok := l.opts.Context.Value(configKey{}).(zap.Config); ok {
  26. zapConfig = zconfig
  27. }
  28. if zcconfig, ok := l.opts.Context.Value(encoderConfigKey{}).(zapcore.EncoderConfig); ok {
  29. zapConfig.EncoderConfig = zcconfig
  30. }
  31. writer, ok := l.opts.Context.Value(writerKey{}).(io.Writer)
  32. if !ok {
  33. writer = os.Stdout
  34. }
  35. skip, ok := l.opts.Context.Value(callerSkipKey{}).(int)
  36. if !ok || skip < 1 {
  37. skip = 1
  38. }
  39. // Set log Level if not default
  40. zapConfig.Level = zap.NewAtomicLevel()
  41. if l.opts.Level != logger.InfoLevel {
  42. zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
  43. }
  44. zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  45. logCore := zapcore.NewCore(
  46. zapcore.NewConsoleEncoder(zapConfig.EncoderConfig),
  47. zapcore.NewMultiWriteSyncer(zapcore.AddSync(writer)),
  48. zapConfig.Level)
  49. log := zap.New(logCore, zap.AddCaller(), zap.AddCallerSkip(skip), zap.AddStacktrace(zap.DPanicLevel))
  50. //log, err := zapConfig.Build(zap.AddCallerSkip(skip))
  51. //if err != nil {
  52. // return err
  53. //}
  54. // Adding seed fields if exist
  55. if l.opts.Fields != nil {
  56. data := []zap.Field{}
  57. for k, v := range l.opts.Fields {
  58. data = append(data, zap.Any(k, v))
  59. }
  60. log = log.With(data...)
  61. }
  62. // Adding namespace
  63. if namespace, ok := l.opts.Context.Value(namespaceKey{}).(string); ok {
  64. log = log.With(zap.Namespace(namespace))
  65. }
  66. // defer log.Sync() ??
  67. l.cfg = zapConfig
  68. l.zap = log
  69. l.fields = make(map[string]interface{})
  70. return nil
  71. }
  72. func (l *zaplog) Fields(fields map[string]interface{}) logger.Logger {
  73. l.Lock()
  74. nfields := make(map[string]interface{}, len(l.fields))
  75. for k, v := range l.fields {
  76. nfields[k] = v
  77. }
  78. l.Unlock()
  79. for k, v := range fields {
  80. nfields[k] = v
  81. }
  82. data := make([]zap.Field, 0, len(nfields))
  83. for k, v := range fields {
  84. data = append(data, zap.Any(k, v))
  85. }
  86. zl := &zaplog{
  87. cfg: l.cfg,
  88. zap: l.zap,
  89. opts: l.opts,
  90. fields: nfields,
  91. }
  92. return zl
  93. }
  94. func (l *zaplog) Error(err error) logger.Logger {
  95. return l.Fields(map[string]interface{}{"error": err})
  96. }
  97. func (l *zaplog) Log(level logger.Level, args ...interface{}) {
  98. l.RLock()
  99. data := make([]zap.Field, 0, len(l.fields))
  100. for k, v := range l.fields {
  101. data = append(data, zap.Any(k, v))
  102. }
  103. l.RUnlock()
  104. lvl := loggerToZapLevel(level)
  105. msg := fmt.Sprint(args...)
  106. switch lvl {
  107. case zap.DebugLevel:
  108. l.zap.Debug(msg, data...)
  109. case zap.InfoLevel:
  110. l.zap.Info(msg, data...)
  111. case zap.WarnLevel:
  112. l.zap.Warn(msg, data...)
  113. case zap.ErrorLevel:
  114. l.zap.Error(msg, data...)
  115. case zap.FatalLevel:
  116. l.zap.Fatal(msg, data...)
  117. }
  118. }
  119. func (l *zaplog) Logf(level logger.Level, format string, args ...interface{}) {
  120. l.RLock()
  121. data := make([]zap.Field, 0, len(l.fields))
  122. for k, v := range l.fields {
  123. data = append(data, zap.Any(k, v))
  124. }
  125. l.RUnlock()
  126. lvl := loggerToZapLevel(level)
  127. msg := fmt.Sprintf(format, args...)
  128. switch lvl {
  129. case zap.DebugLevel:
  130. l.zap.Debug(msg, data...)
  131. case zap.InfoLevel:
  132. l.zap.Info(msg, data...)
  133. case zap.WarnLevel:
  134. l.zap.Warn(msg, data...)
  135. case zap.ErrorLevel:
  136. l.zap.Error(msg, data...)
  137. case zap.FatalLevel:
  138. l.zap.Fatal(msg, data...)
  139. }
  140. }
  141. func (l *zaplog) String() string {
  142. return "zap"
  143. }
  144. func (l *zaplog) Options() logger.Options {
  145. return l.opts
  146. }
  147. // New builds a new logger based on options
  148. func NewLogger(opts ...logger.Option) (logger.Logger, error) {
  149. // Default options
  150. options := logger.Options{
  151. Level: logger.InfoLevel,
  152. Fields: make(map[string]interface{}),
  153. Out: os.Stderr,
  154. Context: context.Background(),
  155. }
  156. l := &zaplog{opts: options}
  157. if err := l.Init(opts...); err != nil {
  158. return nil, err
  159. }
  160. return l, nil
  161. }
  162. func loggerToZapLevel(level logger.Level) zapcore.Level {
  163. switch level {
  164. case logger.TraceLevel, logger.DebugLevel:
  165. return zap.DebugLevel
  166. case logger.InfoLevel:
  167. return zap.InfoLevel
  168. case logger.WarnLevel:
  169. return zap.WarnLevel
  170. case logger.ErrorLevel:
  171. return zap.ErrorLevel
  172. case logger.FatalLevel:
  173. return zap.FatalLevel
  174. default:
  175. return zap.InfoLevel
  176. }
  177. }
  178. func zapToLoggerLevel(level zapcore.Level) logger.Level {
  179. switch level {
  180. case zap.DebugLevel:
  181. return logger.DebugLevel
  182. case zap.InfoLevel:
  183. return logger.InfoLevel
  184. case zap.WarnLevel:
  185. return logger.WarnLevel
  186. case zap.ErrorLevel:
  187. return logger.ErrorLevel
  188. case zap.FatalLevel:
  189. return logger.FatalLevel
  190. default:
  191. return logger.InfoLevel
  192. }
  193. }