log.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package log
  2. import (
  3. "fmt"
  4. "time"
  5. "k8s.io/klog"
  6. )
  7. // TODO for deduped functions, if timeLogged > logTypeLimit, should we log once
  8. // every... 100 (?) times so we don't lose track entirely?
  9. // concurrency-safe counter
  10. var ctr = newCounter()
  11. func Errorf(format string, a ...interface{}) {
  12. klog.Errorf(fmt.Sprintf("[Error] %s", format), a...)
  13. }
  14. func DedupedErrorf(logTypeLimit int, format string, a ...interface{}) {
  15. timesLogged := ctr.increment(format)
  16. if timesLogged < logTypeLimit {
  17. Errorf(format, a...)
  18. } else if timesLogged == logTypeLimit {
  19. Errorf(format, a...)
  20. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  21. }
  22. }
  23. func Warningf(format string, a ...interface{}) {
  24. klog.V(2).Infof(fmt.Sprintf("[Warning] %s", format), a...)
  25. }
  26. func DedupedWarningf(logTypeLimit int, format string, a ...interface{}) {
  27. timesLogged := ctr.increment(format)
  28. if timesLogged < logTypeLimit {
  29. Warningf(format, a...)
  30. } else if timesLogged == logTypeLimit {
  31. Warningf(format, a...)
  32. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  33. }
  34. }
  35. func Infof(format string, a ...interface{}) {
  36. klog.V(3).Infof(fmt.Sprintf("[Info] %s", format), a...)
  37. }
  38. func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
  39. timesLogged := ctr.increment(format)
  40. if timesLogged < logTypeLimit {
  41. Infof(format, a...)
  42. } else if timesLogged == logTypeLimit {
  43. Infof(format, a...)
  44. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  45. }
  46. }
  47. func Profilef(format string, a ...interface{}) {
  48. klog.V(3).Infof(fmt.Sprintf("[Profiler] %s", format), a...)
  49. }
  50. func Debugf(format string, a ...interface{}) {
  51. klog.V(5).Infof(fmt.Sprintf("[Debug] %s", format), a...)
  52. }
  53. func Profile(start time.Time, name string) {
  54. elapsed := time.Since(start)
  55. Profilef("%s: %s", elapsed, name)
  56. }
  57. func ProfileWithThreshold(start time.Time, threshold time.Duration, name string) {
  58. elapsed := time.Since(start)
  59. if elapsed > threshold {
  60. Profilef("%s: %s", elapsed, name)
  61. }
  62. }