github.com/Cloud-Foundations/Dominator@v0.3.4/lib/log/testlogger/api.go (about)

     1  package testlogger
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  type sprintFunc func(v ...interface{}) string
     8  type sprintfFunc func(format string, v ...interface{}) string
     9  
    10  type Logger struct {
    11  	logger    TestLogger
    12  	sprint    sprintFunc
    13  	sprintf   sprintfFunc
    14  	startTime time.Time
    15  }
    16  
    17  // TestLogger defines an interface for a type that can be used for logging by
    18  // tests. The testing.T type from the standard library satisfies this interface.
    19  type TestLogger interface {
    20  	Fatal(v ...interface{})
    21  	Fatalf(format string, v ...interface{})
    22  	Log(v ...interface{})
    23  	Logf(format string, v ...interface{})
    24  }
    25  
    26  // New will create a Logger from a TestLogger. The Logger that is created
    27  // satisfies the log.DebugLogger interface and thus may be used widely. It
    28  // serves as an adaptor between the testing.T type from the standard library and
    29  // library code that expects a generic logging type.
    30  // Trailing newlines are removed before calling the TestLogger methods.
    31  func New(logger TestLogger) *Logger {
    32  	return newTestlogger(logger)
    33  }
    34  
    35  // NewWithTimestamps is the same as New, except that timestamps (since creating
    36  // the logger) are included in the log messages.
    37  // Trailing newlines are removed before calling the TestLogger methods.
    38  func NewWithTimestamps(logger TestLogger) *Logger {
    39  	return newWithTimestamps(logger)
    40  }
    41  
    42  // Debug will call the Log method of the underlying TestLogger, regardless of
    43  // the debug level.
    44  func (l *Logger) Debug(level uint8, v ...interface{}) {
    45  	l.logger.Log(l.sprint(v...))
    46  }
    47  
    48  // Debugf is similar to Debug, with formatting support.
    49  func (l *Logger) Debugf(level uint8, format string, v ...interface{}) {
    50  	l.logger.Log(l.sprintf(format, v...))
    51  }
    52  
    53  // Debugln is similar to Debug.
    54  func (l *Logger) Debugln(level uint8, v ...interface{}) {
    55  	l.logger.Log(l.sprint(v...))
    56  }
    57  
    58  // Fatal will call the Fatal method of the underlying TestLogger.
    59  func (l *Logger) Fatal(v ...interface{}) {
    60  	l.logger.Fatal(l.sprint(v...))
    61  }
    62  
    63  // Fatalf is similar to Fatal, with formatting support.
    64  func (l *Logger) Fatalf(format string, v ...interface{}) {
    65  	l.logger.Fatal(l.sprintf(format, v...))
    66  }
    67  
    68  // Fatalln is similar to Fatal.
    69  func (l *Logger) Fatalln(v ...interface{}) {
    70  	l.logger.Fatal(l.sprint(v...))
    71  }
    72  
    73  // Panic will call the Fatal method of the underlying TestLogger and will then
    74  // call panic.
    75  func (l *Logger) Panic(v ...interface{}) {
    76  	s := l.sprint(v...)
    77  	l.logger.Fatal(s)
    78  	panic(s)
    79  }
    80  
    81  // Panicf is similar to Panic, with formatting support.
    82  func (l *Logger) Panicf(format string, v ...interface{}) {
    83  	s := l.sprintf(format, v...)
    84  	l.logger.Fatal(s)
    85  	panic(s)
    86  }
    87  
    88  // Panicln is similar to Panic.
    89  func (l *Logger) Panicln(v ...interface{}) {
    90  	s := l.sprint(v...)
    91  	l.logger.Fatal(s)
    92  	panic(s)
    93  }
    94  
    95  // Print will call the Log method of the underlying TestLogger.
    96  func (l *Logger) Print(v ...interface{}) {
    97  	l.logger.Log(l.sprint(v...))
    98  }
    99  
   100  // Printf is similar to Print, with formatting support.
   101  func (l *Logger) Printf(format string, v ...interface{}) {
   102  	l.logger.Log(l.sprintf(format, v...))
   103  }
   104  
   105  // Println is similar to Print.
   106  func (l *Logger) Println(v ...interface{}) {
   107  	l.logger.Log(l.sprint(v...))
   108  }