feat: loglevel for each output
This commit is contained in:
parent
8dde70c555
commit
e20fe3c887
2 changed files with 11 additions and 11 deletions
11
logger.go
11
logger.go
|
@ -19,27 +19,20 @@ import "time"
|
|||
var Default = NewLoggerDefault()
|
||||
|
||||
type Logger struct {
|
||||
Level Level
|
||||
Outputs []*Output
|
||||
}
|
||||
|
||||
func NewLogger(level Level, outputs []*Output) *Logger {
|
||||
func NewLogger(outputs []*Output) *Logger {
|
||||
return &Logger{
|
||||
Level: LevelInfo,
|
||||
Outputs: outputs,
|
||||
}
|
||||
}
|
||||
|
||||
func NewLoggerDefault() *Logger {
|
||||
return NewLogger(LevelInfo, []*Output{NewOutputDefault()})
|
||||
return NewLogger([]*Output{NewOutputDefault()})
|
||||
}
|
||||
|
||||
func (logger *Logger) LogEntry(entry *Entry) {
|
||||
// check, if message should be printed
|
||||
if entry.Level > logger.Level {
|
||||
return
|
||||
}
|
||||
|
||||
// set execution time
|
||||
entry.Time = time.Now()
|
||||
|
||||
|
|
11
output.go
11
output.go
|
@ -17,23 +17,30 @@ package golog
|
|||
import "sync"
|
||||
|
||||
type Output struct {
|
||||
Level Level
|
||||
Formatter Formatter
|
||||
formatterLock sync.Mutex
|
||||
Printer Printer
|
||||
}
|
||||
|
||||
func NewOutput(formatter Formatter, printer Printer) *Output {
|
||||
func NewOutput(level Level, formatter Formatter, printer Printer) *Output {
|
||||
return &Output{
|
||||
Level: level,
|
||||
Formatter: formatter,
|
||||
Printer: printer,
|
||||
}
|
||||
}
|
||||
|
||||
func NewOutputDefault() *Output {
|
||||
return NewOutput(NewFormatterKeyValue(), NewPrinterStdout())
|
||||
return NewOutput(LevelInfo, NewFormatterKeyValue(), NewPrinterStdout())
|
||||
}
|
||||
|
||||
func (output *Output) Send(entry *Entry) {
|
||||
// check, if message should be printed
|
||||
if entry.Level > output.Level {
|
||||
return
|
||||
}
|
||||
|
||||
output.format(entry)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue