Skip to content

Commit

Permalink
Add debug check to logp.Logger (elastic#7965)
Browse files Browse the repository at this point in the history
This PR adds a debug check

I made sure to check if `Desugar()` is expensive or not.  The API is documented as follows:

```
// Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring
// is quite inexpensive, so it's reasonable for a single application to use
// both Loggers and SugaredLoggers, converting between them on the boundaries
// of performance-sensitive code.
```
  • Loading branch information
vjsamuel authored and ruflin committed Aug 15, 2018
1 parent 9095426 commit 5b54084
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ https://github.com/elastic/beats/compare/v6.4.0...master[Check the HEAD diff]
internal monitoring data. {issue}7807[7807]
- Allow for cloud-id to specify a custom port. This makes cloud-id work in ECE contexts. {pull}7887[7887]
- Add support to grow or shrink an existing spool file between restarts. {pull}7859[7859]
- Add debug check to logp.Logger {pull}7965[7965]
- Make kubernetes autodiscover ignore events with empty container IDs {pull}7971[7971]

*Auditbeat*
Expand Down
17 changes: 13 additions & 4 deletions libbeat/logp/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ package logp

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// LogOption configures a Logger.
type LogOption = zap.Option

// Logger logs messages to the configured output.
type Logger struct {
sugar *zap.SugaredLogger
logger *zap.Logger
sugar *zap.SugaredLogger
}

func newLogger(rootLogger *zap.Logger, selector string, options ...LogOption) *Logger {
log := rootLogger.
WithOptions(zap.AddCallerSkip(1)).
WithOptions(options...).
Named(selector)
return &Logger{log.Sugar()}
return &Logger{log, log.Sugar()}
}

// NewLogger returns a new Logger labeled with the name of the selector. This
Expand All @@ -49,13 +51,15 @@ func NewLogger(selector string, options ...LogOption) *Logger {
// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
func (l *Logger) With(args ...interface{}) *Logger {
return &Logger{l.sugar.With(args...)}
sugar := l.sugar.With(args...)
return &Logger{sugar.Desugar(), sugar}
}

// Named adds a new path segment to the logger's name. Segments are joined by
// periods.
func (l *Logger) Named(name string) *Logger {
return &Logger{l.sugar.Named(name)}
logger := l.logger.Named(name)
return &Logger{logger, logger.Sugar()}
}

// Sprint
Expand Down Expand Up @@ -96,6 +100,11 @@ func (l *Logger) DPanic(args ...interface{}) {
l.sugar.DPanic(args...)
}

// IsDebug checks to see if the given logger is Debug enabled.
func (l *Logger) IsDebug() bool {
return l.logger.Check(zapcore.DebugLevel, "") != nil
}

// Sprintf

// Debugf uses fmt.Sprintf to construct and log a message.
Expand Down

0 comments on commit 5b54084

Please sign in to comment.