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

Cherry-pick #7965 to 6.x: Add debug check to logp.Logger #9611

Merged
merged 1 commit into from
Dec 18, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ https://github.com/elastic/beats/compare/v6.4.0...v6.5.0[View commits]
- Add backoff on error support to redis output. {pull}7781[7781]
- 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]
- Implement CheckConfig in RunnerFactory to make autodiscover check configs {pull}7961[7961]
- Add DNS processor with support for performing reverse lookups on IP addresses. {issue}7770[7770]
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