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.

141 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package log
  2. import (
  3. "fmt"
  4. "time"
  5. "go.uber.org/zap"
  6. "go.uber.org/zap/zapcore"
  7. )
  8. var log *zap.SugaredLogger
  9. func init() {
  10. // default level: debug
  11. Init("debug", "")
  12. }
  13. // Init the logger with defined level. errorsPath defines the file where to store the errors, if set to "" will not store errors.
  14. func Init(levelStr, logPath string) {
  15. var level zap.AtomicLevel
  16. err := level.UnmarshalText([]byte(levelStr))
  17. if err != nil {
  18. panic(fmt.Errorf("Error on setting log level: %s", err))
  19. }
  20. outputPaths := []string{"stdout"}
  21. if logPath != "" {
  22. log.Infof("log file: %s", logPath)
  23. outputPaths = append(outputPaths, logPath)
  24. }
  25. cfg := zap.Config{
  26. Level: level,
  27. Encoding: "console",
  28. OutputPaths: outputPaths,
  29. ErrorOutputPaths: []string{"stderr"},
  30. EncoderConfig: zapcore.EncoderConfig{
  31. MessageKey: "message",
  32. LevelKey: "level",
  33. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  34. TimeKey: "timestamp",
  35. EncodeTime: func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
  36. encoder.AppendString(ts.Local().Format(time.RFC3339))
  37. },
  38. EncodeDuration: zapcore.SecondsDurationEncoder,
  39. CallerKey: "caller",
  40. EncodeCaller: zapcore.ShortCallerEncoder,
  41. StacktraceKey: "stacktrace",
  42. LineEnding: zapcore.DefaultLineEnding,
  43. },
  44. }
  45. logger, err := cfg.Build()
  46. if err != nil {
  47. panic(err)
  48. }
  49. //nolint:errcheck
  50. defer logger.Sync()
  51. withOptions := logger.WithOptions(zap.AddCallerSkip(1))
  52. log = withOptions.Sugar()
  53. log.Infof("log level: %s", level)
  54. }
  55. // Debug calls log.Debug
  56. func Debug(args ...interface{}) {
  57. log.Debug(args...)
  58. }
  59. // Info calls log.Info
  60. func Info(args ...interface{}) {
  61. log.Info(args...)
  62. }
  63. // Warn calls log.Warn
  64. func Warn(args ...interface{}) {
  65. log.Warn(args...)
  66. }
  67. // Error calls log.Error
  68. func Error(args ...interface{}) {
  69. log.Error(args...)
  70. }
  71. // Fatal calls log.Fatal
  72. func Fatal(args ...interface{}) {
  73. log.Fatal(args...)
  74. }
  75. // Debugf calls log.Debugf
  76. func Debugf(template string, args ...interface{}) {
  77. log.Debugf(template, args...)
  78. }
  79. // Infof calls log.Infof
  80. func Infof(template string, args ...interface{}) {
  81. log.Infof(template, args...)
  82. }
  83. // Warnf calls log.Warnf
  84. func Warnf(template string, args ...interface{}) {
  85. log.Warnf(template, args...)
  86. }
  87. // Fatalf calls log.Warnf
  88. func Fatalf(template string, args ...interface{}) {
  89. log.Fatalf(template, args...)
  90. }
  91. // Errorf calls log.Errorf and stores the error message into the ErrorFile
  92. func Errorf(template string, args ...interface{}) {
  93. log.Errorf(template, args...)
  94. }
  95. // Debugw calls log.Debugw
  96. func Debugw(template string, kv ...interface{}) {
  97. log.Debugw(template, kv...)
  98. }
  99. // Infow calls log.Infow
  100. func Infow(template string, kv ...interface{}) {
  101. log.Infow(template, kv...)
  102. }
  103. // Warnw calls log.Warnw
  104. func Warnw(template string, kv ...interface{}) {
  105. log.Warnw(template, kv...)
  106. }
  107. // Errorw calls log.Errorw
  108. func Errorw(template string, kv ...interface{}) {
  109. log.Errorw(template, kv...)
  110. }
  111. // Fatalw calls log.Fatalw
  112. func Fatalw(template string, kv ...interface{}) {
  113. log.Fatalw(template, kv...)
  114. }