Skip to content

Commit

Permalink
Simplify logging a little
Browse files Browse the repository at this point in the history
We don't need multiple loggers by subsystem, because actually we always uses the
same logger. We don't have different loggers. That's why I removed

```
type SubsystemLoggers map[Subsystem]logger.Logger
```
  • Loading branch information
dsh2dsh committed Oct 15, 2024
1 parent 2066c12 commit 8bb6b4d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 69 deletions.
3 changes: 1 addition & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func Run(ctx context.Context, conf *config.Config) error {

log := logger.NewLogger(outlets, 1*time.Second)
log.Info(version.NewZreplVersionInformation().String())
ctx = logging.WithLoggers(ctx,
logging.SubsystemLoggersWithUniversalLogger(log))
ctx = logging.WithLogger(ctx, log)
registerTraceCallbacks()

log.Info("starting daemon")
Expand Down
2 changes: 1 addition & 1 deletion daemon/hooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ jobs:
cbReached = false

if testing.Verbose() && !tt.SuppressOutput {
ctx = logging.WithLoggers(ctx, logging.SubsystemLoggersWithUniversalLogger(log))
ctx = logging.WithLogger(ctx, log)
}
plan.Run(ctx, false)
report := plan.Report()
Expand Down
95 changes: 30 additions & 65 deletions daemon/logging/build_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"iter"
"log/syslog"
"os"

Expand All @@ -15,9 +16,9 @@ import (
"github.com/dsh2dsh/zrepl/tlsconf"
)

func OutletsFromConfig(in config.LoggingOutletEnumList) (*logger.Outlets, error) {
func OutletsFromConfig(in config.LoggingOutletEnumList,
) (*logger.Outlets, error) {
outlets := logger.NewOutlets()

if len(in) == 0 {
// Default config
out := WriterOutlet{NewSlogFormatter(), os.Stdout}
Expand All @@ -27,7 +28,6 @@ func OutletsFromConfig(in config.LoggingOutletEnumList) (*logger.Outlets, error)

var syslogOutlets, stdoutOutlets int
for lei, le := range in {

outlet, minLevel, err := ParseOutlet(le)
if err != nil {
return nil, fmt.Errorf("cannot parse outlet #%d: %w", lei, err)
Expand All @@ -40,9 +40,7 @@ func OutletsFromConfig(in config.LoggingOutletEnumList) (*logger.Outlets, error)
case WriterOutlet:
stdoutOutlets++
}

outlets.Add(outlet, minLevel)

}

if syslogOutlets > 1 {
Expand All @@ -51,7 +49,6 @@ func OutletsFromConfig(in config.LoggingOutletEnumList) (*logger.Outlets, error)
if stdoutOutlets > 1 {
return nil, errors.New("can only define one 'stdout' outlet")
}

return outlets, nil
}

Expand Down Expand Up @@ -95,80 +92,48 @@ var AllSubsystems = []Subsystem{
SubsysPlatformtest,
}

type injectedField struct {
field string
value interface{}
parent *injectedField
}

func WithInjectedField(ctx context.Context, field string, value interface{}) context.Context {
var parent *injectedField
parentI := ctx.Value(contextKeyInjectedField)
if parentI != nil {
parent = parentI.(*injectedField)
}
func WithInjectedField(ctx context.Context, field string, value any,
) context.Context {
parent, _ := ctx.Value(contextKeyInjectedField).(*injectedField)
// TODO sanity-check `field` now
this := &injectedField{field, value, parent}
return context.WithValue(ctx, contextKeyInjectedField, this)
return context.WithValue(ctx, contextKeyInjectedField,
&injectedField{field, value, parent})
}

func iterInjectedFields(ctx context.Context, cb func(field string, value interface{})) {
injI := ctx.Value(contextKeyInjectedField)
if injI == nil {
return
}
inj := injI.(*injectedField)
for ; inj != nil; inj = inj.parent {
cb(inj.field, inj.value)
}
type injectedField struct {
field string
value any
parent *injectedField
}

type SubsystemLoggers map[Subsystem]logger.Logger

func SubsystemLoggersWithUniversalLogger(l logger.Logger) SubsystemLoggers {
loggers := make(SubsystemLoggers)
for _, s := range AllSubsystems {
loggers[s] = l
func iterInjectedFields(ctx context.Context) iter.Seq2[string, any] {
inj, _ := ctx.Value(contextKeyInjectedField).(*injectedField)
fn := func(yield func(field string, value any) bool) {
for ; inj != nil; inj = inj.parent {
if !yield(inj.field, inj.value) {
return
}
}
}
return loggers
return fn
}

func WithLoggers(ctx context.Context, loggers SubsystemLoggers) context.Context {
return context.WithValue(ctx, contextKeyLoggers, loggers)
}

func GetLoggers(ctx context.Context) SubsystemLoggers {
loggers, ok := ctx.Value(contextKeyLoggers).(SubsystemLoggers)
if !ok {
return nil
}
return loggers
func WithLogger(ctx context.Context, l logger.Logger) context.Context {
return context.WithValue(ctx, contextKeyLoggers, l)
}

func GetLogger(ctx context.Context, subsys Subsystem) logger.Logger {
return getLoggerImpl(ctx, subsys)
}

func getLoggerImpl(ctx context.Context, subsys Subsystem) logger.Logger {
loggers, ok := ctx.Value(contextKeyLoggers).(SubsystemLoggers)
if !ok || loggers == nil {
return logger.NewNullLogger()
}
l, ok := loggers[subsys]
if !ok {
l, _ := ctx.Value(contextKeyLoggers).(logger.Logger)
if l == nil {
return logger.NewNullLogger()
}

l = l.WithField(SubsysField, subsys)

l = l.WithField(SpanField, trace.GetSpanStackOrDefault(ctx, *trace.StackKindId, "NOSPAN"))

fields := make(logger.Fields)
iterInjectedFields(ctx, func(field string, value interface{}) {
fields[field] = value
})
l = l.WithFields(fields)
l = l.WithField(SubsysField, subsys).WithField(SpanField,
trace.GetSpanStackOrDefault(ctx, *trace.StackKindId, "NOSPAN"))

for field, v := range iterInjectedFields(ctx) {
l = l.WithField(field, v)
}
return l
}

Expand Down
2 changes: 1 addition & 1 deletion platformtest/harness/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func HarnessRun(args HarnessArgs) error {
}
ctx := context.Background()
defer trace.WithTaskFromStackUpdateCtx(&ctx)()
ctx = logging.WithLoggers(ctx, logging.SubsystemLoggersWithUniversalLogger(logger))
ctx = logging.WithLogger(ctx, logger)
ex := platformtest.NewEx(logger)

testQueue := list.New()
Expand Down

0 comments on commit 8bb6b4d

Please sign in to comment.