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
2018-09-28 21:04:16 -04:00

50 lines
668 B
Go

package logging
import (
"context"
"github.com/gofrs/uuid"
"github.com/rs/zerolog"
"io"
)
type (
Level uint8
Options struct {
Writer io.Writer
Level Level
}
)
const (
DebugLevel Level = iota
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
NoLevel
Disabled
)
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 {
return zerolog.Ctx(ctx)
}