Skip to content

Commit

Permalink
Breaking change - top level API - log levels (#96)
Browse files Browse the repository at this point in the history
The expectation is that the new "Log" level will be the default level
people use.

Adding "Log()" as a level, created a conflict with the "Log()" that is
used to create loggers, so the top-level logger changed from "Log" to
"Logger".

That creates a name conflict that is not resolved with xopbase.Logger,
the lower level logger ("exporter" in OTEL vocab).
  • Loading branch information
muir authored Dec 30, 2023
1 parent 7570833 commit a5ad1de
Show file tree
Hide file tree
Showing 31 changed files with 537 additions and 512 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ func WithConfigChanges(mods ...ConfigModifier) SeedModifier {
}
}

func (log *Log) Config() Config {
return log.span.seed.config
func (logger *Logger) Config() Config {
return logger.span.seed.config
}
40 changes: 20 additions & 20 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ var contextKey = contextKeyType{}
// does not find a logger. Unless modified, it discards all logs.
var Default = NewSeed().Request("discard")

func (log *Log) IntoContext(ctx context.Context) context.Context {
return context.WithValue(ctx, contextKey, log)
func (logger *Logger) IntoContext(ctx context.Context) context.Context {
return context.WithValue(ctx, contextKey, logger)
}

func FromContext(ctx context.Context) (*Log, bool) {
func FromContext(ctx context.Context) (*Logger, bool) {
v := ctx.Value(contextKey)
if v == nil {
return nil, false
}
return v.(*Log), true
return v.(*Logger), true
}

func FromContextOrDefault(ctx context.Context) *Log {
log, ok := FromContext(ctx)
func FromContextOrDefault(ctx context.Context) *Logger {
logger, ok := FromContext(ctx)
if ok {
return log
return logger
}
return Default
}

func FromContextOrPanic(ctx context.Context) *Log {
log, ok := FromContext(ctx)
func FromContextOrPanic(ctx context.Context) *Logger {
logger, ok := FromContext(ctx)
if !ok {
panic("Could not find logger in context")
}
return log
return logger
}

// CustomFromContext returns a convenience function: it calls either
Expand All @@ -53,10 +53,10 @@ func FromContextOrPanic(ctx context.Context) *Log {
//
// Pass FromContextOrPanic or FromContextOrDefault as the first argument
// and a function to adjust settings as the second argument.
func CustomFromContext(getLogFromContext func(context.Context) *Log, adjustSettings func(*Sub) *Sub) func(context.Context) *Log {
return func(ctx context.Context) *Log {
log := getLogFromContext(ctx)
return adjustSettings(log.Sub()).Log()
func CustomFromContext(getLogFromContext func(context.Context) *Logger, adjustSettings func(*Sub) *Sub) func(context.Context) *Logger {
return func(ctx context.Context) *Logger {
logger := getLogFromContext(ctx)
return adjustSettings(logger.Sub()).Logger()
}
}

Expand All @@ -72,19 +72,19 @@ func CustomFromContext(getLogFromContext func(context.Context) *Log, adjustSetti
//
// package foo
// var adjustLogger = xop.LevelAdjuster()
func LevelAdjuster(opts ...AdjusterOption) func(*Log) *Log {
func LevelAdjuster(opts ...AdjusterOption) func(*Logger) *Logger {
level := adjustConfig(opts)
if level == 0 {
return func(log *Log) *Log { return log }
return func(logger *Logger) *Logger { return logger }
}
return func(log *Log) *Log {
return log.Sub().MinLevel(level).Log()
return func(logger *Logger) *Logger {
return logger.Sub().MinLevel(level).Logger()
}
}

// ContextLevelAdjuster returns a function that gets a logger from
// context. The logger it returns will have a minimum logging level
// that is specific for the calling package. If starting from a log
// that is specific for the calling package. If starting from a logger
// instead of a context, use LevelAdjuster
//
// The level used will be the level set in this order:
Expand All @@ -96,7 +96,7 @@ func LevelAdjuster(opts ...AdjusterOption) func(*Log) *Log {
//
// package foo
// var getLogger = xop.AdjustedLevelLoger(xop.FromContextOrPanic)
func ContextLevelAdjuster(getLogFromContext func(context.Context) *Log, opts ...AdjusterOption) func(context.Context) *Log {
func ContextLevelAdjuster(getLogFromContext func(context.Context) *Logger, opts ...AdjusterOption) func(context.Context) *Logger {
level := adjustConfig(opts)
if level == 0 {
return getLogFromContext
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCustomFromContext(t *testing.T) {
defer customLog.Done()
assert.NotEqual(t, Default, customLog, "logs are not equal")
assert.NotEqual(t, Default.Settings(), customLog.Settings(), "default settings")
assert.Equal(t, noFilenameFunc(Default.Sub().PrefillText("banana").Log().Settings()), noFilenameFunc(customLog.Settings()), "modified settings")
assert.Equal(t, noFilenameFunc(Default.Sub().PrefillText("banana").Logger().Settings()), noFilenameFunc(customLog.Settings()), "modified settings")
}

func noFilenameFunc(settings LogSettings) LogSettings {
Expand Down
3 changes: 1 addition & 2 deletions internal/enumer_test.zzzgo
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package internal_test
import (
"encoding/json"
"strconv"
"strings"
"testing"

"github.com/xoplog/xop-go/xopbase"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
18 changes: 9 additions & 9 deletions line.go

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

18 changes: 9 additions & 9 deletions line.zzzgo
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func (line *Line) Any(k string, v interface{}) *Line {
if line.skip {
return line
}
if line.log.settings.redactAny != nil {
line.log.settings.redactAny(line.line, k, v, !line.log.span.referencesKept)
if line.logger.settings.redactAny != nil {
line.logger.settings.redactAny(line.line, k, v, !line.logger.span.referencesKept)
return line
}
if line.log.span.referencesKept {
if line.logger.span.referencesKept {
// TODO: make copy function configurable
v = deepcopy.Copy(v)
}
Expand Down Expand Up @@ -62,8 +62,8 @@ func (line *Line) Error(k string, v error) *Line {
if line.skip {
return line
}
if line.log.settings.redactError != nil {
line.log.settings.redactError(line.line, k, v)
if line.logger.settings.redactError != nil {
line.logger.settings.redactError(line.line, k, v)
return line
}
line.line.String(k, v.Error(), xopbase.ErrorDataType)
Expand All @@ -77,8 +77,8 @@ func (line *Line) Stringer(k string, v fmt.Stringer) *Line {
if line.skip {
return line
}
if line.log.settings.redactString != nil {
line.log.settings.redactString(line.line, k, v.String())
if line.logger.settings.redactString != nil {
line.logger.settings.redactString(line.line, k, v.String())
return line
}
line.line.String(k, v.String(), xopbase.StringerDataType)
Expand All @@ -92,8 +92,8 @@ func (line *Line) String(k string, v string) *Line {
if line.skip {
return line
}
if line.log.settings.redactString != nil {
line.log.settings.redactString(line.line, k, v)
if line.logger.settings.redactString != nil {
line.logger.settings.redactString(line.line, k, v)
return line
}
line.line.String(k, v, xopbase.StringDataType)
Expand Down
Loading

0 comments on commit a5ad1de

Please sign in to comment.