From 0ef35fdb36cc318c1be81b17f71591045f09ff94 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:50:05 +0200 Subject: [PATCH] Improve logger (#322) * Improve logger * Update docs --- config.yml | 2 +- docs/configuration/cheat-sheet.md | 2 +- internal/config/config.go | 24 ++++++++++++++++++++---- internal/web/server.go | 8 ++------ internal/web/util.go | 6 ++++++ 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/config.yml b/config.yml index 604a6bb..2942714 100644 --- a/config.yml +++ b/config.yml @@ -2,7 +2,7 @@ # https://github.com/thomiceli/opengist/blob/master/docs/configuration/index.md # https://github.com/thomiceli/opengist/blob/master/docs/configuration/cheat-sheet.md -# Set the log level to one of the following: trace, debug, info, warn, error, fatal, panic. Default: warn +# Set the log level to one of the following: debug, info, warn, error, fatal. Default: warn log-level: warn # Set the log output to one or more of the following: `stdout`, `file`. Default: stdout,file diff --git a/docs/configuration/cheat-sheet.md b/docs/configuration/cheat-sheet.md index ef7768f..d182162 100644 --- a/docs/configuration/cheat-sheet.md +++ b/docs/configuration/cheat-sheet.md @@ -2,7 +2,7 @@ | YAML Config Key | Environment Variable | Default value | Description | |-----------------------|-------------------------------------|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| log-level | OG_LOG_LEVEL | `warn` | Set the log level to one of the following: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`. | +| log-level | OG_LOG_LEVEL | `warn` | Set the log level to one of the following: `debug`, `info`, `warn`, `error`, `fatal`. | | log-output | OG_LOG_OUTPUT | `stdout,file` | Set the log output to one or more of the following: `stdout`, `file`. | | external-url | OG_EXTERNAL_URL | none | Public URL to access to Opengist. | | opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. | diff --git a/internal/config/config.go b/internal/config/config.go index 6133019..c47a429 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,6 +10,7 @@ import ( "slices" "strconv" "strings" + "time" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -153,6 +154,21 @@ func InitLog() { logOutputTypes := utils.RemoveDuplicates[string]( strings.Split(strings.ToLower(C.LogOutput), ","), ) + + consoleWriter := zerolog.NewConsoleWriter( + func(w *zerolog.ConsoleWriter) { + w.TimeFormat = time.TimeOnly + w.FormatCaller = func(i interface{}) string { + file := i.(string) + index := strings.Index(file, "internal") + if index == -1 { + return file + } + return file[index:] + } + }, + ) + for _, logOutputType := range logOutputTypes { logOutputType = strings.TrimSpace(logOutputType) if !slices.Contains([]string{"stdout", "file"}, logOutputType) { @@ -162,7 +178,7 @@ func InitLog() { switch logOutputType { case "stdout": - logWriters = append(logWriters, zerolog.NewConsoleWriter()) + logWriters = append(logWriters, consoleWriter) defer func() { log.Debug().Msg("Logging to stdout") }() case "file": file, err := os.OpenFile(filepath.Join(GetHomeDir(), "log", "opengist.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) @@ -174,14 +190,14 @@ func InitLog() { } } if len(logWriters) == 0 { - logWriters = append(logWriters, zerolog.NewConsoleWriter()) + logWriters = append(logWriters, consoleWriter) defer func() { log.Warn().Msg("No valid log outputs, defaulting to stdout") }() } multi := zerolog.MultiLevelWriter(logWriters...) - log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger() + log.Logger = zerolog.New(multi).Level(level).With().Caller().Timestamp().Logger() - if !slices.Contains([]string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, strings.ToLower(C.LogLevel)) { + if !slices.Contains([]string{"debug", "info", "warn", "error", "fatal"}, strings.ToLower(C.LogLevel)) { log.Warn().Msg("Invalid log level: " + C.LogLevel) } } diff --git a/internal/web/server.go b/internal/web/server.go index 32c4b3c..a41f075 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -189,8 +189,8 @@ func NewServer(isDev bool, sessionsPath string) *Server { e.Pre(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{ LogURI: true, LogStatus: true, LogMethod: true, LogValuesFunc: func(ctx echo.Context, v middleware.RequestLoggerValues) error { - log.Info().Str("URI", v.URI).Int("status", v.Status).Str("method", v.Method). - Str("ip", ctx.RealIP()). + log.Info().Str("uri", v.URI).Int("status", v.Status).Str("method", v.Method). + Str("ip", ctx.RealIP()).TimeDiff("duration", time.Now(), v.StartTime). Msg("HTTP") return nil }, @@ -216,10 +216,6 @@ func NewServer(isDev bool, sessionsPath string) *Server { e.HTTPErrorHandler = func(er error, ctx echo.Context) { if err, ok := er.(*echo.HTTPError); ok { - if err.Code >= 500 { - log.Error().Int("code", err.Code).Err(err.Internal).Msg("HTTP: " + err.Message.(string)) - } - setData(ctx, "error", err) if errHtml := htmlWithCode(ctx, err.Code, "error.html"); errHtml != nil { log.Fatal().Err(errHtml).Send() diff --git a/internal/web/util.go b/internal/web/util.go index 732ed96..7e355e5 100644 --- a/internal/web/util.go +++ b/internal/web/util.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/gorilla/sessions" "github.com/labstack/echo/v4" + "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/i18n" @@ -58,6 +59,11 @@ func notFound(message string) error { } func errorRes(code int, message string, err error) error { + if code >= 500 { + var skipLogger = log.With().CallerWithSkipFrameCount(3).Logger() + skipLogger.Error().Err(err).Msg(message) + } + return &echo.HTTPError{Code: code, Message: message, Internal: err} }