0

I have a code (golang/postgres) something like this and I want to print/save the raw sql statement created.

 state.ConnectionState.DB.Find(&companies,`company.name similar to ?`, param1)
Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • The arguments that have to pass is a file pointer. But I need to log this in the DB – user1425212 Aug 03 '20 at 12:44
  • This sounds more like an audit log for executed sql commands. This belongs on the DB side, not the client side and isn't something gorm does. – Marc Aug 03 '20 at 12:47
  • What I mean is , Instead of writing logs to a file, I want to save it in the DB. But in the example I got from the gorm site "db.SetLogger(log.New(os.Stdout, "\r\n", 0))" . the os.Stdout is a file pointer – user1425212 Aug 03 '20 at 13:02
  • I understand. And you'll have to find some other way to do it, gorm doesn't do that. Either use DB-level tooling, or pass a `io.Writer` to [log.New](https://golang.org/pkg/log/#New) then do your own writes to the DB. You'll probably have to distinguish between sql statements and other log entries. – Marc Aug 03 '20 at 13:07
  • Create a custom logger that satisfies GORM's interface of a logger which is really simple. Add a db connection for example to new custom logger struct and use it to save log entries to the DB. Here is an example of custom logger: https://gist.github.com/bnadland/2e4287b801a47dcfcc94 – D.C. Joo Aug 04 '20 at 14:48
  • Thanks D.C. Joo, This make sence – user1425212 Aug 07 '20 at 08:43

0 Answers0