|
4 | 4 | "net/url"
|
5 | 5 | "os"
|
6 | 6 | "os/signal"
|
7 |
| - "path/filepath" |
8 | 7 | "syscall"
|
9 | 8 |
|
10 | 9 | // We use slog here since agent runs in the background and we can benefit
|
@@ -35,34 +34,43 @@ func startCmd() *cobra.Command {
|
35 | 34 | var (
|
36 | 35 | token string
|
37 | 36 | coderURL string
|
| 37 | + logFile string |
38 | 38 | )
|
39 | 39 | cmd := &cobra.Command{
|
40 |
| - Use: "start --coder-url=[coder_url] --token=[token]", |
| 40 | + Use: "start --coder-url=<coder_url> --token=<token> --log-file=<path>", |
41 | 41 | Short: "starts the coder agent",
|
42 | 42 | Long: "starts the coder agent",
|
43 | 43 | Example: `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars
|
44 |
| -
|
45 | 44 | coder agent start
|
46 | 45 |
|
47 | 46 | # start the agent and connect with a specified url and agent token
|
48 |
| -
|
49 | 47 | 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 |
50 | 52 | `,
|
51 | 53 | RunE: func(cmd *cobra.Command, args []string) error {
|
52 | 54 | ctx := cmd.Context()
|
53 |
| - sinks := []slog.Sink{ |
54 |
| - sloghuman.Sink(os.Stderr), |
55 |
| - } |
56 | 55 |
|
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) |
61 | 57 |
|
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 | + } |
65 | 72 | }
|
| 73 | + |
66 | 74 | if coderURL == "" {
|
67 | 75 | var ok bool
|
68 | 76 | coderURL, ok = os.LookupEnv("CODER_URL")
|
@@ -113,6 +121,7 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
|
113 | 121 |
|
114 | 122 | cmd.Flags().StringVar(&token, "token", "", "coder agent token")
|
115 | 123 | cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")
|
| 124 | + cmd.Flags().StringVar(&logFile, "log-file", "", "write a copy of logs to file") |
116 | 125 |
|
117 | 126 | return cmd
|
118 | 127 | }
|
0 commit comments