Skip to content

Commit a4be45f

Browse files
committed
fix: Write agent logs to "/tmp/coder-agent.log" for debugging
It was difficult to obtain logs for the agent if it failed to start for some reason. Now they'll go to a consistent spot!
1 parent eb60692 commit a4be45f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

agent/agent.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/exec"
1414
"os/user"
15+
"path/filepath"
1516
"runtime"
1617
"strconv"
1718
"strings"
@@ -21,7 +22,6 @@ import (
2122
"github.com/armon/circbuf"
2223
"github.com/google/uuid"
2324

24-
gsyslog "github.com/hashicorp/go-syslog"
2525
"go.uber.org/atomic"
2626

2727
"cdr.dev/slog"
@@ -167,15 +167,9 @@ func (*agent) runStartupScript(ctx context.Context, script string) error {
167167
return xerrors.Errorf("get user shell: %w", err)
168168
}
169169

170-
var writer io.WriteCloser
171-
// Attempt to use the syslog to write startup information.
172-
writer, err = gsyslog.NewLogger(gsyslog.LOG_INFO, "USER", "coder-startup-script")
170+
writer, err := os.OpenFile(filepath.Join(os.TempDir(), "coder-startup-script.log"), os.O_CREATE|os.O_RDWR, 0600)
173171
if err != nil {
174-
// If the syslog isn't supported or cannot be created, use a text file in temp.
175-
writer, err = os.CreateTemp("", "coder-startup-script.txt")
176-
if err != nil {
177-
return xerrors.Errorf("open startup script log file: %w", err)
178-
}
172+
return xerrors.Errorf("open startup script log file: %w", err)
179173
}
180174
defer func() {
181175
_ = writer.Close()

cli/agent.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"net/http"
66
"net/url"
7+
"os"
8+
"path/filepath"
79
"time"
810

911
"cloud.google.com/go/compute/metadata"
@@ -37,7 +39,15 @@ func workspaceAgent() *cobra.Command {
3739
if err != nil {
3840
return xerrors.Errorf("parse %q: %w", rawURL, err)
3941
}
40-
logger := slog.Make(sloghuman.Sink(cmd.OutOrStdout())).Leveled(slog.LevelDebug)
42+
43+
logPath := filepath.Join(os.TempDir(), "coder-agent.log")
44+
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_RDWR, 0600)
45+
if err != nil {
46+
return xerrors.Errorf("open log %q: %w", logPath, err)
47+
}
48+
defer logFile.Close()
49+
50+
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logFile)).Leveled(slog.LevelDebug)
4151
client := codersdk.New(coderURL)
4252

4353
// exchangeToken returns a session token.

0 commit comments

Comments
 (0)