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.

122 lines
2.8 KiB

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