Skip to content

Commit e9dc698

Browse files
committed
initial commit for TNZ-23119
1 parent fdce861 commit e9dc698

File tree

3 files changed

+120
-10
lines changed

3 files changed

+120
-10
lines changed

src/code.cloudfoundry.org/gorouter/cmd/gorouter/main.go

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"log/slog"
99
"os"
1010
"runtime"
11+
"strings"
1112
"syscall"
1213
"time"
1314

1415
"code.cloudfoundry.org/gorouter/metrics_prometheus"
16+
"go.uber.org/zap/zapcore"
1517

1618
"code.cloudfoundry.org/clock"
1719
"code.cloudfoundry.org/debugserver"
@@ -50,13 +52,70 @@ var (
5052
h *health.Health
5153
)
5254

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 (s *zapLevelSink) Log(lf lager.LogFormat) {
66+
s.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 (s *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+
zapLevel = zapcore.ErrorLevel
100+
default:
101+
zapLevel = zapcore.InfoLevel
102+
}
103+
104+
grlog.SetLoggingLevel(zapLevel.String())
105+
106+
if s.logger != nil {
107+
s.logger.Info("zapcore log level updated", slog.String("new-level", zapLevel.String()))
108+
}
109+
}
110+
53111
func main() {
54112
flag.StringVar(&configFile, "c", "", "Configuration File")
55113
flag.Parse()
56114

57115
prefix := "gorouter.stdout"
58116
coreLogger := grlog.CreateLogger()
59117
grlog.SetLoggingLevel("INFO")
118+
coreLogger.Info("gorouter-startup-test-log", slog.String("test", "true"))
60119

61120
c, err := config.DefaultConfig()
62121
if err != nil {
@@ -101,12 +160,31 @@ func main() {
101160
runtime.GOMAXPROCS(c.GoMaxProcs)
102161
}
103162

163+
// start the debugserver
104164
if c.DebugAddr != "" {
105-
reconfigurableSink := lager.ReconfigurableSink{}
106-
_, err = debugserver.Run(c.DebugAddr, &reconfigurableSink)
165+
logLevel, logErr := lager.LogLevelFromString(c.Logging.Level)
166+
if logErr != nil {
167+
logger.Error("invalid-log-level", grlog.ErrAttr(logErr))
168+
}
169+
170+
// create a zapLevelSink that uses the logger
171+
sink := &zapLevelSink{logger: logger}
172+
173+
// create a reconfigurable sink that can change log levels at runtime
174+
reconfigurableSink := lager.NewReconfigurableSink(sink, logLevel)
175+
176+
// start the debugserver with the reconfigurable sink
177+
_, err = debugserver.Run(c.DebugAddr, sink)
107178
if err != nil {
108179
logger.Error("failed-to-start-debug-server", grlog.ErrAttr(err))
109180
}
181+
182+
logger.Info("debugserver-started",
183+
slog.String("address", c.DebugAddr),
184+
slog.String("log_level", c.Logging.Level),
185+
slog.String("log_format", c.Logging.Format.Timestamp),
186+
slog.String("log_level_from_sink", reconfigurableSink.GetMinLevel().String()),
187+
)
110188
}
111189

112190
logger.Info("setting-up-nats-connection")

src/code.cloudfoundry.org/gorouter/logger/logger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ func SetLoggingLevel(level string) {
9898
conf.level.SetLevel(zapLevel)
9999
}
100100

101+
/*
102+
GetLoggingLevel returns the current logging level.
103+
*/
104+
func GetLoggingLevel() string {
105+
return conf.level.String()
106+
}
107+
108+
// Logger is an interface that can be used to mock the logger in tests.
101109
type Logger interface {
102110
}
103111

src/code.cloudfoundry.org/vendor/code.cloudfoundry.org/debugserver/server.go

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)