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"
)
var _ Logger = (*CLILogger)(nil)
type CLILogger struct {
Out io.Writer
Level LogLevel
@ -66,6 +68,11 @@ func (pl *CLILogger) NoExit() {
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
func (pl *CLILogger) Debug(msg string) LogBuilder {
return newCLILogBuilder(pl, msg, LogLevelDebug)

View File

@ -40,6 +40,11 @@ func (jl *JSONLogger) NoExit() {
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
func (jl *JSONLogger) Debug(msg string) LogBuilder {
return newJSONLogBuilder(jl, msg, LogLevelDebug)

View File

@ -1,8 +1,9 @@
package log
import (
"go.arsenm.dev/logger"
"os"
"go.arsenm.dev/logger"
)
var Logger logger.Logger = logger.NewJSON(os.Stderr)
@ -17,6 +18,11 @@ func 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
func Debug(msg string) logger.LogBuilder {
return Logger.Debug(msg)

View File

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

View File

@ -34,6 +34,13 @@ func (ml *MultiLogger) NoPanic() {
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
func (ml *MultiLogger) Debug(msg string) LogBuilder {
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
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
func (nl NopLogger) Debug(msg string) LogBuilder {
return NopLogBuilder{}

View File

@ -81,6 +81,11 @@ func (pl *PrettyLogger) NoExit() {
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
func (pl *PrettyLogger) Debug(msg string) LogBuilder {
return newPrettyLogBuilder(pl, msg, LogLevelDebug)