log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package fslog
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. "gopkg.in/natefinch/lumberjack.v2"
  6. "io"
  7. "os"
  8. )
  9. var log *Logger
  10. // 默认配置,准备了控制台、json文件两种输出方式
  11. var consoleEncoder zapcore.Encoder
  12. var jsonEncoder zapcore.Encoder
  13. var consoleSync zapcore.WriteSyncer
  14. var outputSync zapcore.WriteSyncer
  15. func init() {
  16. consoleEncoderConfig := zap.NewDevelopmentEncoderConfig()
  17. consoleEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
  18. consoleEncoder = zapcore.NewConsoleEncoder(consoleEncoderConfig)
  19. jsonEncoderConfig := zap.NewProductionEncoderConfig()
  20. jsonEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
  21. jsonEncoder = zapcore.NewJSONEncoder(jsonEncoderConfig)
  22. consoleSync = zapcore.AddSync(os.Stdout)
  23. outputSync = zapcore.AddSync(&lumberjack.Logger{
  24. Filename: "logs/log",
  25. MaxSize: 1,
  26. MaxAge: 10,
  27. LocalTime: true,
  28. })
  29. log = New()
  30. log.AddCore(BuiltinConsoleCore(DebugLv), BuiltinOutputFileCore(DebugLv))
  31. }
  32. // With 见 Logger.With
  33. func With(k string, v any) *Logger {
  34. return log.With(k, v)
  35. }
  36. // Info 见 Logger.Info
  37. func Info(msg string, vs ...any) *Logger {
  38. log.Info(msg, vs...)
  39. return log
  40. }
  41. // Debug 见 Logger.Debug
  42. func Debug(msg string, vs ...any) *Logger {
  43. log.Debug(msg, vs...)
  44. return log
  45. }
  46. // Warn 见 Logger.Warn
  47. func Warn(msg string, vs ...any) *Logger {
  48. log.Warn(msg, vs...)
  49. return log
  50. }
  51. // Error 见 Logger.Error
  52. func Error(vs ...any) *Logger {
  53. log.Error(vs...)
  54. return log
  55. }
  56. // Lv 见 Logger.Lv
  57. func Lv() Level {
  58. return Level(log.Lv().Level())
  59. }
  60. // SetLv 见 Logger.SetLv
  61. func SetLv(lv Level) *Logger {
  62. log.SetLv(lv)
  63. return log
  64. }
  65. // NewFileOutput 见 Logger.NewFileOutput
  66. func NewFileOutput(opts ...FileOutputOpt) *Logger {
  67. log.NewFileOutput(opts...)
  68. return log
  69. }
  70. // NewOutput 见 Logger.NewOutput
  71. func NewOutput(writer io.Writer) *Logger {
  72. log.NewOutput(writer)
  73. return log
  74. }
  75. // SetLogger 替换fslog包默认的log对象
  76. // Logger 支持了很多zapcore级别的底层配置,见 Logger
  77. func SetLogger(logger *Logger) *Logger {
  78. log = logger
  79. return log
  80. }
  81. // Flush 见 Logger.Flush
  82. func Flush() {
  83. log.Flush()
  84. }
  85. // BuiltinConsoleCore 获取内置的控制台输出 zapcore.Core
  86. func BuiltinConsoleCore(lv Level) zapcore.Core {
  87. return zapcore.NewCore(consoleEncoder, consoleSync, lv.zap())
  88. }
  89. // BuiltinOutputFileCore 获取文件输出 zapcore.Core
  90. func BuiltinOutputFileCore(lv Level) zapcore.Core {
  91. return zapcore.NewCore(jsonEncoder, outputSync, lv.zap())
  92. }