Now TLS handshake errors will respect your loglevel setting
This commit is contained in:
@@ -22,6 +22,9 @@ var (
|
|||||||
// This is necessary to allow parallel test execution where each test
|
// This is necessary to allow parallel test execution where each test
|
||||||
// may initialize the logger with a different log file.
|
// may initialize the logger with a different log file.
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
|
||||||
|
// logLevel stores the configured log level for use by DebugLogger.
|
||||||
|
logLevel slog.Level
|
||||||
)
|
)
|
||||||
|
|
||||||
// slogWriter implements [io.Writer] to redirect standard log to slog.
|
// slogWriter implements [io.Writer] to redirect standard log to slog.
|
||||||
@@ -76,6 +79,8 @@ func Init(level, filename string) error {
|
|||||||
loglevel = slog.LevelInfo
|
loglevel = slog.LevelInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logLevel = loglevel
|
||||||
|
|
||||||
opts := &slog.HandlerOptions{
|
opts := &slog.HandlerOptions{
|
||||||
// Use the ReplaceAttr function on the handler options
|
// Use the ReplaceAttr function on the handler options
|
||||||
// to be able to replace any single attribute in the log output.
|
// to be able to replace any single attribute in the log output.
|
||||||
@@ -149,15 +154,15 @@ func Debugf(format string, a ...any) {
|
|||||||
slog.Debug(fmt.Sprintf(format, a...))
|
slog.Debug(fmt.Sprintf(format, a...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugLogger returns a standard library logger that writes to debug level.
|
// DebugLogger returns a standard library logger that writes at the configured log level.
|
||||||
// This is useful for assigning to http.Server.ErrorLog to move TLS handshake
|
// This is useful for assigning to http.Server.ErrorLog to route net/http errors
|
||||||
// and other client-side errors to debug level.
|
// through our logger while respecting the configured log level.
|
||||||
func DebugLogger() *log.Logger {
|
func DebugLogger() *log.Logger {
|
||||||
mu.RLock()
|
mu.RLock()
|
||||||
defer mu.RUnlock()
|
defer mu.RUnlock()
|
||||||
|
|
||||||
opts := &slog.HandlerOptions{
|
opts := &slog.HandlerOptions{
|
||||||
Level: slog.LevelDebug,
|
Level: logLevel,
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := slog.NewTextHandler(Log, opts)
|
handler := slog.NewTextHandler(Log, opts)
|
||||||
|
|||||||
Reference in New Issue
Block a user