Skip to content

Commit 79a1e96

Browse files
committed
Disable trace log by default
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
1 parent 878bfa4 commit 79a1e96

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

pg/client.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net"
2727
"strconv"
2828

29+
"github.com/jackc/pgx/v5"
2930
"github.com/jackc/pgx/v5/multitracer"
3031
"github.com/jackc/pgx/v5/pgxpool"
3132
"github.com/jackc/pgx/v5/tracelog"
@@ -51,6 +52,8 @@ type (
5152
password string
5253
database string
5354

55+
debug bool
56+
5457
poolSize int32
5558

5659
tlsConfig *tls.Config
@@ -153,6 +156,13 @@ func WithRegisterer(r prometheus.Registerer) Option {
153156
}
154157
}
155158

159+
// WithDebug enables debug logging for the client.
160+
func WithDebug() Option {
161+
return func(c *Client) {
162+
c.debug = true
163+
}
164+
}
165+
156166
// NewClient creates a new database client with customizable options
157167
// for logging, tracing, TLS, and Prometheus metrics.
158168
//
@@ -208,13 +218,21 @@ func NewClient(options ...Option) (*Client, error) {
208218
),
209219
)
210220

211-
config.ConnConfig.Tracer = multitracer.New(
221+
tracers := []pgx.QueryTracer{
212222
&tracer{c.tracer},
213-
&tracelog.TraceLog{
214-
Logger: &logger{c.logger}, // TODO not enable tracelog by default
215-
LogLevel: tracelog.LogLevelInfo,
216-
},
217-
)
223+
}
224+
225+
if c.debug {
226+
tracers = append(
227+
tracers,
228+
&tracelog.TraceLog{
229+
Logger: &logger{c.logger},
230+
LogLevel: tracelog.LogLevelInfo,
231+
},
232+
)
233+
}
234+
235+
config.ConnConfig.Tracer = multitracer.New(tracers...)
218236

219237
pool, err := pgxpool.NewWithConfig(context.Background(), config)
220238
if err != nil {

0 commit comments

Comments
 (0)