Add context.Context to Logger interface
This allows custom logger adapters to add additional fields to log messages. For example, a HTTP server may with to log the request ID. fixes #428
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
package log15adapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
@@ -24,7 +26,7 @@ func NewLogger(l Log15Logger) *Logger {
|
||||
return &Logger{l: l}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
func (l *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
logArgs := make([]interface{}, 0, len(data))
|
||||
for k, v := range data {
|
||||
logArgs = append(logArgs, k, v)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
package logrusadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -15,7 +17,7 @@ func NewLogger(l logrus.FieldLogger) *Logger {
|
||||
return &Logger{l: l}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
func (l *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
var logger logrus.FieldLogger
|
||||
if data != nil {
|
||||
logger = l.l.WithFields(data)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package testingadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
@@ -22,7 +23,7 @@ func NewLogger(l TestingLogger) *Logger {
|
||||
return &Logger{l: l}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
func (l *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
logArgs := make([]interface{}, 0, 2+len(data))
|
||||
logArgs = append(logArgs, level, msg)
|
||||
for k, v := range data {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
package zapadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
@@ -15,7 +17,7 @@ func NewLogger(logger *zap.Logger) *Logger {
|
||||
return &Logger{logger: logger.WithOptions(zap.AddCallerSkip(1))}
|
||||
}
|
||||
|
||||
func (pl *Logger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
fields := make([]zapcore.Field, len(data))
|
||||
i := 0
|
||||
for k, v := range data {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
package zerologadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
@@ -18,7 +20,7 @@ func NewLogger(logger zerolog.Logger) *Logger {
|
||||
}
|
||||
}
|
||||
|
||||
func (pl *Logger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
var zlevel zerolog.Level
|
||||
switch level {
|
||||
case pgx.LogLevelNone:
|
||||
|
||||
Reference in New Issue
Block a user