Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6389faf

Browse files
authored
feat: write agent output to a temporary file (#422)
1 parent ccecc2f commit 6389faf

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

internal/cmd/agent.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/url"
55
"os"
66
"os/signal"
7+
"path/filepath"
78
"syscall"
89

910
// We use slog here since agent runs in the background and we can benefit
@@ -48,10 +49,20 @@ coder agent start
4849
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
4950
`,
5051
RunE: func(cmd *cobra.Command, args []string) error {
51-
var (
52-
ctx = cmd.Context()
53-
log = slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
54-
)
52+
ctx := cmd.Context()
53+
sinks := []slog.Sink{
54+
sloghuman.Sink(os.Stderr),
55+
}
56+
57+
file, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-agent.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
58+
if err == nil && file != nil {
59+
sinks = append(sinks, sloghuman.Sink(file))
60+
}
61+
62+
log := slog.Make(sinks...).Leveled(slog.LevelDebug)
63+
if err != nil {
64+
log.Info(ctx, "failed to open agent log file", slog.Error(err))
65+
}
5566
if coderURL == "" {
5667
var ok bool
5768
coderURL, ok = os.LookupEnv("CODER_URL")

0 commit comments

Comments
 (0)