github.com/Jeffail/benthos/v3@v3.65.0/lib/log/wrap.go (about)

     1  package log
     2  
     3  //------------------------------------------------------------------------------
     4  
     5  // PrintFormatter is an interface implemented by standard loggers.
     6  type PrintFormatter interface {
     7  	Printf(format string, v ...interface{})
     8  	Println(v ...interface{})
     9  }
    10  
    11  //------------------------------------------------------------------------------
    12  
    13  // wrapped is an object with support for levelled logging and modular components.
    14  type wrapped struct {
    15  	pf    PrintFormatter
    16  	level int
    17  }
    18  
    19  // Wrap a PrintFormatter with a log.Modular implementation. Log level is set to
    20  // INFO, use WrapAtLevel to set this explicitly.
    21  func Wrap(l PrintFormatter) Modular {
    22  	return &wrapped{
    23  		pf:    l,
    24  		level: LogInfo,
    25  	}
    26  }
    27  
    28  // WrapAtLevel wraps a PrintFormatter with a log.Modular implementation with an
    29  // explicit log level.
    30  func WrapAtLevel(l PrintFormatter, level int) Modular {
    31  	return &wrapped{
    32  		pf:    l,
    33  		level: level,
    34  	}
    35  }
    36  
    37  func (l *wrapped) NewModule(prefix string) Modular {
    38  	return l
    39  }
    40  
    41  //------------------------------------------------------------------------------
    42  
    43  // WithFields is a no-op.
    44  func (l *wrapped) WithFields(fields map[string]string) Modular {
    45  	return l
    46  }
    47  
    48  // Fatalf prints a fatal message to the console. Does NOT cause panic.
    49  func (l *wrapped) Fatalf(format string, v ...interface{}) {
    50  	if LogFatal <= l.level {
    51  		l.pf.Printf(format, v...)
    52  	}
    53  }
    54  
    55  // Errorf prints an error message to the console.
    56  func (l *wrapped) Errorf(format string, v ...interface{}) {
    57  	if LogError <= l.level {
    58  		l.pf.Printf(format, v...)
    59  	}
    60  }
    61  
    62  // Warnf prints a warning message to the console.
    63  func (l *wrapped) Warnf(format string, v ...interface{}) {
    64  	if LogWarn <= l.level {
    65  		l.pf.Printf(format, v...)
    66  	}
    67  }
    68  
    69  // Infof prints an information message to the console.
    70  func (l *wrapped) Infof(format string, v ...interface{}) {
    71  	if LogInfo <= l.level {
    72  		l.pf.Printf(format, v...)
    73  	}
    74  }
    75  
    76  // Debugf prints a debug message to the console.
    77  func (l *wrapped) Debugf(format string, v ...interface{}) {
    78  	if LogDebug <= l.level {
    79  		l.pf.Printf(format, v...)
    80  	}
    81  }
    82  
    83  // Tracef prints a trace message to the console.
    84  func (l *wrapped) Tracef(format string, v ...interface{}) {
    85  	if LogTrace <= l.level {
    86  		l.pf.Printf(format, v...)
    87  	}
    88  }
    89  
    90  //------------------------------------------------------------------------------
    91  
    92  // Fatalln prints a fatal message to the console. Does NOT cause panic.
    93  func (l *wrapped) Fatalln(message string) {
    94  	if LogFatal <= l.level {
    95  		l.pf.Println(message)
    96  	}
    97  }
    98  
    99  // Errorln prints an error message to the console.
   100  func (l *wrapped) Errorln(message string) {
   101  	if LogError <= l.level {
   102  		l.pf.Println(message)
   103  	}
   104  }
   105  
   106  // Warnln prints a warning message to the console.
   107  func (l *wrapped) Warnln(message string) {
   108  	if LogWarn <= l.level {
   109  		l.pf.Println(message)
   110  	}
   111  }
   112  
   113  // Infoln prints an information message to the console.
   114  func (l *wrapped) Infoln(message string) {
   115  	if LogInfo <= l.level {
   116  		l.pf.Println(message)
   117  	}
   118  }
   119  
   120  // Debugln prints a debug message to the console.
   121  func (l *wrapped) Debugln(message string) {
   122  	if LogDebug <= l.level {
   123  		l.pf.Println(message)
   124  	}
   125  }
   126  
   127  // Traceln prints a trace message to the console.
   128  func (l *wrapped) Traceln(message string) {
   129  	if LogTrace <= l.level {
   130  		l.pf.Println(message)
   131  	}
   132  }
   133  
   134  //------------------------------------------------------------------------------