From 0958c3b864b337c30563a9a30cca923c119e7748 Mon Sep 17 00:00:00 2001 From: Stavros Date: Fri, 8 May 2026 17:22:21 +0300 Subject: [PATCH] refactor: rework cli logging --- cmd/tinyauth/create_user.go | 9 +++++---- cmd/tinyauth/generate_totp.go | 11 ++++++----- cmd/tinyauth/healthcheck.go | 9 +++++---- cmd/tinyauth/verify_user.go | 11 ++++++----- internal/bootstrap/app_bootstrap.go | 19 ++++++++++--------- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/cmd/tinyauth/create_user.go b/cmd/tinyauth/create_user.go index ef5fe266..d7e9f97e 100644 --- a/cmd/tinyauth/create_user.go +++ b/cmd/tinyauth/create_user.go @@ -6,8 +6,8 @@ import ( "strings" "charm.land/huh/v2" - "github.com/tinyauthapp/tinyauth/internal/utils/tlog" "github.com/tinyauthapp/paerser/cli" + "github.com/tinyauthapp/tinyauth/internal/utils/logger" "golang.org/x/crypto/bcrypt" ) @@ -40,7 +40,8 @@ func createUserCmd() *cli.Command { Configuration: tCfg, Resources: loaders, Run: func(_ []string) error { - tlog.NewSimpleLogger().Init() + log := logger.NewLogger().WithSimpleConfig() + log.Init() if tCfg.Interactive { form := huh.NewForm( @@ -73,7 +74,7 @@ func createUserCmd() *cli.Command { return errors.New("username and password cannot be empty") } - tlog.App.Info().Str("username", tCfg.Username).Msg("Creating user") + log.App.Info().Str("username", tCfg.Username).Msg("Creating user") passwd, err := bcrypt.GenerateFromPassword([]byte(tCfg.Password), bcrypt.DefaultCost) if err != nil { @@ -86,7 +87,7 @@ func createUserCmd() *cli.Command { passwdStr = strings.ReplaceAll(passwdStr, "$", "$$") } - tlog.App.Info().Str("user", fmt.Sprintf("%s:%s", tCfg.Username, passwdStr)).Msg("User created") + log.App.Info().Str("user", fmt.Sprintf("%s:%s", tCfg.Username, passwdStr)).Msg("User created") return nil }, diff --git a/cmd/tinyauth/generate_totp.go b/cmd/tinyauth/generate_totp.go index 8819922e..8492f87b 100644 --- a/cmd/tinyauth/generate_totp.go +++ b/cmd/tinyauth/generate_totp.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/tinyauthapp/tinyauth/internal/utils" - "github.com/tinyauthapp/tinyauth/internal/utils/tlog" + "github.com/tinyauthapp/tinyauth/internal/utils/logger" "charm.land/huh/v2" "github.com/mdp/qrterminal/v3" @@ -40,7 +40,8 @@ func generateTotpCmd() *cli.Command { Configuration: tCfg, Resources: loaders, Run: func(_ []string) error { - tlog.NewSimpleLogger().Init() + log := logger.NewLogger().WithSimpleConfig() + log.Init() if tCfg.Interactive { form := huh.NewForm( @@ -88,9 +89,9 @@ func generateTotpCmd() *cli.Command { secret := key.Secret() - tlog.App.Info().Str("secret", secret).Msg("Generated TOTP secret") + log.App.Info().Str("secret", secret).Msg("Generated TOTP secret") - tlog.App.Info().Msg("Generated QR code") + log.App.Info().Msg("Generated QR code") config := qrterminal.Config{ Level: qrterminal.L, @@ -109,7 +110,7 @@ func generateTotpCmd() *cli.Command { user.Password = strings.ReplaceAll(user.Password, "$", "$$") } - tlog.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TOTPSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.") + log.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TOTPSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.") return nil }, diff --git a/cmd/tinyauth/healthcheck.go b/cmd/tinyauth/healthcheck.go index 649a68c7..921479a5 100644 --- a/cmd/tinyauth/healthcheck.go +++ b/cmd/tinyauth/healthcheck.go @@ -9,8 +9,8 @@ import ( "os" "time" - "github.com/tinyauthapp/tinyauth/internal/utils/tlog" "github.com/tinyauthapp/paerser/cli" + "github.com/tinyauthapp/tinyauth/internal/utils/logger" ) type healthzResponse struct { @@ -26,7 +26,8 @@ func healthcheckCmd() *cli.Command { Resources: nil, AllowArg: true, Run: func(args []string) error { - tlog.NewSimpleLogger().Init() + log := logger.NewLogger().WithSimpleConfig() + log.Init() srvAddr := os.Getenv("TINYAUTH_SERVER_ADDRESS") if srvAddr == "" { @@ -48,7 +49,7 @@ func healthcheckCmd() *cli.Command { return errors.New("Could not determine app URL") } - tlog.App.Info().Str("app_url", appUrl).Msg("Performing health check") + log.App.Info().Str("app_url", appUrl).Msg("Performing health check") client := http.Client{ Timeout: 30 * time.Second, @@ -86,7 +87,7 @@ func healthcheckCmd() *cli.Command { return fmt.Errorf("failed to decode response: %w", err) } - tlog.App.Info().Interface("response", healthResp).Msg("Tinyauth is healthy") + log.App.Info().Interface("response", healthResp).Msg("Tinyauth is healthy") return nil }, diff --git a/cmd/tinyauth/verify_user.go b/cmd/tinyauth/verify_user.go index 5058b606..b0347f6f 100644 --- a/cmd/tinyauth/verify_user.go +++ b/cmd/tinyauth/verify_user.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/tinyauthapp/tinyauth/internal/utils" - "github.com/tinyauthapp/tinyauth/internal/utils/tlog" + "github.com/tinyauthapp/tinyauth/internal/utils/logger" "charm.land/huh/v2" "github.com/pquerna/otp/totp" @@ -44,7 +44,8 @@ func verifyUserCmd() *cli.Command { Configuration: tCfg, Resources: loaders, Run: func(_ []string) error { - tlog.NewSimpleLogger().Init() + log := logger.NewLogger().WithSimpleConfig() + log.Init() if tCfg.Interactive { form := huh.NewForm( @@ -97,9 +98,9 @@ func verifyUserCmd() *cli.Command { if user.TOTPSecret == "" { if tCfg.Totp != "" { - tlog.App.Warn().Msg("User does not have TOTP secret") + log.App.Warn().Msg("User does not have TOTP secret") } - tlog.App.Info().Msg("User verified") + log.App.Info().Msg("User verified") return nil } @@ -109,7 +110,7 @@ func verifyUserCmd() *cli.Command { return fmt.Errorf("TOTP code incorrect") } - tlog.App.Info().Msg("User verified") + log.App.Info().Msg("User verified") return nil }, diff --git a/internal/bootstrap/app_bootstrap.go b/internal/bootstrap/app_bootstrap.go index 268d0d30..92a89b39 100644 --- a/internal/bootstrap/app_bootstrap.go +++ b/internal/bootstrap/app_bootstrap.go @@ -249,17 +249,18 @@ func (app *BootstrapApp) Setup() error { }() // monitor cancellation and server errors - select { - case <-app.ctx.Done(): - app.log.App.Debug().Msg("Shutting down application") - return nil - case err := <-errChan: - if err != nil { - return fmt.Errorf("server error: %w", err) + for { + select { + case <-app.ctx.Done(): + app.log.App.Debug().Msg("Oh, seems like I got to shutdown, bye!") + app.db.Close() + return nil + case err := <-errChan: + if err != nil { + return fmt.Errorf("server error: %w", err) + } } } - - return nil } func (app *BootstrapApp) serveHTTP() error {