@@ -8,16 +8,13 @@ import (
88 "log/slog"
99 "os"
1010 "runtime"
11- "strings"
1211 "syscall"
1312 "time"
1413
1514 "code.cloudfoundry.org/gorouter/metrics_prometheus"
16- "go.uber.org/zap/zapcore"
1715
1816 "code.cloudfoundry.org/clock"
1917 "code.cloudfoundry.org/debugserver"
20- "code.cloudfoundry.org/lager/v3"
2118 "code.cloudfoundry.org/tlsconfig"
2219 "github.com/cloudfoundry/dropsonde"
2320 "github.com/cloudfoundry/dropsonde/metric_sender"
@@ -52,81 +49,13 @@ var (
5249 h * health.Health
5350)
5451
55- // zapLevelSink is a lager sink that uses a slog.Logger for logging.
56- // It implements the lager.Sink interface, allowing it to be used with
57- // lager's logging system. The Log method logs the message and source
58- // using the slog.Logger, and the LogLevel method returns the current
59- // logging level.
60-
61- type zapLevelSink struct {
62- logger * slog.Logger
63- }
64-
65- func (z * zapLevelSink ) Log (lf lager.LogFormat ) {
66- z .logger .Info ("zapLevelSink Log()" , slog .String ("Zap Level Sink log invoked" , lf .Message ),
67- slog .String ("source" , lf .Source ))
68- }
69-
70- func (z * zapLevelSink ) LogLevel () lager.LogLevel {
71- switch strings .ToLower (grlog .GetLoggingLevel ()) {
72- case "debug" :
73- return lager .DEBUG
74- case "info" :
75- return lager .INFO
76- case "error" :
77- return lager .ERROR
78- case "fatal" :
79- return lager .FATAL
80- default :
81- return lager .INFO
82- }
83- }
84-
85- // The SetMinLevel method updates the logging level of the
86- // zapLevelSink based on the provided lager.LogLevel, mapping it to
87- // the corresponding zapcore.Level. The logger is expected to be set
88- // before using this sink, and it will log an info message when the
89- // logging level is updated.
90-
91- func (z * zapLevelSink ) SetMinLevel (level lager.LogLevel ) {
92- var zapLevel zapcore.Level
93- switch level {
94- case lager .DEBUG :
95- zapLevel = zapcore .DebugLevel
96- case lager .INFO :
97- zapLevel = zapcore .InfoLevel
98- case lager .ERROR , lager .FATAL :
99- // For lager.ERROR and lager.FATAL, we use zapcore.ErrorLevel.
100- // This is because zapcore.ErrorLevel captures both error and fatal logs.
101- // Using zapcore.FatalLevel would cause the application to exit immediately.
102- // Because zapcore.FatalLevel calls os.Exit(1) after logging the message.
103- zapLevel = zapcore .ErrorLevel
104- default :
105- zapLevel = zapcore .InfoLevel
106- }
107-
108- grlog .SetLoggingLevel (zapLevel .String ())
109-
110- // Print the new log level to the logger to confirm the change.
111- // This is useful for debugging and confirming that the log level has been updated.
112- if z .logger != nil {
113- // We cannot use z.logger.Info() directly here because it won't print the
114- // log level when it is set to zapcore.ErrorLevel or zapcore.FatalLevel.
115- // Instead, we use slog.New() to log the message. This ensures that the
116- // log level change is always logged, regardless of the current log level.
117- tmpLogger := slog .New (slog .NewTextHandler (os .Stdout , nil ))
118- tmpLogger .Info ("Gorouter logger -> zapcore log level updated." , slog .String ("new-level" , z .LogLevel ().String ()))
119- }
120- }
121-
12252func main () {
12353 flag .StringVar (& configFile , "c" , "" , "Configuration File" )
12454 flag .Parse ()
12555
12656 prefix := "gorouter.stdout"
12757 coreLogger := grlog .CreateLogger ()
12858 grlog .SetLoggingLevel ("INFO" )
129- coreLogger .Info ("gorouter-startup-test-log" , slog .String ("test" , "true" ))
13059
13160 c , err := config .DefaultConfig ()
13261 if err != nil {
@@ -173,28 +102,17 @@ func main() {
173102
174103 // start the debugserver
175104 if c .DebugAddr != "" {
176- logLevel , logErr := lager .LogLevelFromString (c .Logging .Level )
177- if logErr != nil {
178- logger .Error ("invalid-log-level" , grlog .ErrAttr (logErr ))
179- }
180-
181105 // create a zapLevelSink that uses the logger
182106 sink := & zapLevelSink {logger : logger }
183-
184- // create a reconfigurable sink that can change log levels at runtime
185- reconfigurableSink := lager .NewReconfigurableSink (sink , logLevel )
186-
187- // start the debugserver with the reconfigurable sink
107+ // create a new debugserver with the address and the sink and run it
188108 _ , err = debugserver .Run (c .DebugAddr , sink )
189109 if err != nil {
190110 logger .Error ("failed-to-start-debug-server" , grlog .ErrAttr (err ))
191111 }
192-
193112 logger .Info ("debugserver-started" ,
194113 slog .String ("address" , c .DebugAddr ),
195114 slog .String ("log_level" , c .Logging .Level ),
196115 slog .String ("log_format" , c .Logging .Format .Timestamp ),
197- slog .String ("log_level_from_sink" , reconfigurableSink .GetMinLevel ().String ()),
198116 )
199117 }
200118
0 commit comments