互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package logger
  2. import (
  3. "go.uber.org/zap"
  4. "go.uber.org/zap/zapcore"
  5. "gopkg.in/natefinch/lumberjack.v2"
  6. "os"
  7. "time"
  8. )
  9. //var Sugar *zap.SugaredLogger
  10. var logger *zap.SugaredLogger
  11. func init() {
  12. InitZap()
  13. }
  14. func InitZap() {
  15. w := zapcore.AddSync(&lumberjack.Logger{
  16. Filename: "log/hdzj.log",
  17. MaxSize: 50, // megabytes
  18. MaxBackups: 3,
  19. MaxAge: 7, // days
  20. })
  21. core := zapcore.NewCore(
  22. zapcore.NewConsoleEncoder(NewEncoderConfig()),
  23. zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), w),
  24. zap.DebugLevel)
  25. logger = zap.New(core, zap.AddCaller()).Sugar()
  26. }
  27. func NewEncoderConfig() zapcore.EncoderConfig {
  28. return zapcore.EncoderConfig{
  29. // Keys can be anything except the empty string.
  30. TimeKey: "T",
  31. LevelKey: "L",
  32. NameKey: "N",
  33. CallerKey: "C",
  34. MessageKey: "M",
  35. StacktraceKey: "S",
  36. LineEnding: zapcore.DefaultLineEnding,
  37. EncodeLevel: zapcore.CapitalLevelEncoder,
  38. EncodeTime: TimeEncoder,
  39. EncodeDuration: zapcore.StringDurationEncoder,
  40. EncodeCaller: zapcore.ShortCallerEncoder,
  41. }
  42. }
  43. func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  44. enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
  45. }
  46. func level(level string) zapcore.Level {
  47. switch level {
  48. case "info":
  49. return zap.InfoLevel
  50. case "fatal":
  51. return zap.FatalLevel
  52. case "panic":
  53. return zap.PanicLevel
  54. case "error":
  55. return zap.ErrorLevel
  56. case "dpanic":
  57. return zap.DPanicLevel
  58. case "warn":
  59. return zap.WarnLevel
  60. default:
  61. return zap.DebugLevel
  62. }
  63. }
  64. func Info(args ...interface{}) {
  65. logger.Info(args...)
  66. }
  67. func Debug(args ...interface{}) {
  68. logger.Debug(args...)
  69. }
  70. func Panic(args ...interface{}) {
  71. logger.Panic(args...)
  72. }
  73. func Warn(args ...interface{}) {
  74. logger.Warn(args...)
  75. }
  76. func Error(args ...interface{}) {
  77. logger.Error(args...)
  78. }