From 645b1e8c9ac202759e446cf8d2f5d7ee59e67d11 Mon Sep 17 00:00:00 2001 From: Code by Ben Date: Fri, 6 Dec 2024 15:39:54 +1100 Subject: [PATCH] zaplog store encoders are not hard coded to a specific format. pprof is enabled with logall. --- handler/handler.go | 4 +++ handler/middleware.go | 4 +-- internal/config/error.go | 2 +- internal/zaplog/zaplog.go | 50 ++++++++++++++++++---------------- internal/zaplog/zaplog_test.go | 2 +- server.go | 8 +----- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 82e1104e..6b67a2f6 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -30,6 +30,7 @@ import ( "github.com/Defacto2/server/handler/html3" "github.com/Defacto2/server/handler/htmx" "github.com/Defacto2/server/internal/config" + "github.com/labstack/echo-contrib/pprof" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "go.uber.org/zap" @@ -74,6 +75,9 @@ func (c Configuration) Controller(db *sql.DB, logger *zap.SugaredLogger) *echo.E } e := echo.New() + if configs.LogAll { + pprof.Register(e) + } e.HideBanner = true e.HTTPErrorHandler = configs.CustomErrorHandler diff --git a/handler/middleware.go b/handler/middleware.go index c306017f..bacab5fc 100644 --- a/handler/middleware.go +++ b/handler/middleware.go @@ -106,8 +106,8 @@ func (c Configuration) configZapLogger() middleware.RequestLoggerConfig { logger := zaplog.Status().Sugar() if c.Environment.ProdMode { - absPath := c.Environment.AbsLog - logger = zaplog.Store(absPath).Sugar() + logPath := c.Environment.AbsLog + logger = zaplog.Store(zaplog.Text(), logPath).Sugar() } defer func() { _ = logger.Sync() diff --git a/internal/config/error.go b/internal/config/error.go index 1cf860b1..04748ef7 100644 --- a/internal/config/error.go +++ b/internal/config/error.go @@ -29,7 +29,7 @@ func (c Config) CustomErrorHandler(err error, ctx echo.Context) { logger := zaplog.Debug().Sugar() if c.ProdMode { root := c.AbsLog - logger = zaplog.Store(root).Sugar() + logger = zaplog.Store(zaplog.Text(), root).Sugar() } defer func() { _ = logger.Sync() diff --git a/internal/zaplog/zaplog.go b/internal/zaplog/zaplog.go index 9f980d72..cddb83a8 100644 --- a/internal/zaplog/zaplog.go +++ b/internal/zaplog/zaplog.go @@ -64,7 +64,7 @@ const ( // Status logger prints all log levels to stdout but without callers. func Status() *zap.Logger { - enc := consoleNoTime() + enc := TextNoTime() defaultLogLevel := zapcore.InfoLevel core := zapcore.NewTee( zapcore.NewCore( @@ -78,7 +78,7 @@ func Status() *zap.Logger { // Timestamp logger prints all log levels to stdout but without callers. func Timestamp() *zap.Logger { - enc := consoleWithTime() + enc := Text() defaultLogLevel := zapcore.InfoLevel core := zapcore.NewTee( zapcore.NewCore( @@ -92,7 +92,7 @@ func Timestamp() *zap.Logger { // Debug logger prints all log levels to stdout. func Debug() *zap.Logger { - enc := consoleWithTime() + enc := Text() defaultLogLevel := zapcore.DebugLevel core := zapcore.NewTee( zapcore.NewCore( @@ -104,17 +104,14 @@ func Debug() *zap.Logger { return zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)) } -// Store logger prints all info and higher log levels to files. +// Store logger prints all info and higher log levels to files using the encoder. +// Either the zaplog.JSON, zaplog.Text, or zaplog.TextNoTime encoders can be used. // Fatal and Panics are also returned to os.Stderr. -func Store(absPath string) *zap.Logger { - config := zap.NewProductionEncoderConfig() - config.EncodeTime = zapcore.TimeEncoderOfLayout("Jan-02-15:04:05.00") - jsonEnc := zapcore.NewJSONEncoder(config) - enc := consoleWithTime() - +func Store(enc zapcore.Encoder, logPath string) *zap.Logger { + errs := Text() // server breakage log serverWr := zapcore.AddSync(&lumberjack.Logger{ - Filename: filepath.Join(absPath, ServerLog), + Filename: filepath.Join(logPath, ServerLog), MaxSize: MaxSizeMB, MaxBackups: MaxBackups, MaxAge: MaxDays, @@ -124,7 +121,7 @@ func Store(absPath string) *zap.Logger { // information and warning log infoWr := zapcore.AddSync(&lumberjack.Logger{ - Filename: filepath.Join(absPath, InfoLog), + Filename: filepath.Join(logPath, InfoLog), MaxSize: MaxSizeMB, MaxBackups: MaxBackups, MaxAge: MaxDays, @@ -132,29 +129,36 @@ func Store(absPath string) *zap.Logger { core := zapcore.NewTee( // log to stderr - zapcore.NewCore(enc, errWr, zapcore.FatalLevel), - zapcore.NewCore(enc, errWr, zapcore.PanicLevel), + zapcore.NewCore(errs, errWr, zapcore.FatalLevel), + zapcore.NewCore(errs, errWr, zapcore.PanicLevel), // log to "server.log" - zapcore.NewCore(jsonEnc, serverWr, zapcore.FatalLevel), - zapcore.NewCore(jsonEnc, serverWr, zapcore.PanicLevel), - zapcore.NewCore(jsonEnc, serverWr, zapcore.ErrorLevel), + zapcore.NewCore(enc, serverWr, zapcore.FatalLevel), + zapcore.NewCore(enc, serverWr, zapcore.PanicLevel), + zapcore.NewCore(enc, serverWr, zapcore.ErrorLevel), // log to "info.log" - zapcore.NewCore(jsonEnc, infoWr, zapcore.WarnLevel), - zapcore.NewCore(jsonEnc, infoWr, zapcore.InfoLevel), + zapcore.NewCore(enc, infoWr, zapcore.WarnLevel), + zapcore.NewCore(enc, infoWr, zapcore.InfoLevel), ) return zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)) } -// console returns a logger in color and time. -func consoleWithTime() zapcore.Encoder { +// Json returns a logger in JSON format. +func Json() zapcore.Encoder { + config := zap.NewProductionEncoderConfig() + config.EncodeTime = zapcore.TimeEncoderOfLayout("Jan-02-15:04:05.00") + return zapcore.NewJSONEncoder(config) +} + +// Text returns a logger in color and time. +func Text() zapcore.Encoder { config := zap.NewDevelopmentEncoderConfig() config.EncodeTime = zapcore.TimeEncoderOfLayout("15:04:05") config.EncodeLevel = zapcore.CapitalColorLevelEncoder return zapcore.NewConsoleEncoder(config) } -// consoleNoTime returns a logger in color but without the time. -func consoleNoTime() zapcore.Encoder { +// TextNoTime returns a logger in color but without the time. +func TextNoTime() zapcore.Encoder { config := zap.NewDevelopmentEncoderConfig() // config.EncodeTime = nil // use nil to remove the leading console separator config.EncodeTime = zapcore.TimeEncoderOfLayout("") diff --git a/internal/zaplog/zaplog_test.go b/internal/zaplog/zaplog_test.go index c14c8e2b..98aa6e40 100644 --- a/internal/zaplog/zaplog_test.go +++ b/internal/zaplog/zaplog_test.go @@ -20,6 +20,6 @@ func TestLog(t *testing.T) { } func TestProduction(t *testing.T) { - logr := zaplog.Store("") + logr := zaplog.Store(zaplog.Json(), "") assert.NotNil(t, logr) } diff --git a/server.go b/server.go index d38ec597..ed5cbdc0 100644 --- a/server.go +++ b/server.go @@ -20,8 +20,6 @@ import ( "errors" "fmt" "io" - - //_ "net/http/pprof" // pprof is used for profiling and can be commented out. "os" "runtime" "slices" @@ -109,10 +107,6 @@ func main() { fmt.Fprintf(w, "%s\n", localIPs) }() - // go func() { - // log.Println(http.ListenAndServe("localhost:6060", nil)) - // }() - // shutdown the web server after a signal is received. website.ShutdownHTTP(router, logger) } @@ -192,7 +186,7 @@ func serverLog(configs config.Config, count int) *zap.SugaredLogger { if err := configs.LogStore(); err != nil { logger.Fatalf("%w using server log: %s", ErrLog, err) } - logger = zaplog.Store(configs.AbsLog).Sugar() + logger = zaplog.Store(zaplog.Text(), configs.AbsLog).Sugar() } return logger }