2018-09-28 06:28:33 +02:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-29 03:04:16 +02:00
|
|
|
"io"
|
2019-08-03 05:28:25 +02:00
|
|
|
|
|
|
|
"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
|
2019-08-03 05:28:25 +02:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2019-08-03 05:28:25 +02:00
|
|
|
func WithContext(ctx context.Context, opts Options) context.Context {
|
|
|
|
c := zerolog.New(opts.Writer).With().Timestamp()
|
2018-09-29 03:04:16 +02:00
|
|
|
|
2019-08-03 05:28:25 +02:00
|
|
|
for k, v := range opts.Fields {
|
|
|
|
c = c.Interface(k, v)
|
2018-09-29 03:04:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 05:28:25 +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)
|
|
|
|
}
|