diff --git a/CHANGELOG.md b/CHANGELOG.md index 457e377..1181ce6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security - Nothing +## [1.5.0] - 2019-07-30 +### Added +- `LoggerFactory` interface abstracting `Factory` struct +- `DefaultFactory` global var that allows to replace factory used to generate loggers + ## [1.4.0] - 2019-05-30 ### Changed - Baggage key is now a public string `"logctx-data-map-string-interface"` that can be set and read by anyone from any package. diff --git a/context_logger.go b/context_logger.go index 145519b..0c373b5 100644 --- a/context_logger.go +++ b/context_logger.go @@ -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, diff --git a/factory.go b/factory.go index ef0fc58..26e5ae2 100644 --- a/factory.go +++ b/factory.go @@ -4,25 +4,16 @@ 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, - } -} +// DefaultFactory is the factory used to create new loggers +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) +// LoggerFactory creates Logger instances +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) }