Skip to content

Commit

Permalink
added redesigned logger API
Browse files Browse the repository at this point in the history
WIP: untested redesigned API

added first draft of priority fields

WIP: changed logger api to using frozen set

WIP: working changes with added tests

fixed tests, standard logger copy, and changed the example files

added requested changes

added newline

new API

WIP: logger added to fields and removed before logging

WIP: fixed comments

WIP: no longer declare with NewMap()

WIP: fixed unit tests

WIP: fixed a couple small bugs and added benchmark test

WIP: fixed a test

WIP: added nulLLogger

WIP: changed frozen version and used Update

WIP: fixed Chain

WIP: added requested changes

WIP: removed tt and added unexported with

WIP: added documentation

WIP: removed prints in the markdown
  • Loading branch information
nofun97 committed Jan 25, 2020
1 parent cfb66c1 commit 9e47950
Show file tree
Hide file tree
Showing 19 changed files with 872 additions and 897 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.sum linguist-generated=true
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module github.com/anz-bank/pkg
go 1.13

require (
github.com/marcelocantos/frozen v0.4.0
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
github.com/alecthomas/colour v0.1.0 // indirect
github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1 // indirect
github.com/arr-ai/frozen v0.11.1
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
)
20 changes: 14 additions & 6 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 94 additions & 16 deletions log/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,107 @@ package log
import (
"context"

"github.com/anz-bank/pkg/log/loggers"
"github.com/arr-ai/frozen"
)

type loggerContextKey int
// Fields is a struct that contains all the fields data to log.
type Fields struct{ m frozen.Map }

const loggerKey loggerContextKey = iota
// From returns a copied logger from the context that you can use to access logger API.
func From(ctx context.Context) Logger {
f := getFields(ctx)
return f.getCopiedLogger().(internalLoggerOps).PutFields(f.resolveFields(ctx))
}

// Suppress will ensure that suppressed keys are not logged.
func Suppress(keys ...string) Fields {
return Fields{}.Suppress(keys...)
}

// With creates a field with a single key value pair.
func With(key string, val interface{}) Fields {
return Fields{}.With(key, val)
}

// WithCtxRef creates a field with a key that refers to the provided context key,
// fields will use key as the fields property and take the value that corresponds
// to ctxKey.
func WithCtxRef(key string, ctxKey interface{}) Fields {
return Fields{}.WithCtxRef(key, ctxKey)
}

// With adds a copy of the logger to the context
func With(ctx context.Context, logger loggers.Logger) context.Context {
return context.WithValue(ctx, loggerKey, logger.Copy())
// WithFunc creates a field with a string key and a callback value.
func WithFunc(key string, f func(context.Context) interface{}) Fields {
return Fields{}.WithFunc(key, f)
}

// WithFields adds multiple fields in the scope of the context, fields will be logged alphabetically
func WithFields(ctx context.Context, fields MultipleFields) context.Context {
return context.WithValue(ctx, loggerKey,
getCopiedLogger(ctx).PutFields(fromFields([]Fields{fields})))
// WithLogger adds logger which will be used for the log operation.
func WithLogger(logger Logger) Fields {
return Fields{}.WithLogger(logger)
}

// From is a way to access the API of the logger inside the context and add log-specific fields
func From(ctx context.Context, fields ...Fields) loggers.Logger {
logger := getCopiedLogger(ctx)
if len(fields) == 0 {
return logger
// Chain merges all the fields and returns the merged fields, the precedence
// of fields in case of overlapping gets higher from left to right.
func (f Fields) Chain(fieldses ...Fields) Fields {
merged := f.m
for _, fields := range fieldses {
merged = merged.Update(fields.m)
}
return logger.PutFields(fromFields(fields))
return Fields{merged}
}

// From returns a logger with the new fields which is the fields from the context
// merged with the current fields were current fields replaces value from
// the context fields.
func (f Fields) From(ctx context.Context) Logger {
return From(f.Onto(ctx))
}

// Onto finishes fields operation, merge them all with the precedence of fields
// in case overlapping gets higher from left to right, and puts the merged fields
// in the context.
func (f Fields) Onto(ctx context.Context) context.Context {
return context.WithValue(ctx, fieldsContextKey{}, getFields(ctx).Chain(f).m)
}

// Suppress ensures that the keys will not be logger.
func (f Fields) Suppress(keys ...string) Fields {
return f.Chain(Fields{
frozen.NewMapFromKeys(
frozen.NewSetFromStrings(keys...),
func(_ interface{}) interface{} {
return suppress{}
},
),
})
}

// With adds to the fields a single key value pair.
func (f Fields) With(key string, val interface{}) Fields {
return f.with(key, val)
}

// WithCtxRef adds key and the context key to the fields.
func (f Fields) WithCtxRef(key string, ctxKey interface{}) Fields {
return f.with(key, ctxRef{ctxKey})
}

// WithFunc adds key and the function to the fields.
func (f Fields) WithFunc(key string, val func(context.Context) interface{}) Fields {
return f.with(key, val)
}

// WithLogger adds logger which will be used for the log operation.
func (f Fields) WithLogger(logger Logger) Fields {
return f.with(loggerKey{}, logger.(internalLoggerOps).Copy())
}

// String returns a string that represent the current fields
func (f Fields) String(ctx context.Context) string {
return f.resolveFields(ctx).String()
}

// MergedString returns a string that represents the current fields merged by fields in context
func (f Fields) MergedString(ctx context.Context) string {
return getFields(ctx).Chain(f).String(ctx)
}
Loading

0 comments on commit 9e47950

Please sign in to comment.