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

Commit4de99d5

Browse files
authored
Add logs to wsnet listener (#388)
1 parent1294dd9 commit4de99d5

File tree

5 files changed

+199
-188
lines changed

5 files changed

+199
-188
lines changed

‎go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/pkg/browserv0.0.0-20180916011732-0a3d74bf9ce4
2424
github.com/rjeczalik/notifyv0.9.2
2525
github.com/spf13/cobrav1.2.1
26+
github.com/stretchr/testifyv1.7.0
2627
golang.org/x/netv0.0.0-20210614182718-04defd469f4e
2728
golang.org/x/syncv0.0.0-20210220032951-036812b2e83c
2829
golang.org/x/sysv0.0.0-20210514084401-e8d321eab015

‎internal/cmd/agent.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cmd
22

33
import (
4-
"context"
5-
"log"
64
"net/url"
75
"os"
86
"os/signal"
97
"syscall"
108

9+
// We use slog here since agent runs in the background and we can benefit
10+
// from structured logging.
11+
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/sloghuman"
1113
"github.com/spf13/cobra"
1214
"golang.org/x/xerrors"
1315

@@ -46,7 +48,10 @@ coder agent start
4648
coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
4749
`,
4850
RunE:func(cmd*cobra.Command,args []string)error {
49-
ctx:=cmd.Context()
51+
var (
52+
ctx=cmd.Context()
53+
log=slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug)
54+
)
5055
ifcoderURL=="" {
5156
varokbool
5257
coderURL,ok=os.LookupEnv("CODER_URL")
@@ -73,20 +78,23 @@ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
7378
}
7479
}
7580

76-
listener,err:=wsnet.Listen(context.Background(),wsnet.ListenEndpoint(u,token),token)
81+
log.Info(ctx,"starting wsnet listener",slog.F("coder_access_url",u.String()))
82+
listener,err:=wsnet.Listen(ctx,log,wsnet.ListenEndpoint(u,token),token)
7783
iferr!=nil {
7884
returnxerrors.Errorf("listen: %w",err)
7985
}
86+
deferfunc() {
87+
err:=listener.Close()
88+
iferr!=nil {
89+
log.Error(ctx,"close listener",slog.Error(err))
90+
}
91+
}()
8092

8193
// Block until user sends SIGINT or SIGTERM
8294
sigs:=make(chan os.Signal,1)
8395
signal.Notify(sigs,syscall.SIGINT,syscall.SIGTERM)
8496
<-sigs
8597

86-
iferr=listener.Close();err!=nil {
87-
log.Panic(err)
88-
}
89-
9098
returnnil
9199
},
92100
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp