Skip to content

Commit

Permalink
Introduce Fork vs New for logger instances
Browse files Browse the repository at this point in the history
  • Loading branch information
grafov committed Nov 20, 2018
1 parent 55fc848 commit 5c77e8f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
26 changes: 21 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ type (
}
)

// New creates a new logger instance.
func New() *Logger {
// Fork creates a new logger instance that inherited the context from
// the global logger.
func Fork() *Logger {
var newContext = make(map[string]Pair, len(globalContext.m)*2)
globalContext.RLock()
for key, pair := range globalContext.m {
Expand All @@ -75,16 +76,31 @@ func New() *Logger {
return &Logger{context: newContext}
}

// New creates copy of logger instance. It copies the context of the old logger
// but skips values of the current record of the old logger.
func (l *Logger) New() *Logger {
// New creates a new logger instance but not copy context from the
// global logger.
func New() *Logger {
var newContext = make(map[string]Pair)
return &Logger{context: newContext}
}

// Fork creates a new instance of the logger. It copies the context
// from the logger from the parent logger. But the values of the
// current record of the parent logger discarded.
func (l *Logger) Fork() *Logger {
var newContext = make(map[string]Pair, len(l.context)*2)
for key, pair := range l.context {
newContext[key] = pair
}
return &Logger{context: newContext}
}

// New creates a new instance of the logger. It not inherited the
// context of the parent logger.
func (l *Logger) New() *Logger {
var newContext = make(map[string]Pair)
return &Logger{context: newContext}
}

// Log is the most common method for flushing previously added key-val pairs to an output.
// After current record is flushed all pairs removed from a record except contextSrc pairs.
func (l *Logger) Log(keyVals ...interface{}) {
Expand Down
28 changes: 22 additions & 6 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,44 @@ func TestNewLogger(t *testing.T) {
}
}

// Test of creating a new logger from existing logger.
// Test of creating a fork the logger from the existing logger.
func TestLogger_ForkWithContext(t *testing.T) {
log := Fork().With("key", "value", "key2", 123)

sublog := log.Fork()
context := sublog.getContext()

if context["key"].Val != "value" {
t.Fail()

}
if context["key2"].Val != "123" {
t.Fail()
}
}

// Test of creating a fork the logger from the existing logger.
func TestLogger_NewWithContext(t *testing.T) {
log := New().With("key", "value", "key2", 123)

sublog := log.New()
context := sublog.getContext()

if context["key"].Val != "value" {
if context["key"].Val == "value" {
t.Fail()

}
if context["key2"].Val != "123" {
if context["key2"].Val == "123" {
t.Fail()
}
}

// Test of creating a new logger from existing logger. Deleted values should not present in sublogger.
func TestLogger_NewWithoutContext(t *testing.T) {
log := New().With("key", "value", "key2", 123)
func TestLogger_ForkWithoutContext(t *testing.T) {
log := Fork().With("key", "value", "key2", 123)

log.Without("key")
sublog := log.New()
sublog := log.Fork()
context := sublog.getContext()

if context["key"].Val == "value" {
Expand Down

0 comments on commit 5c77e8f

Please sign in to comment.