@@ -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
1617var (
17- conf dynamicLoggingConfig
18+ Conf DynamicLoggingConfig
1819 baseLogger * slog.Logger
1920 writeSyncer = & dynamicWriter {w : os .Stdout }
2021 mutex sync.Mutex
2324/*
2425dynamicLoggingConfig 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:
7475All other values: The encoder is set to an Epoch encoder
7576*/
7677func 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
101122type Logger interface {
@@ -108,22 +129,22 @@ timestamp format and writeSyncer.
108129func 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 ))
0 commit comments