log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package log
  2. import (
  3. "fmt"
  4. "time"
  5. "k8s.io/klog"
  6. )
  7. var seen = make(map[string]int)
  8. func Errorf(format string, a ...interface{}) {
  9. klog.Errorf(fmt.Sprintf("[Error] %s", format), a...)
  10. }
  11. func DedupedErrorf(logTypeLimit int, format string, a ...interface{}) {
  12. timesLogged, ok := seen[format]
  13. if !ok {
  14. seen[format] = 1
  15. } else if timesLogged == logTypeLimit {
  16. seen[format]++
  17. f := fmt.Sprintf("[Error] %s", format)
  18. klog.Errorf("%s seen %d times, suppressing future logs", f, logTypeLimit)
  19. } else if timesLogged > logTypeLimit {
  20. seen[format]++
  21. } else {
  22. seen[format]++
  23. klog.Errorf(fmt.Sprintf("[Error] %s", format), a...)
  24. }
  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. timesLogged, ok := seen[format]
  31. if !ok {
  32. seen[format] = 1
  33. } else if timesLogged == logTypeLimit {
  34. seen[format]++
  35. f := fmt.Sprintf("[Warning] %s", format)
  36. klog.Errorf("%s seen %d times, suppressing future logs", f, logTypeLimit)
  37. } else if timesLogged > logTypeLimit {
  38. seen[format]++
  39. } else {
  40. seen[format]++
  41. klog.V(2).Infof(fmt.Sprintf("[Warning] %s", format), a...)
  42. }
  43. }
  44. func Infof(format string, a ...interface{}) {
  45. klog.V(3).Infof(fmt.Sprintf("[Info] %s", format), a...)
  46. }
  47. func DedupedInfof(logTypeLimit int, format string, a ...interface{}) {
  48. timesLogged, ok := seen[format]
  49. if !ok {
  50. seen[format] = 1
  51. } else if timesLogged == logTypeLimit {
  52. seen[format]++
  53. f := fmt.Sprintf("[Info] %s", format)
  54. klog.Errorf("%s seen %d times, suppressing future logs", f, logTypeLimit)
  55. } else if timesLogged > logTypeLimit {
  56. seen[format]++
  57. } else {
  58. seen[format]++
  59. klog.V(3).Infof(fmt.Sprintf("[Info] %s", format), a...)
  60. }
  61. }
  62. func Profilef(format string, a ...interface{}) {
  63. klog.V(3).Infof(fmt.Sprintf("[Profiler] %s", format), a...)
  64. }
  65. func Debugf(format string, a ...interface{}) {
  66. klog.V(4).Infof(fmt.Sprintf("[Debug] %s", format), a...)
  67. }
  68. func Profile(start time.Time, name string) {
  69. elapsed := time.Since(start)
  70. Profilef("%s: %s", elapsed, name)
  71. }
  72. func ProfileWithThreshold(start time.Time, threshold time.Duration, name string) {
  73. elapsed := time.Since(start)
  74. if elapsed > threshold {
  75. Profilef("%s: %s", elapsed, name)
  76. }
  77. }
  78. type Profiler struct {
  79. profiles map[string]time.Duration
  80. starts map[string]time.Time
  81. }
  82. func NewProfiler() *Profiler {
  83. return &Profiler{
  84. profiles: map[string]time.Duration{},
  85. starts: map[string]time.Time{},
  86. }
  87. }
  88. func (p *Profiler) Start(name string) {
  89. if p == nil {
  90. return
  91. }
  92. p.starts[name] = time.Now()
  93. }
  94. func (p *Profiler) Stop(name string) time.Duration {
  95. if p == nil {
  96. return 0
  97. }
  98. if start, ok := p.starts[name]; ok {
  99. elapsed := time.Since(start)
  100. p.profiles[name] += elapsed
  101. return elapsed
  102. }
  103. return 0
  104. }
  105. func (p *Profiler) Log(name string) {
  106. if p == nil {
  107. return
  108. }
  109. Profilef("%s: %s", p.profiles[name], name)
  110. }
  111. func (p *Profiler) LogAll() {
  112. if p == nil {
  113. return
  114. }
  115. // Print profiles, largest to smallest. (Inefficienct, but shouldn't matter.)
  116. print := map[string]time.Duration{}
  117. for name, value := range p.profiles {
  118. print[name] = value
  119. }
  120. for len(print) > 0 {
  121. largest := ""
  122. for name := range print {
  123. if print[name] > print[largest] {
  124. largest = name
  125. }
  126. }
  127. Profilef("%s: %s", print[largest], largest)
  128. delete(print, largest)
  129. }
  130. }