Add SetLevel function

This commit is contained in:
Elara 2023-01-04 14:53:04 -08:00
parent d5add47d25
commit ed50ff00ef
7 changed files with 38 additions and 2 deletions

7
cli.go
View File

@ -14,6 +14,8 @@ import (
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
) )
var _ Logger = (*CLILogger)(nil)
type CLILogger struct { type CLILogger struct {
Out io.Writer Out io.Writer
Level LogLevel Level LogLevel
@ -66,6 +68,11 @@ func (pl *CLILogger) NoExit() {
pl.noExit = true pl.noExit = true
} }
// SetLevel sets the log level of the logger
func (pl *CLILogger) SetLevel(l LogLevel) {
pl.Level = l
}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func (pl *CLILogger) Debug(msg string) LogBuilder { func (pl *CLILogger) Debug(msg string) LogBuilder {
return newCLILogBuilder(pl, msg, LogLevelDebug) return newCLILogBuilder(pl, msg, LogLevelDebug)

View File

@ -40,6 +40,11 @@ func (jl *JSONLogger) NoExit() {
jl.noExit = true jl.noExit = true
} }
// SetLevel sets the log level of the logger
func (jl *JSONLogger) SetLevel(l LogLevel) {
jl.Level = l
}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func (jl *JSONLogger) Debug(msg string) LogBuilder { func (jl *JSONLogger) Debug(msg string) LogBuilder {
return newJSONLogBuilder(jl, msg, LogLevelDebug) return newJSONLogBuilder(jl, msg, LogLevelDebug)

View File

@ -1,8 +1,9 @@
package log package log
import ( import (
"go.arsenm.dev/logger"
"os" "os"
"go.arsenm.dev/logger"
) )
var Logger logger.Logger = logger.NewJSON(os.Stderr) var Logger logger.Logger = logger.NewJSON(os.Stderr)
@ -17,6 +18,11 @@ func NoExit() {
Logger.NoExit() Logger.NoExit()
} }
// SetLevel sets the log level of the logger
func SetLevel(l logger.LogLevel) {
Logger.SetLevel(l)
}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func Debug(msg string) logger.LogBuilder { func Debug(msg string) logger.LogBuilder {
return Logger.Debug(msg) return Logger.Debug(msg)

View File

@ -1,8 +1,8 @@
package logger package logger
import ( import (
"fmt"
"errors" "errors"
"fmt"
"strings" "strings"
) )
@ -51,6 +51,9 @@ type Logger interface {
// NoExit prevents the logger from exiting on fatal events // NoExit prevents the logger from exiting on fatal events
NoExit() NoExit()
// SetLevel sets the log level of the logger
SetLevel(LogLevel)
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
Debug(string) LogBuilder Debug(string) LogBuilder

View File

@ -34,6 +34,13 @@ func (ml *MultiLogger) NoPanic() {
ml.noPanic = true ml.noPanic = true
} }
// SetLevel sets the log level of the logger
func (ml *MultiLogger) SetLevel(l LogLevel) {
for _, logger := range ml.Loggers {
logger.SetLevel(l)
}
}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func (ml *MultiLogger) Debug(msg string) LogBuilder { func (ml *MultiLogger) Debug(msg string) LogBuilder {
lbs := make([]LogBuilder, len(ml.Loggers)) lbs := make([]LogBuilder, len(ml.Loggers))

3
nop.go
View File

@ -21,6 +21,9 @@ func (nl NopLogger) NoPanic() {}
// NoExit prevents the logger from exiting on fatal events // NoExit prevents the logger from exiting on fatal events
func (nl NopLogger) NoExit() {} func (nl NopLogger) NoExit() {}
// SetLevel sets the log level of the logger
func (nl NopLogger) SetLevel(LogLevel) {}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func (nl NopLogger) Debug(msg string) LogBuilder { func (nl NopLogger) Debug(msg string) LogBuilder {
return NopLogBuilder{} return NopLogBuilder{}

View File

@ -81,6 +81,11 @@ func (pl *PrettyLogger) NoExit() {
pl.noExit = true pl.noExit = true
} }
// SetLevel sets the log level of the logger
func (pl *PrettyLogger) SetLevel(l LogLevel) {
pl.Level = l
}
// Debug creates a new debug event with the given message // Debug creates a new debug event with the given message
func (pl *PrettyLogger) Debug(msg string) LogBuilder { func (pl *PrettyLogger) Debug(msg string) LogBuilder {
return newPrettyLogBuilder(pl, msg, LogLevelDebug) return newPrettyLogBuilder(pl, msg, LogLevelDebug)