From f16b6328d878595e994fc922777a4c5fa537093c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= Date: Fri, 15 Nov 2024 08:45:36 +0100 Subject: [PATCH] Log Coordinator's http internal errors to zap logger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- coordinator/server/server.go | 2 ++ internal/logging/logging.go | 43 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 internal/logging/logging.go diff --git a/coordinator/server/server.go b/coordinator/server/server.go index cd7b6587..3821cf0d 100644 --- a/coordinator/server/server.go +++ b/coordinator/server/server.go @@ -21,6 +21,7 @@ import ( "github.com/edgelesssys/marblerun/coordinator/server/handler" v1 "github.com/edgelesssys/marblerun/coordinator/server/v1" v2 "github.com/edgelesssys/marblerun/coordinator/server/v2" + mrlogging "github.com/edgelesssys/marblerun/internal/logging" grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/prometheus/client_golang/prometheus" @@ -115,6 +116,7 @@ func RunClientServer(mux http.Handler, address string, tlsConfig *tls.Config, za Addr: address, Handler: mux, TLSConfig: tlsConfig, + ErrorLog: mrlogging.NewWrapper(zapLogger), } zapLogger.Info("Starting client https server", zap.String("address", address)) err := server.ListenAndServeTLS("", "") diff --git a/internal/logging/logging.go b/internal/logging/logging.go new file mode 100644 index 00000000..4c2a6ec4 --- /dev/null +++ b/internal/logging/logging.go @@ -0,0 +1,43 @@ +package logging + +import ( + "fmt" + "log" + "os" + + "github.com/edgelesssys/marblerun/coordinator/constants" + "github.com/edgelesssys/marblerun/util" + "go.uber.org/zap" +) + +// New creates a new [*zap.Logger]. +func New() *zap.Logger { + var cfg zap.Config + if util.Getenv(constants.DevMode, constants.DevModeDefault) == "1" { + cfg = zap.NewDevelopmentConfig() + } else { + cfg = zap.NewProductionConfig() + cfg.DisableStacktrace = true // Disable stacktraces in production + } + log, err := cfg.Build() + if err != nil { + fmt.Fprintf(os.Stderr, "failed to create logger: %s\n", err) + os.Exit(1) + } + return log +} + +// NewWrapper creates a new [*log.Logger] that writes to the given [*zap.Logger]. +func NewWrapper(zapLogger *zap.Logger) *log.Logger { + return log.New(logWrapper{zapLogger}, "", 0) +} + +// loggerWrapper implements [io.Writer] by writing any data to the error level of the embedded [*zap.Logger]. +type logWrapper struct { + *zap.Logger +} + +func (l logWrapper) Write(p []byte) (n int, err error) { + l.Error(string(p)) + return len(p), nil +}