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