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.

126 lines
2.8 KiB

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 and stores the error message into the ErrorFile
  68. func Error(args ...interface{}) {
  69. log.Error(args...)
  70. }
  71. // Debugf calls log.Debugf
  72. func Debugf(template string, args ...interface{}) {
  73. log.Debugf(template, args...)
  74. }
  75. // Infof calls log.Infof
  76. func Infof(template string, args ...interface{}) {
  77. log.Infof(template, args...)
  78. }
  79. // Warnf calls log.Warnf
  80. func Warnf(template string, args ...interface{}) {
  81. log.Warnf(template, args...)
  82. }
  83. // Errorf calls log.Errorf and stores the error message into the ErrorFile
  84. func Errorf(template string, args ...interface{}) {
  85. log.Errorf(template, args...)
  86. }
  87. // Debugw calls log.Debugw
  88. func Debugw(template string, kv ...interface{}) {
  89. log.Debugw(template, kv...)
  90. }
  91. // Infow calls log.Infow
  92. func Infow(template string, kv ...interface{}) {
  93. log.Infow(template, kv...)
  94. }
  95. // Warnw calls log.Warnw
  96. func Warnw(template string, kv ...interface{}) {
  97. log.Warnw(template, kv...)
  98. }
  99. // Errorw calls log.Errorw and stores the error message into the ErrorFile
  100. func Errorw(template string, kv ...interface{}) {
  101. log.Errorw(template, kv...)
  102. }