fix: Write agent logs to "/tmp/coder-agent.log" for debugging (#1239)

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!
This commit is contained in:
Kyle Carberry
2022-05-02 11:36:51 -05:00
committed by GitHub
parent c2b5009208
commit e531c0930c
4 changed files with 17 additions and 12 deletions

View File

@ -4,6 +4,8 @@ import (
"context"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"cloud.google.com/go/compute/metadata"
@ -17,6 +19,8 @@ import (
"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/codersdk"
"github.com/coder/retry"
"gopkg.in/natefinch/lumberjack.v2"
)
func workspaceAgent() *cobra.Command {
@ -36,7 +40,13 @@ func workspaceAgent() *cobra.Command {
if err != nil {
return xerrors.Errorf("parse %q: %w", rawURL, err)
}
logger := slog.Make(sloghuman.Sink(cmd.OutOrStdout())).Leveled(slog.LevelDebug)
logWriter := &lumberjack.Logger{
Filename: filepath.Join(os.TempDir(), "coder-agent.log"),
MaxSize: 5, // MB
}
defer logWriter.Close()
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
client := codersdk.New(coderURL)
// exchangeToken returns a session token.