log.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Run within a goroutine so that the original call does not block
  16. go func(logTypeLimit int, format string, a ...interface{}) {
  17. timesLogged := ctr.increment(format)
  18. if timesLogged < logTypeLimit {
  19. Errorf(format, a...)
  20. } else if timesLogged == logTypeLimit {
  21. Errorf(format, a...)
  22. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  23. }
  24. }(logTypeLimit, format, a...)
  25. }
  26. func Warningf(format string, a ...interface{}) {
  27. klog.V(2).Infof(fmt.Sprintf("[Warning] %s", format), a...)
  28. }
  29. func DedupedWarningf(logTypeLimit int, format string, a ...interface{}) {
  30. // Run within a goroutine so that the original call does not block
  31. go func(logTypeLimit int, format string, a ...interface{}) {
  32. timesLogged := ctr.increment(format)
  33. if timesLogged < logTypeLimit {
  34. Warningf(format, a...)
  35. } else if timesLogged == logTypeLimit {
  36. Warningf(format, a...)
  37. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  38. }
  39. }(logTypeLimit, format, a...)
  40. }
  41. func Infof(format string, a ...interface{}) {
  42. klog.V(3).Infof(fmt.Sprintf("[Info] %s", format), a...)
  43. }
  44. func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
  45. // Run within a goroutine so that the original call does not block
  46. go func(logTypeLimit int, format string, a ...interface{}) {
  47. timesLogged := ctr.increment(format)
  48. if timesLogged < logTypeLimit {
  49. Infof(format, a...)
  50. } else if timesLogged == logTypeLimit {
  51. Infof(format, a...)
  52. Infof("%s logged %d times: suppressing future logs", format, logTypeLimit)
  53. }
  54. }(logTypeLimit, format, a...)
  55. }
  56. func Profilef(format string, a ...interface{}) {
  57. klog.V(3).Infof(fmt.Sprintf("[Profiler] %s", format), a...)
  58. }
  59. func Debugf(format string, a ...interface{}) {
  60. klog.V(4).Infof(fmt.Sprintf("[Debug] %s", format), a...)
  61. }
  62. func Profile(start time.Time, name string) {
  63. elapsed := time.Since(start)
  64. Profilef("%s: %s", elapsed, name)
  65. }
  66. func ProfileWithThreshold(start time.Time, threshold time.Duration, name string) {
  67. elapsed := time.Since(start)
  68. if elapsed > threshold {
  69. Profilef("%s: %s", elapsed, name)
  70. }
  71. }