互动
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.

110 lines
2.6 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
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. "github.com/go-stack/stack"
  4. "go.uber.org/zap"
  5. "go.uber.org/zap/zapcore"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "os"
  8. "path/filepath"
  9. "time"
  10. )
  11. //var Sugar *zap.SugaredLogger
  12. var logger *zap.SugaredLogger
  13. func init() {
  14. SetLevel("debug")
  15. }
  16. // @func SetLevel 设置日志等级
  17. // @param level 日志等级从高大底: fatal panic dpanic error warn info debug
  18. func SetLevel(level string) {
  19. wd, _ := os.Getwd()
  20. SetLogger(filepath.Join(wd, "log", "hdzj.log"), level, 1024, 10, 7)
  21. }
  22. // @func SetLogger 日志定制化, 包括日志切割的定制化
  23. // @param filename 存放日志的文件, 最好带上路径
  24. // @param level 日志写入的等级
  25. // @param size 单个日志文件的大小
  26. // @param backups 日志备份的数量
  27. // @param age 日志存放的时间
  28. func SetLogger(filename, level string, size, backups, age int) {
  29. w := zapcore.AddSync(&lumberjack.Logger{
  30. Filename: filename,
  31. MaxSize: size, // megabytes
  32. MaxBackups: backups,
  33. MaxAge: age, // days
  34. })
  35. core := zapcore.NewCore(
  36. zapcore.NewConsoleEncoder(NewEncoderConfig()),
  37. zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), w),
  38. Level(level))
  39. logger = zap.New(core, zap.AddCaller()).Sugar()
  40. }
  41. func NewEncoderConfig() zapcore.EncoderConfig {
  42. return zapcore.EncoderConfig{
  43. // Keys can be anything except the empty string.
  44. TimeKey: "T",
  45. LevelKey: "L",
  46. NameKey: "N",
  47. CallerKey: "C",
  48. MessageKey: "M",
  49. StacktraceKey: "S",
  50. LineEnding: zapcore.DefaultLineEnding,
  51. EncodeLevel: zapcore.CapitalLevelEncoder,
  52. EncodeTime: TimeEncoder,
  53. EncodeDuration: zapcore.StringDurationEncoder,
  54. EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
  55. frame := stack.Caller(7).Frame()
  56. caller.Line = frame.Line
  57. caller.File = frame.File
  58. encoder.AppendString(caller.TrimmedPath())
  59. },
  60. }
  61. }
  62. func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  63. enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
  64. }
  65. func Level(level string) zapcore.Level {
  66. switch level {
  67. case "info":
  68. return zap.InfoLevel
  69. case "fatal":
  70. return zap.FatalLevel
  71. case "panic":
  72. return zap.PanicLevel
  73. case "error":
  74. return zap.ErrorLevel
  75. case "dpanic":
  76. return zap.DPanicLevel
  77. case "warn":
  78. return zap.WarnLevel
  79. default:
  80. return zap.DebugLevel
  81. }
  82. }
  83. func Info(args ...interface{}) {
  84. logger.Info(args...)
  85. }
  86. func Debug(args ...interface{}) {
  87. logger.Debug(args...)
  88. }
  89. func Panic(args ...interface{}) {
  90. logger.Panic(args...)
  91. }
  92. func Warn(args ...interface{}) {
  93. logger.Warn(args...)
  94. }
  95. func Error(args ...interface{}) {
  96. logger.Error(args...)
  97. }