feat: loglevel for each output

This commit is contained in:
Martin Riedl 2024-01-03 23:45:40 +01:00
parent 8dde70c555
commit e20fe3c887
Signed by: martinr92
GPG key ID: FB68DA65516A804C
2 changed files with 11 additions and 11 deletions

View file

@ -19,27 +19,20 @@ import "time"
var Default = NewLoggerDefault() var Default = NewLoggerDefault()
type Logger struct { type Logger struct {
Level Level
Outputs []*Output Outputs []*Output
} }
func NewLogger(level Level, outputs []*Output) *Logger { func NewLogger(outputs []*Output) *Logger {
return &Logger{ return &Logger{
Level: LevelInfo,
Outputs: outputs, Outputs: outputs,
} }
} }
func NewLoggerDefault() *Logger { func NewLoggerDefault() *Logger {
return NewLogger(LevelInfo, []*Output{NewOutputDefault()}) return NewLogger([]*Output{NewOutputDefault()})
} }
func (logger *Logger) LogEntry(entry *Entry) { func (logger *Logger) LogEntry(entry *Entry) {
// check, if message should be printed
if entry.Level > logger.Level {
return
}
// set execution time // set execution time
entry.Time = time.Now() entry.Time = time.Now()

View file

@ -17,23 +17,30 @@ package golog
import "sync" import "sync"
type Output struct { type Output struct {
Level Level
Formatter Formatter Formatter Formatter
formatterLock sync.Mutex formatterLock sync.Mutex
Printer Printer Printer Printer
} }
func NewOutput(formatter Formatter, printer Printer) *Output { func NewOutput(level Level, formatter Formatter, printer Printer) *Output {
return &Output{ return &Output{
Level: level,
Formatter: formatter, Formatter: formatter,
Printer: printer, Printer: printer,
} }
} }
func NewOutputDefault() *Output { func NewOutputDefault() *Output {
return NewOutput(NewFormatterKeyValue(), NewPrinterStdout()) return NewOutput(LevelInfo, NewFormatterKeyValue(), NewPrinterStdout())
} }
func (output *Output) Send(entry *Entry) { func (output *Output) Send(entry *Entry) {
// check, if message should be printed
if entry.Level > output.Level {
return
}
output.format(entry) output.format(entry)
} }