diff --git a/internal/cmd/agent.go b/internal/cmd/agent.go index 24e61874..9b769c13 100644 --- a/internal/cmd/agent.go +++ b/internal/cmd/agent.go @@ -4,7 +4,6 @@ import ( "net/url" "os" "os/signal" - "path/filepath" "syscall" // We use slog here since agent runs in the background and we can benefit @@ -35,34 +34,43 @@ func startCmd() *cobra.Command { var ( token string coderURL string + logFile string ) cmd := &cobra.Command{ - Use: "start --coder-url=[coder_url] --token=[token]", + Use: "start --coder-url= --token= --log-file=", Short: "starts the coder agent", Long: "starts the coder agent", Example: `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars - coder agent start # start the agent and connect with a specified url and agent token - coder agent start --coder-url https://my-coder.com --token xxxx-xxxx + +# start the agent and write a copy of the log to /tmp/coder-agent.log +# if the file already exists, it will be truncated +coder agent start --log-file=/tmp/coder-agent.log `, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - sinks := []slog.Sink{ - sloghuman.Sink(os.Stderr), - } - file, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-agent.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err == nil && file != nil { - sinks = append(sinks, sloghuman.Sink(file)) - } + log := slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug) - log := slog.Make(sinks...).Leveled(slog.LevelDebug) - if err != nil { - log.Info(ctx, "failed to open agent log file", slog.Error(err)) + // Optional log file path to write + if logFile != "" { + // Truncate the file if it already exists + file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + // If an error occurs, log it as an error, but consider it non-fatal + log.Warn(ctx, "failed to open log file", slog.Error(err)) + } else { + // Log to both standard output and our file + log = slog.Make( + sloghuman.Sink(os.Stderr), + sloghuman.Sink(file), + ).Leveled(slog.LevelDebug) + } } + if coderURL == "" { var ok bool coderURL, ok = os.LookupEnv("CODER_URL") @@ -113,6 +121,7 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx cmd.Flags().StringVar(&token, "token", "", "coder agent token") cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url") + cmd.Flags().StringVar(&logFile, "log-file", "", "write a copy of logs to file") return cmd } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy