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

Commit 1f35561

Browse files
authored
feat: make log file path optional and configurable (#423)
1 parent 6389faf commit 1f35561

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

internal/cmd/agent.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/url"
55
"os"
66
"os/signal"
7-
"path/filepath"
87
"syscall"
98

109
// We use slog here since agent runs in the background and we can benefit
@@ -35,34 +34,43 @@ func startCmd() *cobra.Command {
3534
var (
3635
token string
3736
coderURL string
37+
logFile string
3838
)
3939
cmd := &cobra.Command{
40-
Use: "start --coder-url=[coder_url] --token=[token]",
40+
Use: "start --coder-url=<coder_url> --token=<token> --log-file=<path>",
4141
Short: "starts the coder agent",
4242
Long: "starts the coder agent",
4343
Example: `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars
44-
4544
coder agent start
4645
4746
# start the agent and connect with a specified url and agent token
48-
4947
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
48+
49+
# start the agent and write a copy of the log to /tmp/coder-agent.log
50+
# if the file already exists, it will be truncated
51+
coder agent start --log-file=/tmp/coder-agent.log
5052
`,
5153
RunE: func(cmd *cobra.Command, args []string) error {
5254
ctx := cmd.Context()
53-
sinks := []slog.Sink{
54-
sloghuman.Sink(os.Stderr),
55-
}
5655

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-
}
56+
log := slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
6157

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))
58+
// Optional log file path to write
59+
if logFile != "" {
60+
// Truncate the file if it already exists
61+
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
62+
if err != nil {
63+
// If an error occurs, log it as an error, but consider it non-fatal
64+
log.Warn(ctx, "failed to open log file", slog.Error(err))
65+
} else {
66+
// Log to both standard output and our file
67+
log = slog.Make(
68+
sloghuman.Sink(os.Stderr),
69+
sloghuman.Sink(file),
70+
).Leveled(slog.LevelDebug)
71+
}
6572
}
73+
6674
if coderURL == "" {
6775
var ok bool
6876
coderURL, ok = os.LookupEnv("CODER_URL")
@@ -113,6 +121,7 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
113121

114122
cmd.Flags().StringVar(&token, "token", "", "coder agent token")
115123
cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")
124+
cmd.Flags().StringVar(&logFile, "log-file", "", "write a copy of logs to file")
116125

117126
return cmd
118127
}

0 commit comments

Comments
 (0)