casbin / casbin-pg-adapter

A go-pg adapter for casbin

Home Page:https://github.com/casbin/casbin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Logging all SQL Queries

arafat-java opened this issue · comments

We are using casbin-pg-adapter for our policies storage to DB
I would like to log all the queries that are fired to the DB.
How can I do that?

Easiest thing one can do is to configure Postgres to log statements to stdout. I use this script:

#!/bin/sh
set -x

PGCONF=/var/lib/postgresql/data/postgresql.conf
sed -i "s/#log_statement = 'none'.*/log_statement = 'all'/g" $PGCONF

I've been using AWS RDS so that'll be a bit difficult for me

There is also go-pg query hook for this purpose:

type dbLogger struct{}

func (d dbLogger) BeforeQuery(ctx context.Context, q *pg.QueryEvent) (context.Context, error) {
	fmt.Println(q.FormattedQuery())
	return ctx, nil
}

func (d dbLogger) AfterQuery(ctx context.Context, q *pg.QueryEvent) error {
	return nil
}

db := pg.Connect(opts)
db.AddQueryHook(dbLogger{})

Thanks @pckhoi
Works perfectly for me

Closing this out