I couldn't find it documentation how to log sql queries if I use pgx pool. For example I have created pool like this:
func DB() *pgxpool.Pool {
connStr := os.Getenv("DATABASE_URL")
conn, err := pgxpool.Connect(context.Background(), connStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
return conn
}
Please tell me how to log my queries then?
Full credit goes to @mystdeim, who answered above.
Reason for copying: Clear explanation of imports
Let's begin
Original answer:
The above code is ok, but I will talk about two points here
log
There is a confusing import of
log
lets have a closer look
First, let's talk about the
log
package import. Assuming from the last line, he is usinglogrus
So
is out of the question, because you will lose the power of
logrus
then.Now if you rename
logrus
tolog
by usingIt will generate another error:
logrusadapter
no longer works:
currently working:
[ well, it seems it is in v4 in 2021, make sure to check your version before importing in the future]
You don't need to remame
logrus
, keep it as it is.and finally
Big thanks to @mystdeim for helping me to find a good logging system