1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/logging/logger.go

47 lines
660 B
Go
Raw Normal View History

2018-09-28 06:28:33 +02:00
package logging
import (
"context"
2018-09-29 03:04:16 +02:00
"io"
"github.com/rs/zerolog"
2018-09-28 06:28:33 +02:00
)
2018-09-29 03:04:16 +02:00
type (
Level uint8
Options struct {
Writer io.Writer
Level Level
Fields map[string]interface{}
2018-09-29 03:04:16 +02:00
}
)
2018-09-28 06:28:33 +02:00
const (
DebugLevel Level = iota
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
NoLevel
Disabled
)
func WithContext(ctx context.Context, opts Options) context.Context {
c := zerolog.New(opts.Writer).With().Timestamp()
2018-09-29 03:04:16 +02:00
for k, v := range opts.Fields {
c = c.Interface(k, v)
2018-09-29 03:04:16 +02:00
}
logger := c.Logger()
logger.Level(zerolog.Level(opts.Level))
2018-09-29 03:04:16 +02:00
return logger.WithContext(ctx)
}
func FromContext(ctx context.Context) *zerolog.Logger {
2018-09-28 06:28:33 +02:00
return zerolog.Ctx(ctx)
}