@@ -122,10 +122,13 @@ type TraceLogConfig struct {
122
122
TimeKey string
123
123
}
124
124
125
+ // defaultTimeKey is the name used in log statements for the timing, i.e. end time - start time, of an operation.
126
+ const defaultTimeKey = "time"
127
+
125
128
// DefaultTraceLogConfig returns the default configuration for TraceLog
126
129
func DefaultTraceLogConfig () * TraceLogConfig {
127
130
return & TraceLogConfig {
128
- TimeKey : "time" ,
131
+ TimeKey : defaultTimeKey ,
129
132
}
130
133
}
131
134
@@ -137,11 +140,11 @@ type TraceLog struct {
137
140
Config * TraceLogConfig
138
141
}
139
142
140
- // ensureConfig initializes the Config field with default values if it is nil.
141
- func (tl * TraceLog ) ensureConfig () {
143
+ func (tl * TraceLog ) determineTimeKey () string {
142
144
if tl .Config == nil {
143
- tl . Config = DefaultTraceLogConfig ()
145
+ return defaultTimeKey
144
146
}
147
+ return tl .Config .TimeKey
145
148
}
146
149
147
150
type ctxKey int
@@ -170,21 +173,21 @@ func (tl *TraceLog) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pg
170
173
}
171
174
172
175
func (tl * TraceLog ) TraceQueryEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceQueryEndData ) {
173
- tl .ensureConfig ()
174
176
queryData := ctx .Value (tracelogQueryCtxKey ).(* traceQueryData )
175
177
176
178
endTime := time .Now ()
177
179
interval := endTime .Sub (queryData .startTime )
180
+ timeKey := tl .determineTimeKey ()
178
181
179
182
if data .Err != nil {
180
183
if tl .shouldLog (LogLevelError ) {
181
- tl .log (ctx , conn , LogLevelError , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), "err" : data .Err , tl . Config . TimeKey : interval })
184
+ tl .log (ctx , conn , LogLevelError , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), "err" : data .Err , timeKey : interval })
182
185
}
183
186
return
184
187
}
185
188
186
189
if tl .shouldLog (LogLevelInfo ) {
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 ()})
190
+ tl .log (ctx , conn , LogLevelInfo , "Query" , map [string ]any {"sql" : queryData .sql , "args" : logQueryArgs (queryData .args ), timeKey : interval , "commandTag" : data .CommandTag .String ()})
188
191
}
189
192
}
190
193
@@ -212,21 +215,21 @@ func (tl *TraceLog) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pg
212
215
}
213
216
214
217
func (tl * TraceLog ) TraceBatchEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceBatchEndData ) {
215
- tl .ensureConfig ()
216
218
queryData := ctx .Value (tracelogBatchCtxKey ).(* traceBatchData )
217
219
218
220
endTime := time .Now ()
219
221
interval := endTime .Sub (queryData .startTime )
222
+ timeKey := tl .determineTimeKey ()
220
223
221
224
if data .Err != nil {
222
225
if tl .shouldLog (LogLevelError ) {
223
- tl .log (ctx , conn , LogLevelError , "BatchClose" , map [string ]any {"err" : data .Err , tl . Config . TimeKey : interval })
226
+ tl .log (ctx , conn , LogLevelError , "BatchClose" , map [string ]any {"err" : data .Err , timeKey : interval })
224
227
}
225
228
return
226
229
}
227
230
228
231
if tl .shouldLog (LogLevelInfo ) {
229
- tl .log (ctx , conn , LogLevelInfo , "BatchClose" , map [string ]any {tl . Config . TimeKey : interval })
232
+ tl .log (ctx , conn , LogLevelInfo , "BatchClose" , map [string ]any {timeKey : interval })
230
233
}
231
234
}
232
235
@@ -245,21 +248,21 @@ func (tl *TraceLog) TraceCopyFromStart(ctx context.Context, conn *pgx.Conn, data
245
248
}
246
249
247
250
func (tl * TraceLog ) TraceCopyFromEnd (ctx context.Context , conn * pgx.Conn , data pgx.TraceCopyFromEndData ) {
248
- tl .ensureConfig ()
249
251
copyFromData := ctx .Value (tracelogCopyFromCtxKey ).(* traceCopyFromData )
250
252
251
253
endTime := time .Now ()
252
254
interval := endTime .Sub (copyFromData .startTime )
255
+ timeKey := tl .determineTimeKey ()
253
256
254
257
if data .Err != nil {
255
258
if tl .shouldLog (LogLevelError ) {
256
- tl .log (ctx , conn , LogLevelError , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , tl . Config . TimeKey : interval })
259
+ tl .log (ctx , conn , LogLevelError , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , timeKey : interval })
257
260
}
258
261
return
259
262
}
260
263
261
264
if tl .shouldLog (LogLevelInfo ) {
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 ()})
265
+ tl .log (ctx , conn , LogLevelInfo , "CopyFrom" , map [string ]any {"tableName" : copyFromData .TableName , "columnNames" : copyFromData .ColumnNames , "err" : data .Err , timeKey : interval , "rowCount" : data .CommandTag .RowsAffected ()})
263
266
}
264
267
}
265
268
@@ -276,20 +279,20 @@ func (tl *TraceLog) TraceConnectStart(ctx context.Context, data pgx.TraceConnect
276
279
}
277
280
278
281
func (tl * TraceLog ) TraceConnectEnd (ctx context.Context , data pgx.TraceConnectEndData ) {
279
- tl .ensureConfig ()
280
282
connectData := ctx .Value (tracelogConnectCtxKey ).(* traceConnectData )
281
283
282
284
endTime := time .Now ()
283
285
interval := endTime .Sub (connectData .startTime )
286
+ timeKey := tl .determineTimeKey ()
284
287
285
288
if data .Err != nil {
286
289
if tl .shouldLog (LogLevelError ) {
287
290
tl .Logger .Log (ctx , LogLevelError , "Connect" , map [string ]any {
288
- "host" : connectData .connConfig .Host ,
289
- "port" : connectData .connConfig .Port ,
290
- "database" : connectData .connConfig .Database ,
291
- tl . Config . TimeKey : interval ,
292
- "err" : data .Err ,
291
+ "host" : connectData .connConfig .Host ,
292
+ "port" : connectData .connConfig .Port ,
293
+ "database" : connectData .connConfig .Database ,
294
+ timeKey : interval ,
295
+ "err" : data .Err ,
293
296
})
294
297
}
295
298
return
@@ -298,10 +301,10 @@ func (tl *TraceLog) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEn
298
301
if data .Conn != nil {
299
302
if tl .shouldLog (LogLevelInfo ) {
300
303
tl .log (ctx , data .Conn , LogLevelInfo , "Connect" , map [string ]any {
301
- "host" : connectData .connConfig .Host ,
302
- "port" : connectData .connConfig .Port ,
303
- "database" : connectData .connConfig .Database ,
304
- tl . Config . TimeKey : interval ,
304
+ "host" : connectData .connConfig .Host ,
305
+ "port" : connectData .connConfig .Port ,
306
+ "database" : connectData .connConfig .Database ,
307
+ timeKey : interval ,
305
308
})
306
309
}
307
310
}
@@ -322,21 +325,21 @@ func (tl *TraceLog) TracePrepareStart(ctx context.Context, _ *pgx.Conn, data pgx
322
325
}
323
326
324
327
func (tl * TraceLog ) TracePrepareEnd (ctx context.Context , conn * pgx.Conn , data pgx.TracePrepareEndData ) {
325
- tl .ensureConfig ()
326
328
prepareData := ctx .Value (tracelogPrepareCtxKey ).(* tracePrepareData )
327
329
328
330
endTime := time .Now ()
329
331
interval := endTime .Sub (prepareData .startTime )
332
+ timeKey := tl .determineTimeKey ()
330
333
331
334
if data .Err != nil {
332
335
if tl .shouldLog (LogLevelError ) {
333
- tl .log (ctx , conn , LogLevelError , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , "err" : data .Err , tl . Config . TimeKey : interval })
336
+ tl .log (ctx , conn , LogLevelError , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , "err" : data .Err , timeKey : interval })
334
337
}
335
338
return
336
339
}
337
340
338
341
if tl .shouldLog (LogLevelInfo ) {
339
- tl .log (ctx , conn , LogLevelInfo , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , tl . Config . TimeKey : interval , "alreadyPrepared" : data .AlreadyPrepared })
342
+ tl .log (ctx , conn , LogLevelInfo , "Prepare" , map [string ]any {"name" : prepareData .name , "sql" : prepareData .sql , timeKey : interval , "alreadyPrepared" : data .AlreadyPrepared })
340
343
}
341
344
}
342
345
0 commit comments