Skip to content

Commit

Permalink
Log Coordinator's http internal errors to zap logger
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Nov 15, 2024
1 parent 21c4dca commit f16b632
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions coordinator/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("", "")
Expand Down
43 changes: 43 additions & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f16b632

Please sign in to comment.