@@ -1,6 +1,7 @@
4 "encoding/json"
5 "encoding/json"
@@ -14,10 +15,6 @@ import (
14 "github.com/spf13/cobra"
15 "github.com/spf13/cobra"
18− rootCmd.AddCommand(logsCmd)
21var logsCmd = &cobra.Command{
18var logsCmd = &cobra.Command{
22 Use: "logs",
19 Use: "logs",
23 Short: "View crush logs",
20 Short: "View crush logs",
@@ -27,7 +24,16 @@ var logsCmd = &cobra.Command{
27 if err != nil {
24 if err != nil {
28 return fmt.Errorf("failed to get current working directory: %v", err)
25 return fmt.Errorf("failed to get current working directory: %v", err)
28+ follow, err := cmd.Flags().GetBool("follow")
30+ return fmt.Errorf("failed to get follow flag: %v", err)
30 log.SetLevel(log.DebugLevel)
33 log.SetLevel(log.DebugLevel)
34+ // Configure log to output to stdout instead of stderr
35+ log.SetOutput(os.Stdout)
31 cfg, err := config.Load(cwd, false)
37 cfg, err := config.Load(cwd, false)
32 if err != nil {
38 if err != nil {
33 return fmt.Errorf("failed to load configuration: %v", err)
39 return fmt.Errorf("failed to load configuration: %v", err)
@@ -38,62 +44,92 @@ var logsCmd = &cobra.Command{
38 log.Warn("Looks like you are not in a crush project. No logs found.")
44 log.Warn("Looks like you are not in a crush project. No logs found.")
39 return nil
45 return nil
41− t, err := tail.TailFile(logsFile, tail.Config{Follow: true, ReOpen: true, Logger: tail.DiscardingLogger})
43− return fmt.Errorf("failed to tail log file: %v", err)
49+ // Follow mode - tail the file continuously
50+ t, err := tail.TailFile(logsFile, tail.Config{Follow: true, ReOpen: true, Logger: tail.DiscardingLogger})
52+ return fmt.Errorf("failed to tail log file: %v", err)
46− // Print the text of each received line
55+ // Print the text of each received line
47− for line := range t.Lines {
56+ for line := range t.Lines {
48− var data map[string]any
57+ printLogLine(line.Text)
49− if err := json.Unmarshal([]byte(line.Text), &data); err != nil {
52− msg := data["msg"]
59+ } else {
53− level := data["level"]
60+ // Oneshot mode - read the entire file once
54− otherData := []any{}
61+ file, err := os.Open(logsFile)
55− keys := []string{}
62+ if err != nil {
56− for k := range data {
63+ return fmt.Errorf("failed to open log file: %v", err)
57− keys = append(keys, k)
59− slices.Sort(keys)
65+ defer file.Close()
60− for _, k := range keys {
62− case "msg", "level", "time":
65− source, ok := data[k].(map[string]any)
69− sourceFile := fmt.Sprintf("%s:%d", source["file"], int(source["line"].(float64)))
70− otherData = append(otherData, "source", sourceFile)
72− default:
67+ scanner := bufio.NewScanner(file)
73− otherData = append(otherData, k, data[k])
68+ for scanner.Scan() {
74− }
69+ printLogLine(scanner.Text())
76− log.SetTimeFunction(func(_ time.Time) time.Time {
71+
77− // parse the timestamp from the log line if available
78− t, err := time.Parse(time.RFC3339, data["time"].(string))
79− if err != nil {
72+ if err := scanner.Err(); err != nil {
80− return time.Now() // fallback to current time if parsing fails
73+ return fmt.Errorf("failed to read log file: %v", err)
86− log.Info(msg, otherData...)
88− log.Debug(msg, otherData...)
90− log.Error(msg, otherData...)
92− log.Warn(msg, otherData...)
94− log.Info(msg, otherData...)
97 return nil
77 return nil
82+ logsCmd.Flags().BoolP("follow", "f", false, "Follow log output")
83+ rootCmd.AddCommand(logsCmd)
86+func printLogLine(lineText string) {
87+ var data map[string]any
88+ if err := json.Unmarshal([]byte(lineText), &data); err != nil {
92+ level := data["level"]
95+ for k := range data {
96+ keys = append(keys, k)
99+ for _, k := range keys {
101+ case "msg", "level", "time":
104+ source, ok := data[k].(map[string]any)
108+ sourceFile := fmt.Sprintf("%s:%d", source["file"], int(source["line"].(float64)))
109+ otherData = append(otherData, "source", sourceFile)
112+ otherData = append(otherData, k, data[k])
115+ log.SetTimeFunction(func(_ time.Time) time.Time {
116+ // parse the timestamp from the log line if available
117+ t, err := time.Parse(time.RFC3339, data["time"].(string))
119+ return time.Now() // fallback to current time if parsing fails
125+ log.Info(msg, otherData...)
127+ log.Debug(msg, otherData...)
129+ log.Error(msg, otherData...)
131+ log.Warn(msg, otherData...)
133+ log.Info(msg, otherData...)