log.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/pflag"
  10. "github.com/spf13/viper"
  11. )
  12. // TODO for deduped functions, if timeLogged > logTypeLimit, should we log once
  13. // every... 100 (?) times so we don't lose track entirely?
  14. // concurrency-safe counter
  15. var ctr = newCounter()
  16. const (
  17. flagFormat = "log-format"
  18. flagLevel = "log-level"
  19. flagDisableColor = "disable-log-color"
  20. )
  21. // InitLoggingFromFlags maps pflags (e.g. from a cobra command) representing
  22. // log level, format, and disable color to this logging package's expected
  23. // format in order to correctly initialize the logger. Passing a nil flag is
  24. // acceptable and will result in default behavior for that option.
  25. func InitLoggingFromFlags(level *pflag.Flag, format *pflag.Flag, disableColor *pflag.Flag) {
  26. viper.BindPFlag(flagLevel, level)
  27. viper.BindPFlag(flagFormat, format)
  28. viper.BindPFlag(flagDisableColor, disableColor)
  29. InitLogging()
  30. }
  31. func InitLogging() {
  32. zerolog.TimeFieldFormat = time.RFC3339Nano
  33. // Default to using pretty formatting
  34. if strings.ToLower(viper.GetString(flagFormat)) != "json" {
  35. disableColor := viper.GetBool(flagDisableColor)
  36. log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano, NoColor: disableColor})
  37. }
  38. level, err := zerolog.ParseLevel(viper.GetString(flagLevel))
  39. if err != nil {
  40. zerolog.SetGlobalLevel(zerolog.InfoLevel)
  41. log.Warn().Msg("Error parsing log-level, setting level to 'info'")
  42. return
  43. }
  44. zerolog.SetGlobalLevel(level)
  45. log.Log().Msgf("Log level set to %v", level)
  46. }
  47. func Errorf(format string, a ...interface{}) {
  48. log.Error().Msgf(format, a...)
  49. }
  50. func DedupedErrorf(logTypeLimit int, format string, a ...interface{}) {
  51. timesLogged := ctr.increment(format)
  52. if timesLogged < logTypeLimit {
  53. Errorf(format, a...)
  54. } else if timesLogged == logTypeLimit {
  55. Errorf(format, a...)
  56. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  57. }
  58. }
  59. func Warnf(format string, a ...interface{}) {
  60. log.Warn().Msgf(format, a...)
  61. }
  62. func DedupedWarningf(logTypeLimit int, format string, a ...interface{}) {
  63. timesLogged := ctr.increment(format)
  64. if timesLogged < logTypeLimit {
  65. Warnf(format, a...)
  66. } else if timesLogged == logTypeLimit {
  67. Warnf(format, a...)
  68. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  69. }
  70. }
  71. func Info(msg string) {
  72. log.Info().Msg(msg)
  73. }
  74. func Infof(format string, a ...interface{}) {
  75. log.Info().Msgf(format, a...)
  76. }
  77. func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
  78. timesLogged := ctr.increment(format)
  79. if timesLogged < logTypeLimit {
  80. Infof(format, a...)
  81. } else if timesLogged == logTypeLimit {
  82. Infof(format, a...)
  83. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  84. }
  85. }
  86. func Profilef(format string, a ...interface{}) {
  87. log.Info().Msgf(fmt.Sprintf("[Profiler] %s", format), a...)
  88. }
  89. func Debug(msg string) {
  90. log.Debug().Msg(msg)
  91. }
  92. func Debugf(format string, a ...interface{}) {
  93. log.Debug().Msgf(format, a...)
  94. }
  95. func Fatalf(format string, a ...interface{}) {
  96. log.Fatal().Msgf(format, a...)
  97. }
  98. func Profile(start time.Time, name string) {
  99. elapsed := time.Since(start)
  100. Profilef("%s: %s", elapsed, name)
  101. }
  102. func ProfileWithThreshold(start time.Time, threshold time.Duration, name string) {
  103. elapsed := time.Since(start)
  104. if elapsed > threshold {
  105. Profilef("%s: %s", elapsed, name)
  106. }
  107. }