Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to wrap new subloggers #126

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type intLogger struct {

// create subloggers with their own level setting
independentLevels bool

subloggerHook func(sub Logger) Logger
}

// New returns a configured logger.
Expand Down Expand Up @@ -151,6 +153,7 @@ func newLogger(opts *LoggerOptions) *intLogger {
independentLevels: opts.IndependentLevels,
headerColor: headerColor,
fieldColor: fieldColor,
subloggerHook: opts.SubloggerHook,
}
if opts.IncludeLocation {
l.callerOffset = offsetIntLogger + opts.AdditionalLocationOffset
Expand All @@ -166,13 +169,21 @@ func newLogger(opts *LoggerOptions) *intLogger {
l.timeFormat = opts.TimeFormat
}

if l.subloggerHook == nil {
l.subloggerHook = identityHook
}

l.setColorization(opts)

atomic.StoreInt32(l.level, int32(level))

return l
}

func identityHook(logger Logger) Logger {
return logger
}

// offsetIntLogger is the stack frame offset in the call stack for the caller to
// one of the Warn, Info, Log, etc methods.
const offsetIntLogger = 3
Expand Down Expand Up @@ -774,7 +785,7 @@ func (l *intLogger) With(args ...interface{}) Logger {
sl.implied = append(sl.implied, MissingKey, extra)
}

return sl
return l.subloggerHook(sl)
}

// Create a new sub-Logger that a name decending from the current name.
Expand All @@ -788,7 +799,7 @@ func (l *intLogger) Named(name string) Logger {
sl.name = name
}

return sl
return l.subloggerHook(sl)
}

// Create a new sub-Logger with an explicit name. This ignores the current
Expand All @@ -799,7 +810,7 @@ func (l *intLogger) ResetNamed(name string) Logger {

sl.name = name

return sl
return l.subloggerHook(sl)
}

func (l *intLogger) ResetOutput(opts *LoggerOptions) error {
Expand Down
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ type LoggerOptions struct {
// logger will not affect any subloggers, and SetLevel on any subloggers
// will not affect the parent or sibling loggers.
IndependentLevels bool

// SubloggerHook registers a function that is called when a sublogger via
// Named, With, or ResetNamed is created. If defined, the function is passed
// the newly created Logger and the returned Logger is returned from the
// original function. This option allows customization via interception and
// wrapping of Logger instances.
SubloggerHook func(sub Logger) Logger
}

// InterceptLogger describes the interface for using a logger
Expand Down