From 1fd7946dce38550114c53fb863194a71fc63d40b Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 26 Jan 2023 18:16:16 +0100 Subject: [PATCH] Print golang http errors in MinIO log format (#16465) --- cmd/server-main.go | 2 +- cmd/utils.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/server-main.go b/cmd/server-main.go index 9fc2d4fc4..4450199a6 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -589,7 +589,7 @@ func serverMain(ctx *cli.Context) { UseIdleTimeout(ctx.Duration("idle-timeout")). UseReadHeaderTimeout(ctx.Duration("read-header-timeout")). UseBaseContext(GlobalContext). - UseCustomLogger(log.New(io.Discard, "", 0)) // Turn-off random logging by Go stdlib + UseCustomLogger(log.New(&goHTTPLogger{}, "", 0)) go func() { globalHTTPServerErrorCh <- httpServer.Start(GlobalContext) diff --git a/cmd/utils.go b/cmd/utils.go index 0c0f21d09..ee8b559b1 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1245,3 +1245,13 @@ func MockOpenIDTestUserInteraction(ctx context.Context, pro OpenIDClientAppParam // fmt.Printf("TOKEN: %s\n", rawIDToken) return rawIDToken, nil } + +// Custom logger for golang http logs +type goHTTPLogger struct{} + +func (goHTTPLogger) Write(p []byte) (int, error) { + // Golang std logger ensures that the error message will be sent + // in one Write call so it is okay to print it directly. + logger.LogOnceIf(context.Background(), fmt.Errorf("%s", string(p)), "go-http-logging") + return len(p), nil +}