github.com/decred/dcrlnd@v0.7.6/build/prefix_log.go (about) 1 package build 2 3 import "github.com/decred/slog" 4 5 // PrefixLog is a pass-through logger that adds a prefix to every logged line. 6 type PrefixLog struct { 7 log slog.Logger 8 prefix string 9 } 10 11 // NewPrefixLog instantiates a new prefixed logger. 12 func NewPrefixLog(prefix string, log slog.Logger) *PrefixLog { 13 return &PrefixLog{ 14 prefix: prefix, 15 log: log, 16 } 17 } 18 19 // addFormatPrefix prepends the prefix to a format string. 20 func (p *PrefixLog) addFormatPrefix(s string) string { 21 return p.prefix + " " + s 22 } 23 24 // addArgsPrefix prepends the prefix to a list of arguments. 25 func (p *PrefixLog) addArgsPrefix(args []interface{}) []interface{} { 26 return append([]interface{}{p.prefix}, args...) 27 } 28 29 // Tracef formats message according to format specifier and writes to to log 30 // with LevelTrace. 31 func (p *PrefixLog) Tracef(format string, params ...interface{}) { 32 p.log.Tracef(p.addFormatPrefix(format), params...) 33 } 34 35 // Debugf formats message according to format specifier and writes to log with 36 // LevelDebug. 37 func (p *PrefixLog) Debugf(format string, params ...interface{}) { 38 p.log.Debugf(p.addFormatPrefix(format), params...) 39 } 40 41 // Infof formats message according to format specifier and writes to log with 42 // LevelInfo. 43 func (p *PrefixLog) Infof(format string, params ...interface{}) { 44 p.log.Infof(p.addFormatPrefix(format), params...) 45 } 46 47 // Warnf formats message according to format specifier and writes to to log with 48 // LevelWarn. 49 func (p *PrefixLog) Warnf(format string, params ...interface{}) { 50 p.log.Warnf(p.addFormatPrefix(format), params...) 51 } 52 53 // Errorf formats message according to format specifier and writes to to log 54 // with LevelError. 55 func (p *PrefixLog) Errorf(format string, params ...interface{}) { 56 p.log.Errorf(p.addFormatPrefix(format), params...) 57 } 58 59 // Criticalf formats message according to format specifier and writes to log 60 // with LevelCritical. 61 func (p *PrefixLog) Criticalf(format string, params ...interface{}) { 62 p.log.Criticalf(p.addFormatPrefix(format), params...) 63 } 64 65 // Trace formats message using the default formats for its operands and writes 66 // to log with LevelTrace. 67 func (p *PrefixLog) Trace(v ...interface{}) { 68 p.log.Trace(p.addArgsPrefix(v)...) 69 } 70 71 // Debug formats message using the default formats for its operands and writes 72 // to log with LevelDebug. 73 func (p *PrefixLog) Debug(v ...interface{}) { 74 p.log.Debug(p.addArgsPrefix(v)...) 75 } 76 77 // Info formats message using the default formats for its operands and writes to 78 // log with LevelInfo. 79 func (p *PrefixLog) Info(v ...interface{}) { 80 p.log.Info(p.addArgsPrefix(v)...) 81 } 82 83 // Warn formats message using the default formats for its operands and writes to 84 // log with LevelWarn. 85 func (p *PrefixLog) Warn(v ...interface{}) { 86 p.log.Warn(p.addArgsPrefix(v)...) 87 } 88 89 // Error formats message using the default formats for its operands and writes 90 // to log with LevelError. 91 func (p *PrefixLog) Error(v ...interface{}) { 92 p.log.Error(p.addArgsPrefix(v)...) 93 } 94 95 // Critical formats message using the default formats for its operands and 96 // writes to log with LevelCritical. 97 func (p *PrefixLog) Critical(v ...interface{}) { 98 p.log.Critical(p.addArgsPrefix(v)...) 99 } 100 101 // Level returns the current logging level. 102 func (p *PrefixLog) Level() slog.Level { 103 return p.log.Level() 104 } 105 106 // SetLevel changes the logging level to the passed level. 107 func (p *PrefixLog) SetLevel(level slog.Level) { 108 p.log.SetLevel(level) 109 } 110 111 // Assert that PrefixLog fulfills the slog.Logger interface. 112 var _ slog.Logger = &PrefixLog{}