on main help sign in
fix: setup logging in server and client commands
65974bbAyman Bagabas
internal/cmd/root.go+6
@@ -24,6 +24,7 @@ import (
24 "github.com/charmbracelet/crush/internal/client"
24 "github.com/charmbracelet/crush/internal/client"
25 "github.com/charmbracelet/crush/internal/config"
25 "github.com/charmbracelet/crush/internal/config"
26 "github.com/charmbracelet/crush/internal/event"
26 "github.com/charmbracelet/crush/internal/event"
27+ crushlog "github.com/charmbracelet/crush/internal/log"
27 "github.com/charmbracelet/crush/internal/proto"
28 "github.com/charmbracelet/crush/internal/proto"
28 "github.com/charmbracelet/crush/internal/server"
29 "github.com/charmbracelet/crush/internal/server"
29 "github.com/charmbracelet/crush/internal/ui/common"
30 "github.com/charmbracelet/crush/internal/ui/common"
@@ -235,6 +236,11 @@ func connectToServer(cmd *cobra.Command) (*client.Client, *proto.Workspace, func
235 event.Init()
236 event.Init()
236 }
237 }
237
238
239+ if ws.Config != nil {
240+ logFile := filepath.Join(ws.Config.Options.DataDirectory, "logs", "crush.log")
241+ crushlog.Setup(logFile, debug)
242+ }
243+
238 cleanup := func() { _ = c.DeleteWorkspace(context.Background(), ws.ID) }
244 cleanup := func() { _ = c.DeleteWorkspace(context.Background(), ws.ID) }
239 return c, ws, cleanup, nil
245 return c, ws, cleanup, nil
240}
246}
internal/cmd/server.go+4−9
@@ -7,9 +7,11 @@ import (
7 "log/slog"
7 "log/slog"
8 "os"
8 "os"
9 "os/signal"
9 "os/signal"
10+ "path/filepath"
10 "time"
11 "time"
11
12
12 "github.com/charmbracelet/crush/internal/config"
13 "github.com/charmbracelet/crush/internal/config"
14+ crushlog "github.com/charmbracelet/crush/internal/log"
13 "github.com/charmbracelet/crush/internal/server"
15 "github.com/charmbracelet/crush/internal/server"
14 "github.com/spf13/cobra"
16 "github.com/spf13/cobra"
15)
17)
@@ -39,15 +41,8 @@ var serverCmd = &cobra.Command{
39 return fmt.Errorf("failed to load configuration: %v", err)
41 return fmt.Errorf("failed to load configuration: %v", err)
40 }
42 }
41
43
42− handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
44+ logFile := filepath.Join(config.GlobalCacheDir(), "server-"+safeNameRegexp.ReplaceAllString(serverHost, "_"), "crush.log")
43− Level: slog.LevelInfo,
45+ crushlog.Setup(logFile, debug)
44− })
45− if debug {
46− handler = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
47− Level: slog.LevelDebug,
48− })
49− }
50− slog.SetDefault(slog.New(handler))
51
46
52 hostURL, err := server.ParseHostURL(serverHost)
47 hostURL, err := server.ParseHostURL(serverHost)
53 if err != nil {
48 if err != nil {
internal/config/load.go−7
@@ -22,7 +22,6 @@ import (
22 "github.com/charmbracelet/crush/internal/env"
22 "github.com/charmbracelet/crush/internal/env"
23 "github.com/charmbracelet/crush/internal/fsext"
23 "github.com/charmbracelet/crush/internal/fsext"
24 "github.com/charmbracelet/crush/internal/home"
24 "github.com/charmbracelet/crush/internal/home"
25− "github.com/charmbracelet/crush/internal/log"
26 powernapConfig "github.com/charmbracelet/x/powernap/pkg/config"
25 powernapConfig "github.com/charmbracelet/x/powernap/pkg/config"
27 "github.com/qjebbs/go-jsons"
26 "github.com/qjebbs/go-jsons"
28)
27)
@@ -52,12 +51,6 @@ func Load(workingDir, dataDir string, debug bool) (*ConfigStore, error) {
52 cfg.Options.Debug = true
51 cfg.Options.Debug = true
53 }
52 }
54
53
55− // Setup logs
56− log.Setup(
57− filepath.Join(cfg.Options.DataDirectory, "logs", fmt.Sprintf("%s.log", appName)),
58− cfg.Options.Debug,
59− )
60−
61 // Load workspace config last so it has highest priority.
54 // Load workspace config last so it has highest priority.
62 if wsData, err := os.ReadFile(store.workspacePath); err == nil && len(wsData) > 0 {
55 if wsData, err := os.ReadFile(store.workspacePath); err == nil && len(wsData) > 0 {
63 merged, mergeErr := loadFromBytes(append([][]byte{mustMarshalConfig(cfg)}, wsData))
56 merged, mergeErr := loadFromBytes(append([][]byte{mustMarshalConfig(cfg)}, wsData))