feat: add trhee log levels

This commit is contained in:
Bruno Carlin 2022-05-31 16:04:07 +02:00
parent bbfc179269
commit db53d11d61
4 changed files with 53 additions and 3 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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)

View file

@ -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 {