Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit1f35561

Browse files
authored
feat: make log file path optional and configurable (#423)
1 parent6389faf commit1f35561

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

‎internal/cmd/agent.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/url"
55
"os"
66
"os/signal"
7-
"path/filepath"
87
"syscall"
98

109
// We use slog here since agent runs in the background and we can benefit
@@ -35,34 +34,43 @@ func startCmd() *cobra.Command {
3534
var (
3635
tokenstring
3736
coderURLstring
37+
logFilestring
3838
)
3939
cmd:=&cobra.Command{
40-
Use:"start --coder-url=[coder_url] --token=[token]",
40+
Use:"start --coder-url=<coder_url> --token=<token> --log-file=<path>",
4141
Short:"starts the coder agent",
4242
Long:"starts the coder agent",
4343
Example:`# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars
44-
4544
coder agent start
4645
4746
# start the agent and connect with a specified url and agent token
48-
4947
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
48+
49+
# start the agent and write a copy of the log to /tmp/coder-agent.log
50+
# if the file already exists, it will be truncated
51+
coder agent start --log-file=/tmp/coder-agent.log
5052
`,
5153
RunE:func(cmd*cobra.Command,args []string)error {
5254
ctx:=cmd.Context()
53-
sinks:= []slog.Sink{
54-
sloghuman.Sink(os.Stderr),
55-
}
5655

57-
file,err:=os.OpenFile(filepath.Join(os.TempDir(),"coder-agent.log"),os.O_RDWR|os.O_CREATE|os.O_TRUNC,0600)
58-
iferr==nil&&file!=nil {
59-
sinks=append(sinks,sloghuman.Sink(file))
60-
}
56+
log:=slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
6157

62-
log:=slog.Make(sinks...).Leveled(slog.LevelDebug)
63-
iferr!=nil {
64-
log.Info(ctx,"failed to open agent log file",slog.Error(err))
58+
// Optional log file path to write
59+
iflogFile!="" {
60+
// Truncate the file if it already exists
61+
file,err:=os.OpenFile(logFile,os.O_RDWR|os.O_CREATE|os.O_TRUNC,0600)
62+
iferr!=nil {
63+
// If an error occurs, log it as an error, but consider it non-fatal
64+
log.Warn(ctx,"failed to open log file",slog.Error(err))
65+
}else {
66+
// Log to both standard output and our file
67+
log=slog.Make(
68+
sloghuman.Sink(os.Stderr),
69+
sloghuman.Sink(file),
70+
).Leveled(slog.LevelDebug)
71+
}
6572
}
73+
6674
ifcoderURL=="" {
6775
varokbool
6876
coderURL,ok=os.LookupEnv("CODER_URL")
@@ -113,6 +121,7 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
113121

114122
cmd.Flags().StringVar(&token,"token","","coder agent token")
115123
cmd.Flags().StringVar(&coderURL,"coder-url","","coder access url")
124+
cmd.Flags().StringVar(&logFile,"log-file","","write a copy of logs to file")
116125

117126
returncmd
118127
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp