qustavo/sqlhooks

how to log sql when sql execute failed?

song-without-words opened this issue · 9 comments

how to log sql when sql execute failed?

Did you try implementing the on OnErrorer[1] interface?
[1]: https://github.com/gchaincl/sqlhooks/blob/master/sqlhooks.go#L21

thanks for your response. it worked!

Sorry, now i have a new problem. Sometimes error log will print twice and i can't find the reason.

can u send the code

func (h *Hooks) OnError(ctx context.Context, err error, query string, args ...interface{}) error {

begin := ctx.Value("begin").(time.Time)
var traceLog string
res := ctx.Value("traceLog")
if res != nil {
	traceLog = res.(string)
} else {
	traceLog, _ = shortid.Generate()
}
l := clog.NewWithCtx(traceLog)
l.Info("SQL | %10s | %s %v", utils.FormatDuration(time.Since(begin)), query, args)
return err
}

then error sql will log twice.

could you just try to print a single like in the OnError hook?
so that we can be sure that the problem is with the hook and not with your logger?

thanks for your response. now i think the problem is not with the hook. i use sqlx, then whatever sql is right or wrong, sqlx throws a driver.ErrSkip error and OnError func will exec, so if sql is right, it will print twice.Do you know how to solve it?
error: driver: skip fast-path; continue as if unimplemented

can you fmt.Printf("error: %+v (%T)", err, err) on the hook.
If the errors are different you can act differently depending on the error you get

thanks for your response, but unfortunately, the errors is the same.
error: driver: skip fast-path; continue as if unimplemented (*errors.errorString)