Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit f40bafa

Browse files
committed
WIP Fix debugserver logging level endpoints
1 parent e3d7611 commit f40bafa

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

logger/logger.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"sync"
99
"time"
1010

11+
"code.cloudfoundry.org/lager/v3"
1112
"go.uber.org/zap"
1213
"go.uber.org/zap/exp/zapslog"
1314
"go.uber.org/zap/zapcore"
1415
)
1516

1617
var (
17-
conf dynamicLoggingConfig
18+
Conf DynamicLoggingConfig
1819
baseLogger *slog.Logger
1920
writeSyncer = &dynamicWriter{w: os.Stdout}
2021
mutex sync.Mutex
@@ -23,7 +24,7 @@ var (
2324
/*
2425
dynamicLoggingConfig holds dynamic configuration for the time encoding and logging level.
2526
*/
26-
type dynamicLoggingConfig struct {
27+
type DynamicLoggingConfig struct {
2728
encoding string
2829
level zap.AtomicLevel
2930
}
@@ -74,10 +75,10 @@ SetTimeEncoder dynamically sets the time encoder at runtime:
7475
All other values: The encoder is set to an Epoch encoder
7576
*/
7677
func SetTimeEncoder(enc string) {
77-
conf.encoding = enc
78+
Conf.encoding = enc
7879
}
7980

80-
func (e *dynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) {
81+
func (e *DynamicLoggingConfig) encodeTime(t time.Time, pae zapcore.PrimitiveArrayEncoder) {
8182
switch e.encoding {
8283
case "rfc3339":
8384
RFC3339Formatter()(t, pae)
@@ -95,7 +96,27 @@ func SetLoggingLevel(level string) {
9596
if err != nil {
9697
panic(err)
9798
}
98-
conf.level.SetLevel(zapLevel)
99+
Conf.level.SetLevel(zapLevel)
100+
}
101+
102+
// This exists to be able to export the logging level configs to the debugserver
103+
func (loggingConf DynamicLoggingConfig) SetMinLevel(level lager.LogLevel) {
104+
Conf.level.SetLevel(toZapLevel(level))
105+
}
106+
107+
func toZapLevel(level lager.LogLevel) zapcore.Level {
108+
switch level {
109+
case lager.DEBUG:
110+
return zapcore.DebugLevel
111+
case lager.INFO:
112+
return zapcore.InfoLevel
113+
case lager.ERROR:
114+
return zapcore.ErrorLevel
115+
case lager.FATAL:
116+
return zapcore.FatalLevel
117+
default:
118+
return zapcore.InfoLevel
119+
}
99120
}
100121

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

111-
conf = dynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)}
132+
Conf = DynamicLoggingConfig{encoding: "epoch", level: zap.NewAtomicLevelAt(zapLevel)}
112133

113134
zapConfig := zapcore.EncoderConfig{
114135
MessageKey: "message",
115136
LevelKey: "log_level",
116137
EncodeLevel: numberLevelFormatter,
117138
TimeKey: "timestamp",
118-
EncodeTime: conf.encodeTime,
139+
EncodeTime: Conf.encodeTime,
119140
EncodeCaller: zapcore.ShortCallerEncoder,
120141
StacktraceKey: "stack_trace",
121142
}
122143

123144
zapCore := zapcore.NewCore(
124145
zapcore.NewJSONEncoder(zapConfig),
125146
writeSyncer,
126-
conf.level,
147+
Conf.level,
127148
)
128149

129150
zapHandler := zapslog.NewHandler(zapCore, zapslog.WithCaller(true))

main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.cloudfoundry.org/clock"
1616
"code.cloudfoundry.org/debugserver"
1717
mr "code.cloudfoundry.org/go-metric-registry"
18-
"code.cloudfoundry.org/lager/v3"
1918
"code.cloudfoundry.org/tlsconfig"
2019
"github.com/cloudfoundry/dropsonde"
2120
"github.com/cloudfoundry/dropsonde/metric_sender"
@@ -107,8 +106,7 @@ func main() {
107106
}
108107

109108
if c.DebugAddr != "" {
110-
reconfigurableSink := lager.ReconfigurableSink{}
111-
_, err = debugserver.Run(c.DebugAddr, &reconfigurableSink)
109+
_, err = debugserver.Run(c.DebugAddr, *grlog.Conf)
112110
if err != nil {
113111
logger.Error("failed-to-start-debug-server", grlog.ErrAttr(err))
114112
}

0 commit comments

Comments
 (0)