log.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/rs/zerolog"
  8. "github.com/rs/zerolog/log"
  9. "github.com/spf13/viper"
  10. )
  11. // TODO for deduped functions, if timeLogged > logTypeLimit, should we log once
  12. // every... 100 (?) times so we don't lose track entirely?
  13. // concurrency-safe counter
  14. var ctr = newCounter()
  15. func InitLogging() {
  16. zerolog.TimeFieldFormat = time.RFC3339Nano
  17. // Default to using pretty formatting
  18. if strings.ToLower(viper.GetString("log-format")) != "json" {
  19. log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano})
  20. }
  21. level, err := zerolog.ParseLevel(viper.GetString("log-level"))
  22. if err != nil {
  23. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  24. log.Warn().Msg("Error parsing log-level, setting level to 'info'")
  25. return
  26. }
  27. zerolog.SetGlobalLevel(level)
  28. log.Log().Msgf("Log level set to %v", level)
  29. }
  30. func Errorf(format string, a ...interface{}) {
  31. log.Error().Msgf(format, a...)
  32. }
  33. func DedupedErrorf(logTypeLimit int, format string, a ...interface{}) {
  34. timesLogged := ctr.increment(format)
  35. if timesLogged < logTypeLimit {
  36. Errorf(format, a...)
  37. } else if timesLogged == logTypeLimit {
  38. Errorf(format, a...)
  39. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  40. }
  41. }
  42. func Warnf(format string, a ...interface{}) {
  43. log.Warn().Msgf(format, a...)
  44. }
  45. func DedupedWarningf(logTypeLimit int, format string, a ...interface{}) {
  46. timesLogged := ctr.increment(format)
  47. if timesLogged < logTypeLimit {
  48. Warnf(format, a...)
  49. } else if timesLogged == logTypeLimit {
  50. Warnf(format, a...)
  51. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  52. }
  53. }
  54. func Info(msg string) {
  55. log.Info().Msg(msg)
  56. }
  57. func Infof(format string, a ...interface{}) {
  58. log.Info().Msgf(format, a...)
  59. }
  60. func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
  61. timesLogged := ctr.increment(format)
  62. if timesLogged < logTypeLimit {
  63. Infof(format, a...)
  64. } else if timesLogged == logTypeLimit {
  65. Infof(format, a...)
  66. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  67. }
  68. }
  69. func Profilef(format string, a ...interface{}) {
  70. log.Info().Msgf(fmt.Sprintf("[Profiler] %s", format), a...)
  71. }
  72. func Debug(msg string) {
  73. log.Debug().Msg(msg)
  74. }
  75. func Debugf(format string, a ...interface{}) {
  76. log.Debug().Msgf(format, a...)
  77. }
  78. func Fatalf(format string, a ...interface{}) {
  79. log.Fatal().Msgf(format, a...)
  80. }
  81. func Profile(start time.Time, name string) {
  82. elapsed := time.Since(start)
  83. Profilef("%s: %s", elapsed, name)
  84. }
  85. func ProfileWithThreshold(start time.Time, threshold time.Duration, name string) {
  86. elapsed := time.Since(start)
  87. if elapsed > threshold {
  88. Profilef("%s: %s", elapsed, name)
  89. }
  90. }