github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/logger/periodic_log.go (about)

     1  package logger
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Periodic is the same as logger.Instance, but writes only once in a period
     8  type Periodic struct {
     9  	Instance
    10  	prevLogTime time.Time
    11  }
    12  
    13  // Info is timed log.Info
    14  func (l *Periodic) Info(period time.Duration, msg string, ctx ...interface{}) {
    15  	if time.Since(l.prevLogTime) > period {
    16  		l.Log.Info(msg, ctx...)
    17  		l.prevLogTime = time.Now()
    18  	}
    19  }
    20  
    21  // Warn is timed log.Warn
    22  func (l *Periodic) Warn(period time.Duration, msg string, ctx ...interface{}) {
    23  	if time.Since(l.prevLogTime) > period {
    24  		l.Log.Warn(msg, ctx...)
    25  		l.prevLogTime = time.Now()
    26  	}
    27  }
    28  
    29  // Error is timed log.Error
    30  func (l *Periodic) Error(period time.Duration, msg string, ctx ...interface{}) {
    31  	if time.Since(l.prevLogTime) > period {
    32  		l.Log.Error(msg, ctx...)
    33  		l.prevLogTime = time.Now()
    34  	}
    35  }
    36  
    37  // Debug is timed log.Debug
    38  func (l *Periodic) Debug(period time.Duration, msg string, ctx ...interface{}) {
    39  	if time.Since(l.prevLogTime) > period {
    40  		l.Log.Debug(msg, ctx...)
    41  		l.prevLogTime = time.Now()
    42  	}
    43  }
    44  
    45  // Trace is timed log.Trace
    46  func (l *Periodic) Trace(period time.Duration, msg string, ctx ...interface{}) {
    47  	if time.Since(l.prevLogTime) > period {
    48  		l.Log.Trace(msg, ctx...)
    49  		l.prevLogTime = time.Now()
    50  	}
    51  }