log.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package log
  2. import (
  3. "io"
  4. // "github.com/mattn/go-colorable"
  5. "gopkg.in/yaml.v3"
  6. // log "github.com/sirupsen/logrus"
  7. . "github.com/logrusorgru/aurora/v4"
  8. "go.uber.org/zap"
  9. "go.uber.org/zap/zapcore"
  10. )
  11. var engineConfig = zapcore.EncoderConfig{
  12. // Keys can be anything except the empty string.
  13. TimeKey: "T",
  14. LevelKey: "L",
  15. NameKey: "N",
  16. CallerKey: "C",
  17. FunctionKey: zapcore.OmitKey,
  18. MessageKey: "M",
  19. StacktraceKey: "S",
  20. LineEnding: zapcore.DefaultLineEnding,
  21. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  22. EncodeTime: zapcore.TimeEncoderOfLayout("15:04:05"),
  23. EncodeDuration: zapcore.StringDurationEncoder,
  24. EncodeCaller: zapcore.ShortCallerEncoder,
  25. EncodeName: NameEncoder,
  26. NewReflectedEncoder: func(w io.Writer) zapcore.ReflectedEncoder {
  27. return yaml.NewEncoder(w)
  28. },
  29. }
  30. var LogLevel = zap.NewAtomicLevelAt(zap.DebugLevel)
  31. var Trace bool
  32. var logger *zap.Logger = zap.New(
  33. zapcore.NewCore(zapcore.NewConsoleEncoder(engineConfig), zapcore.AddSync(multipleWriter), LogLevel),
  34. )
  35. var sugaredLogger *zap.SugaredLogger = logger.Sugar()
  36. var LocaleLogger *Logger
  37. func NameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
  38. enc.AppendString(Colorize(loggerName, WhiteFg|BlackBg).String())
  39. }
  40. type Zap interface {
  41. Lang(lang map[string]string) *Logger
  42. Named(name string) *Logger
  43. With(fields ...zap.Field) *Logger
  44. Trace(msg string, fields ...zap.Field)
  45. Debug(msg string, fields ...zap.Field)
  46. Info(msg string, fields ...zap.Field)
  47. Warn(msg string, fields ...zap.Field)
  48. Error(msg string, fields ...zap.Field)
  49. }
  50. type Logger struct {
  51. *zap.Logger
  52. lang map[string]string
  53. }
  54. func (l Logger) Lang(lang map[string]string) *Logger {
  55. l.Logger = logger
  56. l.lang = lang
  57. return &l
  58. }
  59. func (l Logger) Named(name string) *Logger {
  60. l.Logger = l.Logger.Named(name)
  61. return &l
  62. }
  63. func (l Logger) With(fields ...zap.Field) *Logger {
  64. for i, field := range fields {
  65. if v, ok := l.lang[field.Key]; ok {
  66. fields[i].Key = v
  67. }
  68. }
  69. l.Logger = l.Logger.With(fields...)
  70. return &l
  71. }
  72. func (l *Logger) formatLang(msg *string, fields []zapcore.Field) {
  73. if l.lang != nil {
  74. if v, ok := l.lang[*msg]; ok {
  75. *msg = v
  76. }
  77. for i, field := range fields {
  78. if v, ok := l.lang[field.Key]; ok {
  79. fields[i].Key = v
  80. }
  81. }
  82. }
  83. }
  84. func (l *Logger) Trace(msg string, fields ...zap.Field) {
  85. l.formatLang(&msg, fields)
  86. l.Logger.Debug(msg, fields...)
  87. }
  88. func (l *Logger) Debug(msg string, fields ...zap.Field) {
  89. l.formatLang(&msg, fields)
  90. l.Logger.Debug(msg, fields...)
  91. }
  92. func (l *Logger) Info(msg string, fields ...zap.Field) {
  93. l.formatLang(&msg, fields)
  94. l.Logger.Info(msg, fields...)
  95. }
  96. func (l *Logger) Warn(msg string, fields ...zap.Field) {
  97. l.formatLang(&msg, fields)
  98. l.Logger.Warn(msg, fields...)
  99. }
  100. func (l *Logger) Error(msg string, fields ...zap.Field) {
  101. l.formatLang(&msg, fields)
  102. l.Logger.Error(msg, fields...)
  103. }
  104. func (l *Logger) Fatal(msg string, fields ...zap.Field) {
  105. l.formatLang(&msg, fields)
  106. l.Logger.Fatal(msg, fields...)
  107. }
  108. func (l *Logger) Panic(msg string, fields ...zap.Field) {
  109. l.formatLang(&msg, fields)
  110. l.Logger.Panic(msg, fields...)
  111. }