Skip to content

Commit 9379e0f

Browse files
committed
feat(cmd): integrate structured logging throughout application
- add `--log` flag to specify custom log file path - initialize logger at application startup with error handling - add comprehensive logging to presentation loading and parsing - add logging to file watcher and live reload functionality - add error logging for config loading and TUI failures - log application lifecycle events (start, end, reload)
1 parent 71a07ab commit 9379e0f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cmd/root.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ import (
1212
"github.com/spf13/cobra"
1313

1414
"github.com/museslabs/kyma/internal/config"
15+
"github.com/museslabs/kyma/internal/logger"
1516
"github.com/museslabs/kyma/internal/tui"
1617
"github.com/museslabs/kyma/internal/tui/transitions"
1718
)
1819

1920
var (
2021
static bool
2122
configPath string
23+
logPath string
2224
)
2325

2426
func init() {
2527
rootCmd.Flags().BoolVarP(&static, "static", "s", false, "Disable live reload (watch mode is enabled by default)")
2628
rootCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to config file")
29+
rootCmd.Flags().StringVarP(&logPath, "log", "l", "", "Path to log file (default: ~/.config/kyma/logs/<timestamp>.kyma.log)")
2730
rootCmd.AddCommand(versionCmd)
2831
}
2932

@@ -43,27 +46,41 @@ var rootCmd = &cobra.Command{
4346
DisableDefaultCmd: true,
4447
},
4548
RunE: func(cmd *cobra.Command, args []string) error {
49+
if err := logger.Load(logPath); err != nil {
50+
return fmt.Errorf("failed to initialize logger: %w", err)
51+
}
52+
53+
logger.Info("Starting Kyma")
54+
4655
if err := config.Load(configPath); err != nil {
56+
logger.Error("Failed to load config", "error", err, "config_path", configPath)
4757
return err
4858
}
4959

5060
filename := args[0]
61+
logger.Info("Loading presentation", "filename", filename)
5162

5263
data, err := os.ReadFile(filename)
5364
if err != nil {
65+
logger.Error("Failed to read presentation file", "error", err, "filename", filename)
5466
return err
5567
}
5668

5769
root, err := parseSlides(string(data))
5870
if err != nil {
71+
logger.Error("Failed to parse slides", "error", err, "filename", filename)
5972
return err
6073
}
6174

75+
logger.Info("Successfully parsed presentation")
76+
6277
p := tea.NewProgram(tui.New(root), tea.WithAltScreen(), tea.WithMouseAllMotion())
6378

6479
if !static {
80+
logger.Info("Starting file watcher for live reload")
6581
watcher, err := fsnotify.NewWatcher()
6682
if err != nil {
83+
logger.Error("Failed to create file watcher", "error", err)
6784
p.Send(tui.UpdateSlidesMsg{NewRoot: createErrorSlide(err, "none")})
6885
return nil
6986
}
@@ -97,10 +114,13 @@ var rootCmd = &cobra.Command{
97114
go watchFileChanges(watcher, p, filename, absPath, configPath)
98115
}
99116

117+
logger.Info("Starting TUI program")
100118
if _, err := p.Run(); err != nil {
119+
logger.Error("TUI program failed", "error", err)
101120
return err
102121
}
103122

123+
logger.Info("Kyma session ended")
104124
return nil
105125
},
106126
}
@@ -130,23 +150,29 @@ func watchFileChanges(
130150
debounceTimer.Stop()
131151
}
132152
debounceTimer = time.AfterFunc(100*time.Millisecond, func() {
153+
logger.Info("File changed, reloading presentation", "file", event.Name)
154+
133155
data, err := os.ReadFile(filename)
134156
if err != nil {
157+
logger.Error("Failed to read file during reload", "error", err, "filename", filename)
135158
p.Send(tui.UpdateSlidesMsg{NewRoot: createErrorSlide(err, "none")})
136159
return
137160
}
138161

139162
if err := config.Load(configPath); err != nil {
163+
logger.Error("Failed to reload config", "error", err, "config_path", configPath)
140164
p.Send(tui.UpdateSlidesMsg{NewRoot: createErrorSlide(err, "none")})
141165
return
142166
}
143167

144168
newRoot, err := parseSlides(string(data))
145169
if err != nil {
170+
logger.Error("Failed to parse slides during reload", "error", err, "filename", filename)
146171
p.Send(tui.UpdateSlidesMsg{NewRoot: createErrorSlide(err, "none")})
147172
return
148173
}
149174

175+
logger.Info("Successfully reloaded presentation")
150176
p.Send(tui.UpdateSlidesMsg{NewRoot: newRoot})
151177
})
152178
}
@@ -162,6 +188,7 @@ func watchFileChanges(
162188

163189
func Execute() {
164190
if err := rootCmd.Execute(); err != nil {
191+
logger.Error("Application failed", "error", err)
165192
fmt.Fprintln(os.Stderr, err)
166193
os.Exit(1)
167194
}

0 commit comments

Comments
 (0)