diff --git a/cmd/server/main.go b/cmd/server/main.go index 5a63a26..1805b88 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,4 +1,3 @@ -//nolint:err113 // all errors are shown to the user so it's ok for them to be dynamic package main import ( @@ -14,6 +13,7 @@ import ( "github.com/olexsmir/onasty/internal/config" "github.com/olexsmir/onasty/internal/hasher" "github.com/olexsmir/onasty/internal/jwtutil" + "github.com/olexsmir/onasty/internal/logger" "github.com/olexsmir/onasty/internal/mailer" "github.com/olexsmir/onasty/internal/service/notesrv" "github.com/olexsmir/onasty/internal/service/usersrv" @@ -24,7 +24,6 @@ import ( "github.com/olexsmir/onasty/internal/store/psqlutil" httptransport "github.com/olexsmir/onasty/internal/transport/http" "github.com/olexsmir/onasty/internal/transport/http/httpserver" - "github.com/olexsmir/onasty/internal/transport/http/reqid" ) func main() { @@ -41,12 +40,12 @@ func run(ctx context.Context) error { cfg := config.NewConfig() // logger - baseLoger, err := setupBasicLoggerHandler(cfg) + logger, err := logger.NewCustomLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogShowLine) if err != nil { return err } - slog.SetDefault(slog.New(&CustomLogger{Handler: baseLoger})) + slog.SetDefault(logger) // semi dev mode if !cfg.IsDevMode() { @@ -89,7 +88,7 @@ func run(ctx context.Context) error { go func() { slog.DebugContext(ctx, "starting http server", "port", cfg.ServerPort) if err := srv.Start(); !errors.Is(err, http.ErrServerClosed) { - slog.Error("failed to start http server", "error", err) + slog.ErrorContext(ctx, "failed to start http server", "error", err) } }() @@ -108,46 +107,3 @@ func run(ctx context.Context) error { return nil } - -type CustomLogger struct { - slog.Handler -} - -func (l *CustomLogger) Handle(ctx context.Context, r slog.Record) error { - if requestID := reqid.GetFromContext(ctx); requestID != "" { - r.AddAttrs(slog.String("request_id", requestID)) - } - - return l.Handler.Handle(ctx, r) -} - -func setupBasicLoggerHandler(cfg *config.Config) (slog.Handler, error) { - loggerLevels := map[string]slog.Level{ - "info": slog.LevelInfo, - "debug": slog.LevelDebug, - "error": slog.LevelError, - "warn": slog.LevelWarn, - } - - logLevel, ok := loggerLevels[cfg.LogLevel] - if !ok { - return nil, errors.New("unknown log level") - } - - handlerOptions := &slog.HandlerOptions{ - Level: logLevel, - AddSource: cfg.LogShowLine, - } - - var slogHandler slog.Handler - switch cfg.LogFormat { - case "json": - slogHandler = slog.NewJSONHandler(os.Stdout, handlerOptions) - case "text": - slogHandler = slog.NewTextHandler(os.Stdout, handlerOptions) - default: - return nil, errors.New("unknown log format") - } - - return slogHandler, nil -} diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 845ed55..f42af3f 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -16,6 +16,7 @@ import ( "github.com/olexsmir/onasty/internal/config" "github.com/olexsmir/onasty/internal/hasher" "github.com/olexsmir/onasty/internal/jwtutil" + "github.com/olexsmir/onasty/internal/logger" "github.com/olexsmir/onasty/internal/mailer" "github.com/olexsmir/onasty/internal/service/notesrv" "github.com/olexsmir/onasty/internal/service/usersrv" @@ -76,7 +77,6 @@ func (e *AppTestSuite) SetupSuite() { e.postgresDB = db e.stopPostgres = stop - e.setupLogger() e.initDeps() } @@ -89,6 +89,11 @@ func (e *AppTestSuite) TearDownSuite() { func (e *AppTestSuite) initDeps() { cfg := e.getConfig() + logger, err := logger.NewCustomLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogShowLine) + e.require.NoError(err) + + slog.SetDefault(logger) + e.hasher = hasher.NewSHA256Hasher(cfg.PasswordSalt) e.jwtTokenizer = jwtutil.NewJWTUtil(cfg.JwtSigningKey, time.Hour) e.mailer = mailer.NewTestMailer() @@ -168,13 +173,6 @@ func (e *AppTestSuite) prepPostgres() (*psqlutil.DB, stopDBFunc, error) { return db, stop, driver.Close() } -func (e *AppTestSuite) setupLogger() { - slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ - Level: slog.LevelDebug, - AddSource: os.Getenv("LOG_SHOW_LINE") == "true", - }))) -} - func (e *AppTestSuite) getConfig() *config.Config { return &config.Config{ //nolint:exhaustruct AppEnv: "testing", @@ -185,5 +183,8 @@ func (e *AppTestSuite) getConfig() *config.Config { JwtAccessTokenTTL: time.Hour, JwtRefreshTokenTTL: 24 * time.Hour, VerficationTokenTTL: 24 * time.Hour, + LogShowLine: os.Getenv("LOG_SHOW_LINE") == "true", + LogFormat: "text", + LogLevel: "debug", } } diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..797ec3b --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,54 @@ +package logger + +import ( + "context" + "errors" + "log/slog" + "os" + + "github.com/olexsmir/onasty/internal/transport/http/reqid" +) + +type CustomLogger struct { + slog.Handler +} + +//nolint:err113 +func NewCustomLogger(lvl, format string, showLine bool) (*slog.Logger, error) { + loggerLevels := map[string]slog.Level{ + "info": slog.LevelInfo, + "debug": slog.LevelDebug, + "error": slog.LevelError, + "warn": slog.LevelWarn, + } + + logLevel, ok := loggerLevels[lvl] + if !ok { + return nil, errors.New("unknown log level") + } + + handlerOptions := &slog.HandlerOptions{ + Level: logLevel, + AddSource: showLine, + } + + var slogHandler slog.Handler + switch format { + case "json": + slogHandler = slog.NewJSONHandler(os.Stdout, handlerOptions) + case "text": + slogHandler = slog.NewTextHandler(os.Stdout, handlerOptions) + default: + return nil, errors.New("unknown log format") + } + + return slog.New(&CustomLogger{Handler: slogHandler}), nil +} + +func (l *CustomLogger) Handle(ctx context.Context, r slog.Record) error { + if requestID := reqid.GetFromContext(ctx); requestID != "" { + r.AddAttrs(slog.String("request_id", requestID)) + } + + return l.Handler.Handle(ctx, r) +}