Skip to content

Commit

Permalink
inherit debug logging to custom loggers, fixes issue #384
Browse files Browse the repository at this point in the history
  • Loading branch information
frairon committed Jun 9, 2022
1 parent 2ff0aea commit dc3bd4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 9 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"github.com/Shopify/sarama"
)

var (
defaultLogger = &std{
log: log.New(os.Stderr, "", log.LstdFlags),
}
)
var defaultLogger = &std{
log: log.New(os.Stderr, "", log.LstdFlags),
}

// Logger is the interface Goka and its subpackages use for logging.
type Logger interface {
Expand Down Expand Up @@ -77,6 +75,8 @@ func DefaultLogger() Logger {
}

// Debug enables or disables debug logging using the global logger.
// The goka debugging setting is applied to any custom loggers in goka components (Processors, Views, Emitters).
// To enable debug logging for custom loggers in selected components only, pass 'true' in the respective 'WithLogger' options.
func Debug(gokaDebug, saramaDebug bool) {
defaultLogger.debug = gokaDebug
if saramaDebug {
Expand All @@ -89,15 +89,17 @@ func SetSaramaLogger(logger Logger) {
}

// newLogger creates a new goka logger
func wrapLogger(l Logger) logger {
func wrapLogger(l Logger, debug bool) logger {
return &std{
log: l,
log: l,
debug: debug,
}
}

func (s *std) CurrentPrefix() string {
return s.prefix
}

func (s *std) StackPrefix(prefix string) logger {
var prefPath []string
// append existing path
Expand Down
10 changes: 6 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func DefaultHasher() func() hash.Hash32 {
return func() hash.Hash32 {
return fnv.New32a()
}

}

// DefaultUpdateContext implements the UpdateContext interface.
Expand Down Expand Up @@ -216,12 +215,13 @@ func WithPartitionChannelSize(size int) ProcessorOption {

// WithLogger sets the logger the processor should use. By default, processors
// use the standard library logger.
// To enable debug logging, pass true as an optional parameter
func WithLogger(l Logger) ProcessorOption {
return func(o *poptions, gg *GroupGraph) {
if prefixLogger, ok := l.(logger); ok {
o.log = prefixLogger
} else {
o.log = wrapLogger(l)
o.log = wrapLogger(l, defaultLogger.debug)
}
}
}
Expand Down Expand Up @@ -401,12 +401,13 @@ type voptions struct {

// WithViewLogger sets the logger the view should use. By default, views
// use the standard library logger.
// To enable debug logging, pass true as an optional parameter
func WithViewLogger(l Logger) ViewOption {
return func(o *voptions, table Table, codec Codec) {
if prefixLogger, ok := l.(logger); ok {
o.log = prefixLogger
} else {
o.log = wrapLogger(l)
o.log = wrapLogger(l, defaultLogger.debug)
}
}
}
Expand Down Expand Up @@ -550,12 +551,13 @@ type eoptions struct {

// WithEmitterLogger sets the logger the emitter should use. By default,
// emitters use the standard library logger.
// To enable debug logging, pass true as an optional parameter
func WithEmitterLogger(l Logger) EmitterOption {
return func(o *eoptions, topic Stream, codec Codec) {
if prefixLogger, ok := l.(logger); ok {
o.log = prefixLogger
} else {
o.log = wrapLogger(l)
o.log = wrapLogger(l, defaultLogger.debug)
}
}
}
Expand Down

0 comments on commit dc3bd4f

Please sign in to comment.