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

Disable zap sampling and cleanup config #102620

Merged
merged 1 commit into from
Jun 7, 2021
Merged
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
72 changes: 37 additions & 35 deletions staging/src/k8s.io/component-base/logs/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package logs

import (
"os"
"time"

"github.com/go-logr/logr"
Expand All @@ -35,6 +34,38 @@ var (
timeNow = time.Now
)

func init() {
JSONLogger = NewJSONLogger(nil)
}

// NewJSONLogger creates a new json logr.Logger using the given Zap Logger to log.
func NewJSONLogger(w zapcore.WriteSyncer) logr.Logger {
l, _ := zapConfig.Build()
l = l.WithOptions(zap.AddCallerSkip(1))
if w != nil {
l = l.WithOptions(zap.WrapCore(
func(zapcore.Core) zapcore.Core {
return zapcore.NewCore(zapcore.NewJSONEncoder(zapConfig.EncoderConfig), zapcore.AddSync(w), zapcore.DebugLevel)
}))
}
return &zapLogger{l: l}
}

var zapConfig = zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: false,
Sampling: nil,
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "msg",
TimeKey: "ts",
EncodeTime: zapcore.EpochMillisTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}

// zapLogger is a logr.Logger that uses Zap to record log.
type zapLogger struct {
// NB: this looks very similar to zap.SugaredLogger, but
Expand All @@ -43,7 +74,7 @@ type zapLogger struct {
lvl int
}

// implement logr.Logger
// zapLogger implement logr.Logger
var _ logr.Logger = &zapLogger{}

// Enabled should always return true
Expand Down Expand Up @@ -124,6 +155,10 @@ func (l *zapLogger) Error(err error, msg string, keysAndVals ...interface{}) {
checkedEntry.Write(l.handleFields(keysAndVals, handleError(err))...)
}

func handleError(err error) zap.Field {
return zap.NamedError("err", err)
}

// V return info logr.Logger with specified level
func (l *zapLogger) V(level int) logr.Logger {
return &zapLogger{
Expand All @@ -143,36 +178,3 @@ func (l *zapLogger) WithName(name string) logr.Logger {
l.l = l.l.Named(name)
return l
}

// encoderConfig config zap encodetime format
var encoderConfig = zapcore.EncoderConfig{
MessageKey: "msg",

TimeKey: "ts",
EncodeTime: zapcore.EpochMillisTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}

// NewJSONLogger creates a new json logr.Logger using the given Zap Logger to log.
func NewJSONLogger(w zapcore.WriteSyncer) logr.Logger {
l, _ := zap.NewProduction()
if w == nil {
w = os.Stdout
}
log := l.WithOptions(zap.AddCallerSkip(1),
zap.WrapCore(
func(zapcore.Core) zapcore.Core {
return zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), zapcore.AddSync(w), zapcore.DebugLevel)
}))
return &zapLogger{
l: log,
}
}

func handleError(err error) zap.Field {
return zap.NamedError("err", err)
}

func init() {
JSONLogger = NewJSONLogger(nil)
}