From 6d84c5b9e3d56d5015f150259bf7fafaf7c894f6 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 3 Jan 2021 10:38:28 +0100 Subject: [PATCH] capture http servers error logs otherwise they will be printed to stdout --- httpd/httpd.go | 2 ++ logger/logger.go | 18 ++++++++++++++++++ telemetry/telemetry.go | 2 ++ webdavd/server.go | 2 ++ 4 files changed, 24 insertions(+) diff --git a/httpd/httpd.go b/httpd/httpd.go index 12085850..d9726853 100644 --- a/httpd/httpd.go +++ b/httpd/httpd.go @@ -9,6 +9,7 @@ package httpd import ( "crypto/tls" "fmt" + "log" "net/http" "path/filepath" "runtime" @@ -154,6 +155,7 @@ func (c Conf) Initialize(configDir string) error { WriteTimeout: 60 * time.Second, IdleTimeout: 120 * time.Second, MaxHeaderBytes: 1 << 16, // 64KB + ErrorLog: log.New(&logger.StdLoggerWrapper{Sender: logSender}, "", 0), } if certificateFile != "" && certificateKeyFile != "" { certMgr, err = common.NewCertManager(certificateFile, certificateKeyFile, logSender) diff --git a/logger/logger.go b/logger/logger.go index 98014a1a..e87c4aae 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -40,6 +40,24 @@ var ( rollingLogger *lumberjack.Logger ) +// StdLoggerWrapper is a wrapper for standard logger compatibility +type StdLoggerWrapper struct { + Sender string +} + +// Write implements the io.Writer interface. This is useful to set as a writer +// for the standard library log. +func (l *StdLoggerWrapper) Write(p []byte) (n int, err error) { + n = len(p) + if n > 0 && p[n-1] == '\n' { + // Trim CR added by stdlog. + p = p[0 : n-1] + } + + Log(LevelError, l.Sender, "", string(p)) + return +} + // GetLogger get the configured logger instance func GetLogger() *zerolog.Logger { return &logger diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index 8e43625a..28e1cb57 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -6,6 +6,7 @@ package telemetry import ( "crypto/tls" + "log" "net/http" "path/filepath" "runtime" @@ -82,6 +83,7 @@ func (c Conf) Initialize(configDir string) error { WriteTimeout: 60 * time.Second, IdleTimeout: 120 * time.Second, MaxHeaderBytes: 1 << 14, // 16KB + ErrorLog: log.New(&logger.StdLoggerWrapper{Sender: logSender}, "", 0), } if certificateFile != "" && certificateKeyFile != "" { certMgr, err = common.NewCertManager(certificateFile, certificateKeyFile, logSender) diff --git a/webdavd/server.go b/webdavd/server.go index 52f81d13..52571b70 100644 --- a/webdavd/server.go +++ b/webdavd/server.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "log" "net/http" "path" "path/filepath" @@ -42,6 +43,7 @@ func (s *webDavServer) listenAndServe() error { ReadHeaderTimeout: 30 * time.Second, IdleTimeout: 120 * time.Second, MaxHeaderBytes: 1 << 16, // 64KB + ErrorLog: log.New(&logger.StdLoggerWrapper{Sender: logSender}, "", 0), } if s.config.Cors.Enabled { c := cors.New(cors.Options{