2018-09-28 06:28:33 +02:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-29 03:04:16 +02:00
|
|
|
"github.com/gofrs/uuid"
|
2018-09-28 06:28:33 +02:00
|
|
|
"github.com/rs/zerolog"
|
2018-09-29 03:04:16 +02:00
|
|
|
"io"
|
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
|
|
|
|
}
|
|
|
|
)
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
DebugLevel Level = iota
|
|
|
|
InfoLevel
|
|
|
|
WarnLevel
|
|
|
|
ErrorLevel
|
|
|
|
FatalLevel
|
|
|
|
PanicLevel
|
|
|
|
NoLevel
|
|
|
|
Disabled
|
|
|
|
)
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
func WithContext(ctx context.Context, opts *Options) context.Context {
|
|
|
|
id, err := uuid.NewV4()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := zerolog.New(opts.Writer).
|
|
|
|
With().
|
|
|
|
Str("id", id.String()).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
logger.WithLevel(zerolog.Level(opts.Level))
|
|
|
|
|
|
|
|
return logger.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromContext(ctx context.Context) *zerolog.Logger {
|
2018-09-28 06:28:33 +02:00
|
|
|
return zerolog.Ctx(ctx)
|
|
|
|
}
|