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

feat: write agent output to a temporary file #422

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions internal/cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"os"
"os/signal"
"path/filepath"
"syscall"

// We use slog here since agent runs in the background and we can benefit
Expand Down Expand Up @@ -48,10 +49,20 @@ coder agent start
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
`,
RunE: func(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
log = slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
)
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it didn't seem like the file we write needs to be hidden, so I made it a not-dotfile

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be consistent for the other log files in that case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which other log files do we have? I think just a code-server one right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code-server and envagent, they're in environment_stages.go

if err == nil && file != nil {
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an error log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, but I don't expect it to happen for coder workspaces and everything would be logged to our console anyways

}
if coderURL == "" {
var ok bool
coderURL, ok = os.LookupEnv("CODER_URL")
Expand Down