0

I'm following this SO question to implement a logger into my pgx connection since I'm getting a closed pool error on Scan.

I'm getting this error in vscode.

cannot use logrusadapter.NewLogger(logrusLogger) (value of type *logrusadapter.Logger) as "github.com/jackc/pgx/v4".Logger value in assignment: wrong type for method Log (have func(level github.com/jackc/pgx.LogLevel, msg string, data map[string]interface{}), want func(ctx context.Context, level github.com/jackc/pgx/v4.LogLevel, msg string, data map[string]interface{}))

package main

import (
    "context"
    "log"
    "net/http"
    "os"

    "github.com/go-chi/chi"
    "github.com/go-chi/chi/middleware"
    "github.com/go-chi/jwtauth"
    "github.com/go-chi/render"
    "github.com/jackc/pgx/log/logrusadapter"
    "github.com/jackc/pgx/v4/pgxpool"
    "github.com/minio/minio-go/v7"
    "github.com/minio/minio-go/v7/pkg/credentials"
    "github.com/sirupsen/logrus"
)

func router() http.Handler {

    var err error

    // urlExample := "postgres://username:password@localhost:5432/database_name"

    config, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))

    if err != nil {
        log.Fatalln(err)
    }

    logrusLogger := &logrus.Logger{
        Out:          os.Stderr,
        Formatter:    new(logrus.JSONFormatter),
        Hooks:        make(logrus.LevelHooks),
        Level:        logrus.InfoLevel,
        ExitFunc:     os.Exit,
        ReportCaller: false,
    }
    config.ConnConfig.Logger = logrusadapter.NewLogger(logrusLogger)

    db, err := pgxpool.ConnectConfig(context.Background(), config)

    if err != nil {
        log.Fatalln(err)
    }

    defer db.Close()
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
  • 1
    You can easily copy and past the existing logrus adapter into your app, fix it by adding the required `context.Context` parameter (no need to use it) and live happily ever after. Or you can open an issue on github, I'm sure somebody will fix it promptly. :-) – oakad Feb 24 '22 at 02:30
  • I've tried this but somehow can't get the code right. – markhorrocks Feb 24 '22 at 03:07
  • We can only help you with code you share, @markhorrocks. Seems like the code you shared here is farily irrelevant. Show us how you're attempting to wrap or reimplement `logrusadapter` with context. – erik258 Feb 24 '22 at 05:10
  • This looks perfectly fine and workable to me: https://github.com/jackc/pgx/blob/b6b24f9e8a5d7422c229b0d35330347e6e913bdb/log/logrusadapter/adapter.go#L20 Easy to copy and modify. – oakad Feb 24 '22 at 05:10
  • Import same version(`v4`) of adapter. import "github.com/jackc/pgx/v4/log/logrusadapter" – Muhammet Madraimov Jul 26 '22 at 11:00

0 Answers0