log.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. func init() {
  11. consoleEncoderConfig := zap.NewDevelopmentEncoderConfig()
  12. consoleEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
  13. consoleEncoder = zapcore.NewConsoleEncoder(consoleEncoderConfig)
  14. jsonEncoderConfig := zap.NewProductionEncoderConfig()
  15. jsonEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
  16. jsonEncoder = zapcore.NewJSONEncoder(jsonEncoderConfig)
  17. consoleSync = zapcore.AddSync(os.Stdout)
  18. outputSync = zapcore.AddSync(&lumberjack.Logger{
  19. Filename: "logs/log",
  20. MaxSize: 1,
  21. MaxAge: 10,
  22. LocalTime: true,
  23. })
  24. log = NewLogger()
  25. }
  26. // With 见 Logger.With
  27. func With(k string, v any) *Logger {
  28. return log.With(k, v)
  29. }
  30. // Info 见 Logger.Info
  31. func Info(msg string, vs ...any) {
  32. log.Info(msg, vs...)
  33. }
  34. // Debug 见 Logger.Debug
  35. func Debug(msg string, vs ...any) {
  36. log.Debug(msg, vs...)
  37. }
  38. // Warn 见 Logger.Warn
  39. func Warn(msg string, vs ...any) {
  40. log.Warn(msg, vs...)
  41. }
  42. // Error 见 Logger.Error
  43. func Error(vs ...any) {
  44. log.Error(vs...)
  45. }
  46. // Lv 见 Logger.Lv
  47. func Lv() Level {
  48. return Level(log.Lv().Level())
  49. }
  50. // SetLv 见 Logger.SetLv
  51. func SetLv(lv Level) *Logger {
  52. log.SetLv(lv)
  53. return log
  54. }
  55. // NewFileOutput 见 Logger.NewFileOutput
  56. func NewFileOutput(opts ...FileOutputOpt) *Logger {
  57. log.NewFileOutput(opts...)
  58. return log
  59. }
  60. // NewOutput 见 Logger.NewOutput
  61. func NewOutput(writer io.Writer) *Logger {
  62. log.NewOutput(writer)
  63. return log
  64. }
  65. func Flush() {
  66. log.Flush()
  67. }