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
logThere is a confusing import of
loglets have a closer look
First, let's talk about the
logpackage import. Assuming from the last line, he is usinglogrusSo
is out of the question, because you will lose the power of
logrusthen.Now if you rename
logrustologby usingIt will generate another error:
logrusadapterno 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