MCPcopy
hub / github.com/jackc/pgx / TestLoggerFunc

Function TestLoggerFunc

tracelog/tracelog_test.go:103–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

101}
102
103func TestLoggerFunc(t *testing.T) {
104 t.Parallel()
105
106 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
107 defer cancel()
108
109 const testMsg = "foo"
110
111 buf := bytes.Buffer{}
112 logger := log.New(&buf, "", 0)
113
114 createAdapterFn := func(logger *log.Logger) tracelog.LoggerFunc {
115 return func(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any) {
116 logger.Printf("%s", testMsg)
117 }
118 }
119
120 config := defaultConnTestRunner.CreateConfig(ctx, t)
121 config.Tracer = &tracelog.TraceLog{
122 Logger: createAdapterFn(logger),
123 LogLevel: tracelog.LogLevelTrace,
124 }
125
126 conn, err := pgx.ConnectConfig(ctx, config)
127 require.NoError(t, err)
128 defer conn.Close(ctx)
129
130 buf.Reset() // Clear logs written when establishing connection
131
132 if _, err := conn.Exec(context.TODO(), ";"); err != nil {
133 t.Fatal(err)
134 }
135
136 if strings.TrimSpace(buf.String()) != testMsg {
137 t.Errorf("Expected logger function to return '%s', but it was '%s'", testMsg, buf.String())
138 }
139}
140
141func TestLogQuery(t *testing.T) {
142 t.Parallel()

Callers

nothing calls this directly

Calls 4

ResetMethod · 0.80
CloseMethod · 0.65
ExecMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected