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()
|
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()
|
||||||
|
|
||||||
|
|
11
output.go
11
output.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue