diff options
author | Robin Jarry <robin@jarry.cc> | 2023-06-04 21:34:11 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-06-12 11:22:55 +0200 |
commit | 531440f33610e8bdccaf31b916da990d144fa0ae (patch) | |
tree | 95b6d1ad4864c675c25ce4024915df70187faf6d /log/logger.go | |
parent | 9479513ec138b0e4b2ad2c53a0566c00c7035845 (diff) | |
download | aerc-531440f33610e8bdccaf31b916da990d144fa0ae.tar.gz |
logger: add support for named loggers
Allow creating loggers with name prefixes. This will be used in next
commits to have per worker/account loggers.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'log/logger.go')
-rw-r--r-- | log/logger.go | 89 |
1 files changed, 64 insertions, 25 deletions
diff --git a/log/logger.go b/log/logger.go index eb52d7b7..2ece5f32 100644 --- a/log/logger.go +++ b/log/logger.go @@ -62,52 +62,91 @@ func ErrorLogger() *log.Logger { return err } -func Tracef(message string, args ...interface{}) { - if trace == nil || minLevel > TRACE { - return - } +type Logger interface { + Tracef(string, ...interface{}) + Debugf(string, ...interface{}) + Infof(string, ...interface{}) + Warnf(string, ...interface{}) + Errorf(string, ...interface{}) +} + +type logger struct { + name string + calldepth int +} + +func NewLogger(name string, calldepth int) Logger { + return &logger{name: name, calldepth: calldepth} +} + +func (l *logger) format(message string, args ...interface{}) string { if len(args) > 0 { message = fmt.Sprintf(message, args...) } - trace.Output(2, message) //nolint:errcheck // we can't do anything with what we log + if l.name != "" { + message = fmt.Sprintf("[%s] %s", l.name, message) + } + return message } -func Debugf(message string, args ...interface{}) { - if dbg == nil || minLevel > DEBUG { +func (l *logger) Tracef(message string, args ...interface{}) { + if trace == nil || minLevel > TRACE { return } - if len(args) > 0 { - message = fmt.Sprintf(message, args...) + message = l.format(message, args...) + trace.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +func (l *logger) Debugf(message string, args ...interface{}) { + if dbg == nil || minLevel > DEBUG { + return } - dbg.Output(2, message) //nolint:errcheck // we can't do anything with what we log + message = l.format(message, args...) + dbg.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log } -func Infof(message string, args ...interface{}) { +func (l *logger) Infof(message string, args ...interface{}) { if info == nil || minLevel > INFO { return } - if len(args) > 0 { - message = fmt.Sprintf(message, args...) - } - info.Output(2, message) //nolint:errcheck // we can't do anything with what we log + message = l.format(message, args...) + info.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log } -func Warnf(message string, args ...interface{}) { +func (l *logger) Warnf(message string, args ...interface{}) { if warn == nil || minLevel > WARN { return } - if len(args) > 0 { - message = fmt.Sprintf(message, args...) - } - warn.Output(2, message) //nolint:errcheck // we can't do anything with what we log + message = l.format(message, args...) + warn.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log } -func Errorf(message string, args ...interface{}) { +func (l *logger) Errorf(message string, args ...interface{}) { if err == nil || minLevel > ERROR { return } - if len(args) > 0 { - message = fmt.Sprintf(message, args...) - } - err.Output(2, message) //nolint:errcheck // we can't do anything with what we log + message = l.format(message, args...) + err.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log +} + +var root = logger{calldepth: 3} + +func Tracef(message string, args ...interface{}) { + root.Tracef(message, args...) +} + +func Debugf(message string, args ...interface{}) { + root.Debugf(message, args...) +} + +func Infof(message string, args ...interface{}) { + root.Infof(message, args...) +} + +func Warnf(message string, args ...interface{}) { + root.Warnf(message, args...) +} + +func Errorf(message string, args ...interface{}) { + root.Errorf(message, args...) } |