mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-06 08:39:09 +02:00
Bugfix/logging (#346)
* Fixed inability to pass custom ID to th runtime logger * Removed UUID module * Removed redundant check
This commit is contained in:
@@ -2,9 +2,9 @@ package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
"io"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -13,6 +13,7 @@ type (
|
||||
Options struct {
|
||||
Writer io.Writer
|
||||
Level Level
|
||||
Fields map[string]interface{}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -27,19 +28,15 @@ const (
|
||||
Disabled
|
||||
)
|
||||
|
||||
func WithContext(ctx context.Context, opts *Options) context.Context {
|
||||
id, err := uuid.NewV4()
|
||||
func WithContext(ctx context.Context, opts Options) context.Context {
|
||||
c := zerolog.New(opts.Writer).With().Timestamp()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
for k, v := range opts.Fields {
|
||||
c = c.Interface(k, v)
|
||||
}
|
||||
|
||||
logger := zerolog.New(opts.Writer).
|
||||
With().
|
||||
Str("id", id.String()).
|
||||
Logger()
|
||||
|
||||
logger.WithLevel(zerolog.Level(opts.Level))
|
||||
logger := c.Logger()
|
||||
logger.Level(zerolog.Level(opts.Level))
|
||||
|
||||
return logger.WithContext(ctx)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
type (
|
||||
Options struct {
|
||||
params map[string]core.Value
|
||||
logging *logging.Options
|
||||
logging logging.Options
|
||||
}
|
||||
|
||||
Option func(*Options)
|
||||
@@ -22,7 +22,7 @@ type (
|
||||
func NewOptions(setters []Option) *Options {
|
||||
opts := &Options{
|
||||
params: make(map[string]core.Value),
|
||||
logging: &logging.Options{
|
||||
logging: logging.Options{
|
||||
Writer: os.Stdout,
|
||||
Level: logging.ErrorLevel,
|
||||
},
|
||||
@@ -61,6 +61,12 @@ func WithLogLevel(lvl logging.Level) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogFields(fields map[string]interface{}) Option {
|
||||
return func(options *Options) {
|
||||
options.logging.Fields = fields
|
||||
}
|
||||
}
|
||||
|
||||
func (opts *Options) WithContext(parent context.Context) context.Context {
|
||||
ctx := core.ParamsWith(parent, opts.params)
|
||||
ctx = logging.WithContext(ctx, opts.logging)
|
||||
|
||||
Reference in New Issue
Block a user