Skip to content

Commit 14760dd

Browse files
authored
PBM-1460: Connection error isn't written to PBM log despite log-path option is specified (#1066)
Apply logger opts when db conn still doesn't exist.
1 parent d6bacd7 commit 14760dd

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

cmd/pbm-agent/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ func main() {
9393
LogLevel: *logLevel,
9494
LogJSON: *logJSON,
9595
}
96+
l := log.NewWithOpts(nil, "", "", logOpts).NewDefaultEvent()
9697

9798
err = runAgent(url, *dumpConns, logOpts)
98-
stdlog.Println("Exit:", err)
9999
if err != nil {
100+
l.Error("Exit: %v", err)
100101
os.Exit(1)
101102
}
103+
l.Info("Exit: <nil>")
102104
}
103105

104106
func runAgent(
@@ -125,7 +127,6 @@ func runAgent(
125127
}
126128

127129
logger := log.NewWithOpts(
128-
ctx,
129130
agent.leadConn,
130131
agent.brief.SetName,
131132
agent.brief.Me,

pbm/log/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func New(conn connect.Client, rs, node string) Logger {
5454
}
5555

5656
// NewWithOpts creates logger based on provided options.
57-
func NewWithOpts(ctx context.Context, conn connect.Client, rs, node string, opts *Opts) Logger {
57+
func NewWithOpts(conn connect.Client, rs, node string, opts *Opts) Logger {
5858
l := &loggerImpl{
5959
conn: conn,
6060
rs: rs,

pbm/log/logger_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package log
2+
3+
import (
4+
"io"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"go.mongodb.org/mongo-driver/bson/primitive"
10+
)
11+
12+
func TestLoggerConstructor(t *testing.T) {
13+
t.Run("logger logs without db connection", func(t *testing.T) {
14+
t.Run("logger with opts", func(t *testing.T) {
15+
f := "db-conn-nil"
16+
l := NewWithOpts(nil, "rs", "node", &Opts{LogPath: f})
17+
defer os.Remove(f)
18+
19+
l.Debug("", "", "", primitive.Timestamp{}, "msg: %v", "nil conn")
20+
21+
lEntry, _ := os.ReadFile(f)
22+
if !strings.Contains(string(lEntry), "msg: nil conn") {
23+
t.Errorf("expected: 'msg: nil conn', got: %q", string(lEntry))
24+
}
25+
})
26+
27+
t.Run("logger without opts", func(t *testing.T) {
28+
old := os.Stderr
29+
r, w, _ := os.Pipe()
30+
os.Stderr = w
31+
defer func() { os.Stderr = old }()
32+
33+
l := New(nil, "rs", "node").NewDefaultEvent()
34+
msg := "msg from logger without opts"
35+
36+
l.Info(msg)
37+
38+
w.Close()
39+
stdErrOut, _ := io.ReadAll(r)
40+
41+
if !strings.Contains(string(stdErrOut), msg) {
42+
t.Errorf("expected: %q, got: %q", msg, string(stdErrOut))
43+
}
44+
})
45+
})
46+
}

0 commit comments

Comments
 (0)