@@ -117,11 +117,31 @@ func logQueryArgs(args []any) []any {
117
117
return logArgs
118
118
}
119
119
120
+ // TraceLogConfig holds the configuration for key names
121
+ type TraceLogConfig struct {
122
+ TimeKey string
123
+ }
124
+
125
+ // DefaultTraceLogConfig returns the default configuration for TraceLog
126
+ func DefaultTraceLogConfig () * TraceLogConfig {
127
+ return & TraceLogConfig {
128
+ TimeKey : "time" ,
129
+ }
130
+ }
131
+
120
132
// TraceLog implements pgx.QueryTracer, pgx.BatchTracer, pgx.ConnectTracer, and pgx.CopyFromTracer. All fields are
121
133
// required.
122
134
type TraceLog struct {
123
135
Logger Logger
124
136
LogLevel LogLevel
137
+ Config * TraceLogConfig
138
+ }
139
+
140
+ // ensureConfig initializes the Config field with default values if it is nil.
141
+ func (tl * TraceLog ) ensureConfig () {
142
+ if tl .Config == nil {
143
+ tl .Config = DefaultTraceLogConfig ()
144
+ }
125
145
}
126
146
127
147
type ctxKey int
@@ -150,20 +170,21 @@ func (tl *TraceLog) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pg
150
170
}
151
171
152
172
func (tl * TraceLog ) TraceQueryEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceQueryEndData ) {
173
+ tl .ensureConfig ()
153
174
queryData := ctx .Value (tracelogQueryCtxKey ).(* traceQueryData )
154
175
155
176
endTime := time .Now ()
156
177
interval := endTime .Sub (queryData .startTime )
157
178
158
179
if data .Err != nil {
159
180
if tl .shouldLog (LogLevelError ) {
160
- tl .log (ctx , conn , LogLevelError , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), "err" : data .Err , "time" : interval })
181
+ tl .log (ctx , conn , LogLevelError , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), "err" : data .Err , tl . Config . TimeKey : interval })
161
182
}
162
183
return
163
184
}
164
185
165
186
if tl .shouldLog (LogLevelInfo ) {
166
- tl .log (ctx , conn , LogLevelInfo , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), "time" : interval , "commandTag" : data .CommandTag .String ()})
187
+ tl .log (ctx , conn , LogLevelInfo , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), tl . Config . TimeKey : interval , "commandTag" : data .CommandTag .String ()})
167
188
}
168
189
}
169
190
@@ -191,20 +212,21 @@ func (tl *TraceLog) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pg
191
212
}
192
213
193
214
func (tl * TraceLog ) TraceBatchEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceBatchEndData ) {
215
+ tl .ensureConfig ()
194
216
queryData := ctx .Value (tracelogBatchCtxKey ).(* traceBatchData )
195
217
196
218
endTime := time .Now ()
197
219
interval := endTime .Sub (queryData .startTime )
198
220
199
221
if data .Err != nil {
200
222
if tl .shouldLog (LogLevelError ) {
201
- tl .log (ctx , conn , LogLevelError , "BatchClose" , map [string ]any {"err" : data .Err , "time" : interval })
223
+ tl .log (ctx , conn , LogLevelError , "BatchClose" , map [string ]any {"err" : data .Err , tl . Config . TimeKey : interval })
202
224
}
203
225
return
204
226
}
205
227
206
228
if tl .shouldLog (LogLevelInfo ) {
207
- tl .log (ctx , conn , LogLevelInfo , "BatchClose" , map [string ]any {"time" : interval })
229
+ tl .log (ctx , conn , LogLevelInfo , "BatchClose" , map [string ]any {tl . Config . TimeKey : interval })
208
230
}
209
231
}
210
232
@@ -223,20 +245,21 @@ func (tl *TraceLog) TraceCopyFromStart(ctx context.Context, conn *pgx.Conn, data
223
245
}
224
246
225
247
func (tl * TraceLog ) TraceCopyFromEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceCopyFromEndData ) {
248
+ tl .ensureConfig ()
226
249
copyFromData := ctx .Value (tracelogCopyFromCtxKey ).(* traceCopyFromData )
227
250
228
251
endTime := time .Now ()
229
252
interval := endTime .Sub (copyFromData .startTime )
230
253
231
254
if data .Err != nil {
232
255
if tl .shouldLog (LogLevelError ) {
233
- tl .log (ctx , conn , LogLevelError , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , "time" : interval })
256
+ tl .log (ctx , conn , LogLevelError , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , tl . Config . TimeKey : interval })
234
257
}
235
258
return
236
259
}
237
260
238
261
if tl .shouldLog (LogLevelInfo ) {
239
- tl .log (ctx , conn , LogLevelInfo , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , "time" : interval , "rowCount" : data .CommandTag .RowsAffected ()})
262
+ tl .log (ctx , conn , LogLevelInfo , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , tl . Config . TimeKey : interval , "rowCount" : data .CommandTag .RowsAffected ()})
240
263
}
241
264
}
242
265
@@ -253,6 +276,7 @@ func (tl *TraceLog) TraceConnectStart(ctx context.Context, data pgx.TraceConnect
253
276
}
254
277
255
278
func (tl * TraceLog ) TraceConnectEnd (ctx context.Context , data pgx.TraceConnectEndData ) {
279
+ tl .ensureConfig ()
256
280
connectData := ctx .Value (tracelogConnectCtxKey ).(* traceConnectData )
257
281
258
282
endTime := time .Now ()
@@ -261,11 +285,11 @@ func (tl *TraceLog) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEn
261
285
if data .Err != nil {
262
286
if tl .shouldLog (LogLevelError ) {
263
287
tl .Logger .Log (ctx , LogLevelError , "Connect" , map [string ]any {
264
- "host" : connectData .connConfig .Host ,
265
- "port" : connectData .connConfig .Port ,
266
- "database" : connectData .connConfig .Database ,
267
- "time" : interval ,
268
- "err" : data .Err ,
288
+ "host" : connectData .connConfig .Host ,
289
+ "port" : connectData .connConfig .Port ,
290
+ "database" : connectData .connConfig .Database ,
291
+ tl . Config . TimeKey : interval ,
292
+ "err" : data .Err ,
269
293
})
270
294
}
271
295
return
@@ -274,10 +298,10 @@ func (tl *TraceLog) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEn
274
298
if data .Conn != nil {
275
299
if tl .shouldLog (LogLevelInfo ) {
276
300
tl .log (ctx , data .Conn , LogLevelInfo , "Connect" , map [string ]any {
277
- "host" : connectData .connConfig .Host ,
278
- "port" : connectData .connConfig .Port ,
279
- "database" : connectData .connConfig .Database ,
280
- "time" : interval ,
301
+ "host" : connectData .connConfig .Host ,
302
+ "port" : connectData .connConfig .Port ,
303
+ "database" : connectData .connConfig .Database ,
304
+ tl . Config . TimeKey : interval ,
281
305
})
282
306
}
283
307
}
@@ -298,20 +322,21 @@ func (tl *TraceLog) TracePrepareStart(ctx context.Context, _ *pgx.Conn, data pgx
298
322
}
299
323
300
324
func (tl * TraceLog ) TracePrepareEnd (ctx context.Context , conn * pgx.Conn , data pgx.TracePrepareEndData ) {
325
+ tl .ensureConfig ()
301
326
prepareData := ctx .Value (tracelogPrepareCtxKey ).(* tracePrepareData )
302
327
303
328
endTime := time .Now ()
304
329
interval := endTime .Sub (prepareData .startTime )
305
330
306
331
if data .Err != nil {
307
332
if tl .shouldLog (LogLevelError ) {
308
- tl .log (ctx , conn , LogLevelError , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , "err" : data .Err , "time" : interval })
333
+ tl .log (ctx , conn , LogLevelError , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , "err" : data .Err , tl . Config . TimeKey : interval })
309
334
}
310
335
return
311
336
}
312
337
313
338
if tl .shouldLog (LogLevelInfo ) {
314
- tl .log (ctx , conn , LogLevelInfo , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , "time" : interval , "alreadyPrepared" : data .AlreadyPrepared })
339
+ tl .log (ctx , conn , LogLevelInfo , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , tl . Config . TimeKey : interval , "alreadyPrepared" : data .AlreadyPrepared })
315
340
}
316
341
}
317
342
0 commit comments