diff --git a/cmd/root.go b/cmd/root.go index 8437937..6a19332 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,11 +19,23 @@ var ( var ( rootCmd = &cobra.Command{ - RunE: func(cmd *cobra.Command, args []string) error { - if err := logger.Load(); err != nil { - return err + PreRunE: func(cmd *cobra.Command, args []string) error { + var level slog.Level + switch str, _ := cmd.Flags().GetString("log-level"); str { + case "debug": + level = slog.LevelDebug + case "info": + level = slog.LevelInfo + case "warn": + level = slog.LevelWarn + case "error": + level = slog.LevelError } + return logger.Load(level) + }, + + RunE: func(cmd *cobra.Command, args []string) error { token, _ := cmd.Flags().GetString("token") if token == "" { var err error @@ -63,5 +75,7 @@ var ( ) func init() { - rootCmd.Flags().StringP("token", "t", "", "the authentication token") + flags := rootCmd.Flags() + flags.StringP("token", "t", "", "the authentication token") + flags.StringP("log-level", "l", "info", "log level") } diff --git a/cmd/state.go b/cmd/state.go index 1552a32..dfe5925 100644 --- a/cmd/state.go +++ b/cmd/state.go @@ -49,7 +49,7 @@ func openState(token string) error { func (s *State) onRequest(r httpdriver.Request) error { req, ok := r.(*httpdriver.DefaultRequest) if ok { - slog.Info("new HTTP request", "method", req.Method, "path", req.URL.Path) + slog.Debug("new HTTP request", "method", req.Method, "url", req.URL, "headers", req.Header) } return nil diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 3f5ab07..2cb743c 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -11,7 +11,7 @@ import ( const fileName = "logs.txt" // Opens the log file and configures default logger. -func Load() error { +func Load(level slog.Level) error { path, err := os.UserCacheDir() if err != nil { return err @@ -28,7 +28,7 @@ func Load() error { return err } - l := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{AddSource: true})) + l := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{AddSource: true, Level: level})) slog.SetDefault(l) return nil }