1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-06 08:39:09 +02:00

#27 Added logging

This commit is contained in:
Tim Voronov
2018-09-28 00:28:33 -04:00
parent 4d4c6ceadd
commit e427efd74e
18 changed files with 408 additions and 112 deletions

View File

@@ -1,12 +1,21 @@
package runtime
import "context"
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/gofrs/uuid"
"github.com/rs/zerolog"
"io"
"os"
)
type (
Options struct {
proxy string
cdp string
variables map[string]interface{}
logWriter io.Writer
logLevel zerolog.Level
}
Option func(*Options)
@@ -14,8 +23,10 @@ type (
func newOptions() *Options {
return &Options{
cdp: "http://127.0.0.1:9222",
cdp: "http://0.0.0.0:9222",
variables: make(map[string]interface{}),
logWriter: os.Stdout,
logLevel: zerolog.ErrorLevel,
}
}
@@ -38,10 +49,38 @@ func WithProxy(address string) Option {
}
}
func WithLog(writer io.Writer) Option {
return func(options *Options) {
options.logWriter = writer
}
}
func WithLogLevel(lvl logging.Level) Option {
return func(options *Options) {
options.logLevel = zerolog.Level(lvl)
}
}
func (opts *Options) withContext(parent context.Context) context.Context {
return context.WithValue(
ctx := context.WithValue(
parent,
"variables",
opts.variables,
)
id, err := uuid.NewV4()
if err != nil {
panic(err)
}
logger := zerolog.New(opts.logWriter).
With().
Str("id", id.String()).
Logger()
logger.WithLevel(opts.logLevel)
ctx = logger.WithContext(ctx)
return ctx
}