Skip to content

Commit

Permalink
Abstract Factory as interface
Browse files Browse the repository at this point in the history
Use legacy factory as DefaultFactory
  • Loading branch information
Joël Marty committed Jul 30, 2019
1 parent fbdfaec commit 74d3544
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
17 changes: 17 additions & 0 deletions context_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ func baggageString(b map[string]interface{}) string {
return strings.Join(kvPairs, ": ")
}

// Factory provides context aware loggers.
type Factory struct {
baseLogger Logger
}

// NewFactory instantiates a factory with the default logger.
func NewFactory() Factory {
return Factory{
baseLogger: DefaultLogger,
}
}

// For provides a logger which is aware of the passed context and will prepend the context baggage values.
func (f Factory) For(ctx context.Context) Logger {
return newBaggageLogger(ctx, f.baseLogger)
}

func newBaggageLogger(ctx context.Context, base Logger) baggageLogger {
return baggageLogger{
Logger: base,
Expand Down
19 changes: 4 additions & 15 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,14 @@ import (
"context"
)

// Factory provides context aware loggers.
type Factory struct {
baseLogger Logger
}

// NewFactory instantiates a factory with the default logger.
func NewFactory() Factory {
return Factory{
baseLogger: DefaultLogger,
}
}
var DefaultFactory Factory = NewFactory()

// For provides a logger which is aware of the passed context and will prepend the context baggage values.
func (f Factory) For(ctx context.Context) Logger {
return newBaggageLogger(ctx, f.baseLogger)
type LoggerFactory interface {
For(ctx context.Context) Logger
}

// For provides a logger which is aware of the passed context and will prepend
// the context baggage values, using DefaultLogger as base logger.
func For(ctx context.Context) Logger {
return newBaggageLogger(ctx, DefaultLogger)
return DefaultFactory.For(ctx)
}

0 comments on commit 74d3544

Please sign in to comment.