From b7de9e92b371e69a6e2b83d3047a445ece9b486f Mon Sep 17 00:00:00 2001 From: Yonghwan SO Date: Sat, 28 Jan 2023 00:30:31 +0900 Subject: [PATCH] applied txlogger to log transaction information --- pop/logger.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ pop/popmw/tx.go | 16 +++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/pop/logger.go b/pop/logger.go index 8e2e9d8..2bb4e5f 100644 --- a/pop/logger.go +++ b/pop/logger.go @@ -39,3 +39,50 @@ func Logger(app *buffalo.App) logger { } } } + +// since Pop v6.1.0 +type txlogger = func(level logging.Level, conn interface{}, s string, args ...interface{}) + +func TxLogger(app *buffalo.App) txlogger { + return func(lvl logging.Level, conn interface{}, s string, args ...interface{}) { + if !pop.Debug && lvl <= logging.Debug { + return + } + + l := app.Logger + + if pop.Color { + s = color.YellowString(s) + } + + switch typed := conn.(type) { + case *pop.Connection: + l = l.WithField("conn", typed.ID) + if typed.TX != nil { + l = l.WithField("tx", typed.TX.ID) + } + case *pop.Tx: + l = l.WithField("tx", typed.ID) + default: + l = l.WithField("conn", "unknown") + } + + switch lvl { + case logging.SQL: + if len(args) > 0 { + for i, a := range args { + l = l.WithField(fmt.Sprintf("$%d", i+1), a) + } + } + l.Debug(s) + case logging.Debug: + l.Debugf(s, args...) + case logging.Info: + l.Infof(s, args...) + case logging.Warn: + l.Warnf(s, args...) + default: + l.Printf(s, args...) + } + } +} diff --git a/pop/popmw/tx.go b/pop/popmw/tx.go index 9179df6..3928f19 100644 --- a/pop/popmw/tx.go +++ b/pop/popmw/tx.go @@ -26,10 +26,11 @@ func Transaction(db *pop.Connection) buffalo.MiddlewareFunc { return } if app, ok := i.(*buffalo.App); ok { - pop.SetLogger(pp.Logger(app)) + pop.SetTxLogger(pp.TxLogger(app)) } }) - return func(h buffalo.Handler) buffalo.Handler { + + return func(handler buffalo.Handler) buffalo.Handler { return func(c buffalo.Context) error { // wrap all requests in a transaction and set the length // of time doing things in the db to the log. @@ -44,18 +45,23 @@ func Transaction(db *pop.Connection) buffalo.MiddlewareFunc { c.LogField("db", elapsed) }() + c.LogField("conn", tx.ID) + if tx.TX != nil { + c.LogField("tx", tx.TX.ID) + } + // add the transaction to the context c.Set("tx", tx) // call the next handler; if it errors stop and return the error - if yourError := h(c); yourError != nil { + if yourError := handler(c); yourError != nil { return yourError } // check the response status code. if the code is NOT 200..399 // then it is considered "NOT SUCCESSFUL" and an error will be returned if res, ok := c.Response().(*buffalo.Response); ok { - if res.Status < 200 || res.Status >= 400 { + if res.Status >= 400 { return errNonSuccess } } @@ -68,7 +74,7 @@ func Transaction(db *pop.Connection) buffalo.MiddlewareFunc { // * nil - everything went well, if so, return // * yourError - an error returned from your application, middleware, etc... // * a database error - this is returned if there were problems committing the transaction - // * a errNonSuccess - this is returned if the response status code is not between 200..399 + // * a errNonSuccess - this is returned if the response status code is 4xx or 5xx if couldBeDBorYourErr != nil && !errors.Is(couldBeDBorYourErr, errNonSuccess) { return couldBeDBorYourErr }