Skip to content

Commit

Permalink
Merge pull request #77 from gobuffalo/applying-txlog
Browse files Browse the repository at this point in the history
applied txlogger to log transaction information
  • Loading branch information
sio4 authored Jan 28, 2023
2 parents ac78098 + b7de9e9 commit d6a57ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
47 changes: 47 additions & 0 deletions pop/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
}
16 changes: 11 additions & 5 deletions pop/popmw/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}
Expand All @@ -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
}
Expand Down

0 comments on commit d6a57ce

Please sign in to comment.