aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-06-04 21:34:11 +0200
committerRobin Jarry <robin@jarry.cc>2023-06-04 22:03:34 +0200
commitbbdd1a6caed03b3db336740ebb5ab4a8e0763768 (patch)
treef851f4455e7d8c61c846b57935c839f9fe02e566
parentedd4752268b266d697be51733599b708e4ae449a (diff)
downloadaerc-bbdd1a6caed03b3db336740ebb5ab4a8e0763768.tar.gz
aerc-bbdd1a6caed03b3db336740ebb5ab4a8e0763768.zip
logger: add support for named loggers
Signed-off-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--log/logger.go89
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...)
}