From db53d11d6151952dad2681bdf58dcd658660f700 Mon Sep 17 00:00:00 2001 From: Bruno Carlin Date: Tue, 31 May 2022 16:04:07 +0200 Subject: [PATCH] feat: add trhee log levels --- CHANGELOG.md | 7 +++++++ backend_syslog_linux.go | 6 +++++- logger.go | 33 +++++++++++++++++++++++++++++++++ logging.go | 10 ++++++++-- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dccd862..63f4d7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +## v0.4.0 (2020-05-17) + +- Add three new log levels: `Trace`, `Notice` and `Alert` with the following + order: `Trace`, `Debug`, `Info`, `Notice`, `Warning`, `Error`, `Critical`, + `Alert`, `Fatal`. + As Syslog has no equivalent of `Trace`, it is mapped to `Debug`. + ## v0.3.0 (2020-05-17) ### Incompatible Changes diff --git a/backend_syslog_linux.go b/backend_syslog_linux.go index 21ceb3e..ad23339 100644 --- a/backend_syslog_linux.go +++ b/backend_syslog_linux.go @@ -52,16 +52,20 @@ func (sb *SyslogBackend) Write(r *Record) error { text := sb.formatter(r) switch r.Level { - case Debug: + case Trace, Debug: err = sb.w.Debug(text) case Info: err = sb.w.Info(text) + case Notice: + err = sb.w.Notice(text) case Warning: err = sb.w.Warning(text) case Error: err = sb.w.Err(text) case Critical: err = sb.w.Crit(text) + case Alert: + err = sb.w.Alert(text) case Fatal: err = sb.w.Emerg(text) } diff --git a/logger.go b/logger.go index 9acc846..2123741 100644 --- a/logger.go +++ b/logger.go @@ -88,6 +88,17 @@ func (l *Logger) Log(level Level, m string) { } } +// Trace logs a message with the Trace level. +func (l *Logger) Trace(text string) { + l.Log(Trace, text) +} + +// Tracef formats the message with given args and logs the result with the +// Trace level. +func (l *Logger) Tracef(text string, args ...interface{}) { + l.Trace(fmt.Sprintf(text, args...)) +} + // Debug logs a message with the Debug level. func (l *Logger) Debug(text string) { l.Log(Debug, text) @@ -110,6 +121,17 @@ func (l *Logger) Infof(text string, args ...interface{}) { l.Info(fmt.Sprintf(text, args...)) } +// Notice logs a message with the Notice level. +func (l *Logger) Notice(text string) { + l.Log(Notice, text) +} + +// Noticef formats the message with given args and logs the result with the +// Notice level. +func (l *Logger) Noticef(text string, args ...interface{}) { + l.Notice(fmt.Sprintf(text, args...)) +} + // Warning logs a message with the Warning level. func (l *Logger) Warning(text string) { l.Log(Warning, text) @@ -143,6 +165,17 @@ func (l *Logger) Criticalf(text string, args ...interface{}) { l.Critical(fmt.Sprintf(text, args...)) } +// Alert logs a message with the Alert level. +func (l *Logger) Alert(text string) { + l.Log(Alert, text) +} + +// Alertf formats the message with given args and logs the result with the. +// Alert level. +func (l *Logger) Alertf(text string, args ...interface{}) { + l.Alert(fmt.Sprintf(text, args...)) +} + // Fatal logs a message with the Fatal level. func (l *Logger) Fatal(text string) { l.Log(Debug, text) diff --git a/logging.go b/logging.go index 422cf4d..f89bf0f 100644 --- a/logging.go +++ b/logging.go @@ -38,11 +38,14 @@ type Level byte //revive:disable:exported const ( - Debug Level = iota + Trace Level = iota + Debug Info + Notice Warning Error Critical + Alert Fatal DefaultLevel = Info ) @@ -50,7 +53,10 @@ const ( //revive:enable:exported //nolint:gochecknoglobals // designed this way -var levelNames = [6]string{"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "FATAL"} +var levelNames = [...]string{ + "TRACE", "DEBUG", "INFO", "NOTICE", "WARNING", + "ERROR", "CRITICAL", "ALERT", "FATAL", +} // Name returns the name of the log level. func (l Level) Name() string {