Full credit goes to @mystdeim, who answered above.
Reason for copying: Clear explanation of imports
Let's begin
Original answer:
func DB() *pgxpool.Pool {
config, err := pgxpool.ParseConfig(connStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
os.Exit(1)
}
looger := &log.Logger{
Out: os.Stderr,
Formatter: new(log.JSONFormatter),
Hooks: make(log.LevelHooks),
Level: log.InfoLevel,
ExitFunc: os.Exit,
ReportCaller: false,
}
config.ConnConfig.Logger = logrusadapter.NewLogger(looger)
conn, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
return conn
}
The above code is ok, but I will talk about two points here
- Import:
log
There is a confusing import of log
lets have a closer look
looger := &log.Logger{
Out: os.Stderr,
Formatter: new(log.JSONFormatter),
Hooks: make(log.LevelHooks),
Level: log.InfoLevel,
ExitFunc: os.Exit,
ReportCaller: false,
}
config.ConnConfig.Logger = logrusadapter.NewLogger(looger)
First, let's talk about the log
package import. Assuming from the last line, he is using logrus
So
import (
"log"
)
is out of the question, because you will lose the power of logrus
then.
Now if you rename logrus
to log
by using
import (
log "github.com/sirupsen/logrus"
)
It will generate another error:
LstdFlags not declared by package logrus (UndeclaredImportedName)
- Import
logrusadapter
no longer works:
import (
"github.com/jackc/pgx/log/logrusadapter"
)
currently working:
import (
"github.com/jackc/pgx/v4/log/logrusadapter"
)
[ well, it seems it is in v4 in 2021, make sure to check your version before importing in the future]
- My modified solution
You don't need to remame logrus
, keep it as it is.
import (
"github.com/sirupsen/logrus"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v4/log/logrusadapter"
)
and finally
func DB() *pgxpool.Pool {
config, err := pgxpool.ParseConfig(connStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse config: %v\n", err)
os.Exit(1)
}
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)
conn, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
return conn
}
Big thanks to @mystdeim for helping me to find a good logging system