From 79331dac38f61df861b1746ed7f3e34ff31f9d8e Mon Sep 17 00:00:00 2001 From: Jonathan Yu Date: Thu, 2 Sep 2021 17:36:44 +0000 Subject: [PATCH 1/3] feat: make log file path optional and configurable --- internal/cmd/agent.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/cmd/agent.go b/internal/cmd/agent.go index 24e61874..a3ea299d 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,18 +34,21 @@ 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() @@ -54,15 +56,21 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx 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 { + // 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, it should be fatal, because we asked to write + // a log to a path but cannot write it for some reason + return xerrors.Errorf("open log-file %q: %w", logFile, err) + } + sinks = append(sinks, sloghuman.Sink(file)) } log := slog.Make(sinks...).Leveled(slog.LevelDebug) - if err != nil { - log.Info(ctx, "failed to open agent log file", slog.Error(err)) - } + 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 } From 735f0f313990c249a8f1a5da3c29614ce19ec90b Mon Sep 17 00:00:00 2001 From: Jonathan Yu Date: Thu, 2 Sep 2021 17:40:21 +0000 Subject: [PATCH 2/3] fix error msg --- internal/cmd/agent.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/agent.go b/internal/cmd/agent.go index a3ea299d..87cf0de1 100644 --- a/internal/cmd/agent.go +++ b/internal/cmd/agent.go @@ -63,7 +63,7 @@ coder agent start --log-file=/tmp/coder-agent.log if err != nil { // If an error occurs, it should be fatal, because we asked to write // a log to a path but cannot write it for some reason - return xerrors.Errorf("open log-file %q: %w", logFile, err) + return xerrors.Errorf("open log file: %w", err) } sinks = append(sinks, sloghuman.Sink(file)) From 08b19ceae10998f817416877fb5fb7724b2c5ba3 Mon Sep 17 00:00:00 2001 From: Jonathan Yu Date: Thu, 2 Sep 2021 19:25:36 +0000 Subject: [PATCH 3/3] make failures non-fatal --- internal/cmd/agent.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/cmd/agent.go b/internal/cmd/agent.go index 87cf0de1..9b769c13 100644 --- a/internal/cmd/agent.go +++ b/internal/cmd/agent.go @@ -52,25 +52,25 @@ 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), - } + + log := slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug) // 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, it should be fatal, because we asked to write - // a log to a path but cannot write it for some reason - return xerrors.Errorf("open log file: %w", err) + // 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) } - - sinks = append(sinks, sloghuman.Sink(file)) } - log := slog.Make(sinks...).Leveled(slog.LevelDebug) - if coderURL == "" { var ok bool coderURL, ok = os.LookupEnv("CODER_URL") 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