13 "github.com/charmbracelet/colorprofile"
14 "github.com/charmbracelet/crush/internal/config"
15 "github.com/charmbracelet/log/v2"
16 "github.com/charmbracelet/x/term"
17 "github.com/nxadm/tail"
18 "github.com/spf13/cobra"
21const defaultTailLines = 1000
23var logsCmd = &cobra.Command{
25 Short: "View crush logs",
26 Long: `View the logs generated by Crush. This command allows you to see the log output for debugging and monitoring.`,
27 RunE: func(cmd *cobra.Command, args []string) error {
28 cwd, err := cmd.Flags().GetString("cwd")
30 return fmt.Errorf("failed to get current working directory: %v", err)
33 dataDir, err := cmd.Flags().GetString("data-dir")
35 return fmt.Errorf("failed to get data directory: %v", err)
38 follow, err := cmd.Flags().GetBool("follow")
40 return fmt.Errorf("failed to get follow flag: %v", err)
43 tailLines, err := cmd.Flags().GetInt("tail")
45 return fmt.Errorf("failed to get tail flag: %v", err)
48 log.SetLevel(log.DebugLevel)
49 log.SetOutput(os.Stdout)
50 if !term.IsTerminal(os.Stdout.Fd()) {
51 log.SetColorProfile(colorprofile.NoTTY)
54 cfg, err := config.Load(cwd, dataDir, false)
56 return fmt.Errorf("failed to load configuration: %v", err)
58 logsFile := filepath.Join(cfg.Options.DataDirectory, "logs", "crush.log")
59 _, err = os.Stat(logsFile)
60 if os.IsNotExist(err) {
61 log.Warn("Looks like you are not in a crush project. No logs found.")
66 return followLogs(cmd.Context(), logsFile, tailLines)
69 return showLogs(logsFile, tailLines)
74 logsCmd.Flags().BoolP("follow", "f", false, "Follow log output")
75 logsCmd.Flags().IntP("tail", "t", defaultTailLines, "Show only the last N lines default: 1000 for performance")
78func followLogs(ctx context.Context, logsFile string, tailLines int) error {
79 t, err := tail.TailFile(logsFile, tail.Config{
82 Logger: tail.DiscardingLogger,
85 return fmt.Errorf("failed to tail log file: %v", err)
89 for line := range t.Lines {
93 lines = append(lines, line.Text)
94 if len(lines) > tailLines {
95 lines = lines[len(lines)-tailLines:]
100 for _, line := range lines {
104 if len(lines) == tailLines {
105 fmt.Fprintf(os.Stderr, "\nShowing last %d lines. Full logs available at: %s\n", tailLines, logsFile)
106 fmt.Fprintf(os.Stderr, "Following new log entries...\n\n")
109 t, err = tail.TailFile(logsFile, tail.Config{
112 Logger: tail.DiscardingLogger,
113 Location: &tail.SeekInfo{Offset: 0, Whence: io.SeekEnd},
116 return fmt.Errorf("failed to tail log file: %v", err)
122 case line := <-t.Lines:
126 printLogLine(line.Text)
133func showLogs(logsFile string, tailLines int) error {
134 t, err := tail.TailFile(logsFile, tail.Config{
137 Logger: tail.DiscardingLogger,
141 return fmt.Errorf("failed to tail log file: %v", err)
146 for line := range t.Lines {
150 lines = append(lines, line.Text)
151 if len(lines) > tailLines {
152 lines = lines[len(lines)-tailLines:]
156 for _, line := range lines {
160 if len(lines) == tailLines {
161 fmt.Fprintf(os.Stderr, "\nShowing last %d lines. Full logs available at: %s\n", tailLines, logsFile)
167func printLogLine(lineText string) {
168 var data map[string]any
169 if err := json.Unmarshal([]byte(lineText), &data); err != nil {
173 level := data["level"]
176 for k := range data {
177 keys = append(keys, k)
180 for _, k := range keys {
182 case "msg", "level", "time":
185 source, ok := data[k].(map[string]any)
189 sourceFile := fmt.Sprintf("%s:%d", source["file"], int(source["line"].(float64)))
190 otherData = append(otherData, "source", sourceFile)
193 otherData = append(otherData, k, data[k])
196 log.SetTimeFunction(func(_ time.Time) time.Time {
197 // parse the timestamp from the log line if available
198 t, err := time.Parse(time.RFC3339, data["time"].(string))
200 return time.Now() // fallback to current time if parsing fails
206 log.Info(msg, otherData...)
208 log.Debug(msg, otherData...)
210 log.Error(msg, otherData...)
212 log.Warn(msg, otherData...)
214 log.Info(msg, otherData...)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list