lesslogger.go 725 B

123456789101112131415161718192021222324252627
  1. package logx
  2. // A LessLogger is a logger that control to log once during the given duration.
  3. type LessLogger struct {
  4. *limitedExecutor
  5. }
  6. // NewLessLogger returns a LessLogger.
  7. func NewLessLogger(milliseconds int) *LessLogger {
  8. return &LessLogger{
  9. limitedExecutor: newLimitedExecutor(milliseconds),
  10. }
  11. }
  12. // Error logs v into error log or discard it if more than once in the given duration.
  13. func (logger *LessLogger) Error(v ...interface{}) {
  14. logger.logOrDiscard(func() {
  15. Error(v...)
  16. })
  17. }
  18. // Errorf logs v with format into error log or discard it if more than once in the given duration.
  19. func (logger *LessLogger) Errorf(format string, v ...interface{}) {
  20. logger.logOrDiscard(func() {
  21. Errorf(format, v...)
  22. })
  23. }