-
-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logger or output of raw SQL generated #45
Comments
No, but in development environment you can adjust PostgreSQL to log all queries:
Then |
That works for now. Thanks. |
As it mentioned in wiki it also can be done this way: type dbLogger struct { }
func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
fmt.Println(q.FormattedQuery())
return nil
}
db := pg.Connect(&pg.Options{...})
db.AddQueryHook(dbLogger{}) |
When I try that, I get this: |
This seems to work when trying to AfterQuery method. I haven't worked out the BeforeQuery method yet: `type dbLogger struct{} func (d dbLogger) AfterQuery(q *pg.QueryEvent) { db := pg.Connect(&pg.Options{...}) |
@PhillyWebGuy you likely have an incorrect import. Make sure you have imported: "github.com/go-pg/pg/v10" |
I am not able to print my query |
https://github.com/monetr/monetr/blob/9f3129958e49e2ef94497e1daa7a3595b6d3f89d/pkg/logging/pg.go This file has an example of a logrus hook that I made to print queries along with some basic context around them, as well as sending query information to sentry.io. Specifically: func (h *PostgresHooks) BeforeQuery(ctx context.Context, event *pg.QueryEvent) (context.Context, error) {
query, err := event.FormattedQuery()
if err != nil {
return ctx, nil
}
if strings.TrimSpace(strings.ToLower(string(query))) != "select 1" {
h.log.WithContext(ctx).Trace(strings.TrimSpace(string(query)))
}
return ctx, nil
} |
Also let me know how to use your code. type Hook struct {
Id int
Value string
}
func (d Hook) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
q.StartTime = time.Now()
return c, nil
}
func (d Hook) AfterQuery(c context.Context, q *pg.QueryEvent) error {
fmt.Println("----DEBUGGER----")
fmt.Println(q.FormattedQuery())
fmt.Printf("%s%s\n\n", time.Since(q.StartTime), q.Err.Error())
return nil
}
h := Hook{}
h.BeforeQuery(conn.Context(), &pg.QueryEvent{})
h.AfterQuery(conn.Context(), &pg.QueryEvent{})
conn.AddQueryHook(h) This is giving panic
|
At a glance it looks like the line fmt.Printf("%s%s\n\n", time.Since(q.StartTime), q.Err.Error()) is the one thats causing the panic, becausee |
It worked but how can I print file name and function name and line no . Where I will get them |
The file name and function that is calling the query? You'd have to traverse the stack trace a bit. https://golang.hotexamples.com/examples/runtime/-/Caller/golang-caller-function-examples.html these might be some good examples to get started? |
Thanks |
Can you help me in different issue That issue was solved. Thanks for the help. I have some other issue This is my table if err = tx.Model(tableModel).CreateTable( CREATE TABLE IF NOT EXISTS "test_users" ("user_id" bigint, "username" text, "password" text, "pass_salt" text, "email" text, "name" text, "alt_contact_no" text, "alt_phone_code" text, "landline" text, "department" text, "designation" text, "email_verified" text, "phone_verified" text, "whatsapp_verified" text, "attribute" jsonb, "last_login" timestamptz, "created_by" bigint, "created_at" timestamptz, "updated_at" timestamptz) See the table name has extra s at last. Can you tell me how I can resolve this. |
If you change the tableName field to be:
does it work then? |
No |
tableName string This worked |
Hello |
I believe you'd have to initialize a new DB object entirely and simply not register that hook. It'd probably be better to have the hook functions itself though be aware of when you do and do not want them to be fired rather than trying to "remove the hook". |
I've started migrating my project from beego ORM to use pg.v2. So far, it's been fantastic and handles some of few PostgreSQL issues I've had like UUID and Array support. Is there a way to output the raw generated SQL?
The text was updated successfully, but these errors were encountered: