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.

121 lines
2.7 KiB

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. log = logger.Sugar()
  50. if errorsPath != "" {
  51. log.Infof("file where errors will be written: %s", errorsPath)
  52. errorsFile, err = os.OpenFile(errorsPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
  53. if err != nil {
  54. panic(err)
  55. }
  56. }
  57. log.Infof("log level: %s", level)
  58. }
  59. func writeToErrorsFile(msg string) {
  60. if errorsFile == nil {
  61. return
  62. }
  63. //nolint:errcheck
  64. errorsFile.WriteString(fmt.Sprintf("%s %s\n", time.Now().Format(time.RFC3339), msg))
  65. }
  66. // Debug calls log.Debug
  67. func Debug(args ...interface{}) {
  68. log.Debug(args...)
  69. }
  70. // Info calls log.Info
  71. func Info(args ...interface{}) {
  72. log.Info(args...)
  73. }
  74. // Warn calls log.Warn
  75. func Warn(args ...interface{}) {
  76. log.Warn(args...)
  77. }
  78. // Error calls log.Error and stores the error message into the ErrorFile
  79. func Error(args ...interface{}) {
  80. log.Error(args...)
  81. go writeToErrorsFile(fmt.Sprint(args...))
  82. }
  83. // Debugf calls log.Debugf
  84. func Debugf(template string, args ...interface{}) {
  85. log.Debugf(template, args...)
  86. }
  87. // Infof calls log.Infof
  88. func Infof(template string, args ...interface{}) {
  89. log.Infof(template, args...)
  90. }
  91. // Warnf calls log.Warnf
  92. func Warnf(template string, args ...interface{}) {
  93. log.Warnf(template, args...)
  94. }
  95. // Errorf calls log.Errorf and stores the error message into the ErrorFile
  96. func Errorf(template string, args ...interface{}) {
  97. log.Errorf(template, args...)
  98. go writeToErrorsFile(fmt.Sprintf(template, args...))
  99. }