mirror of
https://github.com/thomiceli/opengist.git
synced 2024-12-22 20:42:40 +00:00
parent
cf4e0e303c
commit
0ef35fdb36
5 changed files with 30 additions and 12 deletions
|
@ -2,7 +2,7 @@
|
||||||
# https://github.com/thomiceli/opengist/blob/master/docs/configuration/index.md
|
# https://github.com/thomiceli/opengist/blob/master/docs/configuration/index.md
|
||||||
# https://github.com/thomiceli/opengist/blob/master/docs/configuration/cheat-sheet.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
|
log-level: warn
|
||||||
|
|
||||||
# Set the log output to one or more of the following: `stdout`, `file`. Default: stdout,file
|
# Set the log output to one or more of the following: `stdout`, `file`. Default: stdout,file
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
| YAML Config Key | Environment Variable | Default value | Description |
|
| 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`. |
|
| 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. |
|
| 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. |
|
| opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. |
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -153,6 +154,21 @@ func InitLog() {
|
||||||
logOutputTypes := utils.RemoveDuplicates[string](
|
logOutputTypes := utils.RemoveDuplicates[string](
|
||||||
strings.Split(strings.ToLower(C.LogOutput), ","),
|
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 {
|
for _, logOutputType := range logOutputTypes {
|
||||||
logOutputType = strings.TrimSpace(logOutputType)
|
logOutputType = strings.TrimSpace(logOutputType)
|
||||||
if !slices.Contains([]string{"stdout", "file"}, logOutputType) {
|
if !slices.Contains([]string{"stdout", "file"}, logOutputType) {
|
||||||
|
@ -162,7 +178,7 @@ func InitLog() {
|
||||||
|
|
||||||
switch logOutputType {
|
switch logOutputType {
|
||||||
case "stdout":
|
case "stdout":
|
||||||
logWriters = append(logWriters, zerolog.NewConsoleWriter())
|
logWriters = append(logWriters, consoleWriter)
|
||||||
defer func() { log.Debug().Msg("Logging to stdout") }()
|
defer func() { log.Debug().Msg("Logging to stdout") }()
|
||||||
case "file":
|
case "file":
|
||||||
file, err := os.OpenFile(filepath.Join(GetHomeDir(), "log", "opengist.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
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 {
|
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") }()
|
defer func() { log.Warn().Msg("No valid log outputs, defaulting to stdout") }()
|
||||||
}
|
}
|
||||||
|
|
||||||
multi := zerolog.MultiLevelWriter(logWriters...)
|
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)
|
log.Warn().Msg("Invalid log level: " + C.LogLevel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,8 +189,8 @@ func NewServer(isDev bool, sessionsPath string) *Server {
|
||||||
e.Pre(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
e.Pre(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
||||||
LogURI: true, LogStatus: true, LogMethod: true,
|
LogURI: true, LogStatus: true, LogMethod: true,
|
||||||
LogValuesFunc: func(ctx echo.Context, v middleware.RequestLoggerValues) error {
|
LogValuesFunc: func(ctx echo.Context, v middleware.RequestLoggerValues) error {
|
||||||
log.Info().Str("URI", v.URI).Int("status", v.Status).Str("method", v.Method).
|
log.Info().Str("uri", v.URI).Int("status", v.Status).Str("method", v.Method).
|
||||||
Str("ip", ctx.RealIP()).
|
Str("ip", ctx.RealIP()).TimeDiff("duration", time.Now(), v.StartTime).
|
||||||
Msg("HTTP")
|
Msg("HTTP")
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -216,10 +216,6 @@ func NewServer(isDev bool, sessionsPath string) *Server {
|
||||||
|
|
||||||
e.HTTPErrorHandler = func(er error, ctx echo.Context) {
|
e.HTTPErrorHandler = func(er error, ctx echo.Context) {
|
||||||
if err, ok := er.(*echo.HTTPError); ok {
|
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)
|
setData(ctx, "error", err)
|
||||||
if errHtml := htmlWithCode(ctx, err.Code, "error.html"); errHtml != nil {
|
if errHtml := htmlWithCode(ctx, err.Code, "error.html"); errHtml != nil {
|
||||||
log.Fatal().Err(errHtml).Send()
|
log.Fatal().Err(errHtml).Send()
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/thomiceli/opengist/internal/config"
|
"github.com/thomiceli/opengist/internal/config"
|
||||||
"github.com/thomiceli/opengist/internal/db"
|
"github.com/thomiceli/opengist/internal/db"
|
||||||
"github.com/thomiceli/opengist/internal/i18n"
|
"github.com/thomiceli/opengist/internal/i18n"
|
||||||
|
@ -58,6 +59,11 @@ func notFound(message string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorRes(code int, message string, err error) 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}
|
return &echo.HTTPError{Code: code, Message: message, Internal: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue