Skip to content

Commit aca7d35

Browse files
authored
Merge pull request #6 from cabify/add-default-factory
Abstract Factory as interface
2 parents fbdfaec + 2e9d2a5 commit aca7d35

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Security
2424
- Nothing
2525

26+
## [1.5.0] - 2019-07-30
27+
### Added
28+
- `LoggerFactory` interface abstracting `Factory` struct
29+
- `DefaultFactory` global var that allows to replace factory used to generate loggers
30+
2631
## [1.4.0] - 2019-05-30
2732
### Changed
2833
- Baggage key is now a public string `"logctx-data-map-string-interface"` that can be set and read by anyone from any package.

context_logger.go

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ func baggageString(b map[string]interface{}) string {
2323
return strings.Join(kvPairs, ": ")
2424
}
2525

26+
// Factory provides context aware loggers.
27+
type Factory struct {
28+
baseLogger Logger
29+
}
30+
31+
// NewFactory instantiates a factory with the default logger.
32+
func NewFactory() Factory {
33+
return Factory{
34+
baseLogger: DefaultLogger,
35+
}
36+
}
37+
38+
// For provides a logger which is aware of the passed context and will prepend the context baggage values.
39+
func (f Factory) For(ctx context.Context) Logger {
40+
return newBaggageLogger(ctx, f.baseLogger)
41+
}
42+
2643
func newBaggageLogger(ctx context.Context, base Logger) baggageLogger {
2744
return baggageLogger{
2845
Logger: base,

factory.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@ import (
44
"context"
55
)
66

7-
// Factory provides context aware loggers.
8-
type Factory struct {
9-
baseLogger Logger
10-
}
11-
12-
// NewFactory instantiates a factory with the default logger.
13-
func NewFactory() Factory {
14-
return Factory{
15-
baseLogger: DefaultLogger,
16-
}
17-
}
7+
// DefaultFactory is the factory used to create new loggers
8+
var DefaultFactory Factory = NewFactory()
189

19-
// For provides a logger which is aware of the passed context and will prepend the context baggage values.
20-
func (f Factory) For(ctx context.Context) Logger {
21-
return newBaggageLogger(ctx, f.baseLogger)
10+
// LoggerFactory creates Logger instances
11+
type LoggerFactory interface {
12+
For(ctx context.Context) Logger
2213
}
2314

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

0 commit comments

Comments
 (0)