Skip to content

Commit

Permalink
WIP Fix debugserver logging level endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcPaquette committed Oct 29, 2024
1 parent e3d7611 commit f40bafa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
37 changes: 29 additions & 8 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"sync"
"time"

"code.cloudfoundry.org/lager/v3"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)

var (
conf dynamicLoggingConfig
Conf DynamicLoggingConfig
baseLogger *slog.Logger
writeSyncer = &dynamicWriter{w: os.Stdout}
mutex sync.Mutex
Expand All @@ -23,7 +24,7 @@ var (
/*
dynamicLoggingConfig holds dynamic configuration for the time encoding and logging level.
*/
type dynamicLoggingConfig struct {
type DynamicLoggingConfig struct {
encoding string
level zap.AtomicLevel
}
Expand Down Expand Up @@ -74,10 +75,10 @@ SetTimeEncoder dynamically sets the time encoder at runtime:
All other values: The encoder is set to an Epoch encoder
*/
func SetTimeEncoder(enc string) {
conf.encoding = enc
Conf.encoding = enc
}

func (e *dynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) {
func (e *DynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) {
switch e.encoding {
case "rfc3339":
RFC3339Formatter()(t, pae)
Expand All @@ -95,7 +96,27 @@ func SetLoggingLevel(level string) {
if err != nil {
panic(err)
}
conf.level.SetLevel(zapLevel)
Conf.level.SetLevel(zapLevel)
}

// This exists to be able to export the logging level configs to the debugserver
func (loggingConf DynamicLoggingConfig) SetMinLevel(level lager.LogLevel) {
Conf.level.SetLevel(toZapLevel(level))
}

func toZapLevel(level lager.LogLevel) zapcore.Level {
switch level {
case lager.DEBUG:
return zapcore.DebugLevel
case lager.INFO:
return zapcore.InfoLevel
case lager.ERROR:
return zapcore.ErrorLevel
case lager.FATAL:
return zapcore.FatalLevel
default:
return zapcore.InfoLevel
}
}

type Logger interface {
Expand All @@ -108,22 +129,22 @@ timestamp format and writeSyncer.
func initializeLogger() *slog.Logger {
zapLevel := zap.InfoLevel

conf = dynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)}
Conf = DynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)}

zapConfig := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "log_level",
EncodeLevel: numberLevelFormatter,
TimeKey: "timestamp",
EncodeTime: conf.encodeTime,
EncodeTime: Conf.encodeTime,
EncodeCaller: zapcore.ShortCallerEncoder,
StacktraceKey: "stack_trace",
}

zapCore := zapcore.NewCore(
zapcore.NewJSONEncoder(zapConfig),
writeSyncer,
conf.level,
Conf.level,
)

zapHandler := zapslog.NewHandler(zapCore, zapslog.WithCaller(true))
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/debugserver"
mr "code.cloudfoundry.org/go-metric-registry"
"code.cloudfoundry.org/lager/v3"
"code.cloudfoundry.org/tlsconfig"
"github.com/cloudfoundry/dropsonde"
"github.com/cloudfoundry/dropsonde/metric_sender"
Expand Down Expand Up @@ -107,8 +106,7 @@ func main() {
}

if c.DebugAddr != "" {
reconfigurableSink := lager.ReconfigurableSink{}
_, err = debugserver.Run(c.DebugAddr, &reconfigurableSink)
_, err = debugserver.Run(c.DebugAddr, *grlog.Conf)
if err != nil {
logger.Error("failed-to-start-debug-server", grlog.ErrAttr(err))
}
Expand Down

0 comments on commit f40bafa

Please sign in to comment.