Skip to content

Commit

Permalink
logging enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbornsteinMOOV committed Aug 3, 2023
1 parent 1e305a7 commit a3f9494
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
3 changes: 3 additions & 0 deletions pkg/response/file_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (ft *FileTransfomer) Transform(file *ach.File) error {

func processMatchedAction(action *service.Action, ft *FileTransfomer, mirror *batchMirror, file *ach.File, batch ach.Batcher, entries []*ach.EntryDetail, i int, j int) (ach.Batcher, error) {
if action != nil {
logger := ft.Matcher.Logger.With(action)
logger.Log("Processing matched action")

entry, err := ft.Entry.MorphEntry(file.Header, entries[j], action)
if err != nil {
return nil, fmt.Errorf("transform batch[%d] morph entry[%d] error: %v", i, j, err)
Expand Down
56 changes: 7 additions & 49 deletions pkg/response/match/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func New(logger log.Logger, cfg service.Matching, responses []service.Response)
}

func (m Matcher) FindAction(ed *ach.EntryDetail) (copyAction *service.Action, processAction *service.Action) {
logger := m.Logger.With(log.Fields{
"entry_trace_number": log.String(ed.TraceNumber),
})
logger.Log("starting EntryDetail matching")

/*
* See https://github.com/moov-io/ach-test-harness#config-schema for more details on how to configure.
*/
for i := range m.Responses {
logger := m.Logger.With(log.Fields{
"entry_trace_number": log.String(ed.TraceNumber),
})
logger.Log("starting EntryDetail matching")

positive, negative := 0, 0 // Matchers are AND'd together
matcher := m.Responses[i].Match
action := m.Responses[i].Action
Expand All @@ -48,50 +48,8 @@ func (m Matcher) FindAction(ed *ach.EntryDetail) (copyAction *service.Action, pr
continue // skip, we already have a process action
}

var delayTime string
var copyPath string
var correctionCode string
var correctionData string
var returnCode string
var amount int

// Safely retrieve several values that are needed for the debug log below
if action.Delay != nil {
delayTime = action.Delay.String()
logger = logger.With(log.Fields{
"delay": log.String(delayTime),
})
}

if action.Copy != nil {
copyPath = action.Copy.Path
logger = logger.With(log.Fields{
"copy_path": log.String(copyPath),
})
}

if action.Correction != nil {
correctionCode = action.Correction.Code
correctionData = action.Correction.Data
logger = logger.With(log.Fields{
"correction_code": log.String(correctionCode),
"correction_data": log.String(correctionData),
})
}

if action.Return != nil {
returnCode = action.Return.Code
logger = logger.With(log.Fields{
"return_code": log.String(returnCode),
})
}

if matcher.Amount != nil {
amount = matcher.Amount.Value
logger = logger.With(log.Fields{
"amount": log.Int(amount),
})
}
logger = logger.With(action)
logger = logger.With(matcher)

if m.Debug {
logger = logger.With(log.Fields{
Expand Down
41 changes: 41 additions & 0 deletions pkg/service/model_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/moov-io/ach"
"github.com/moov-io/base/log"
)

type GlobalConfig struct {
Expand Down Expand Up @@ -110,6 +111,17 @@ type Match struct {
TraceNumber string
}

func (m Match) Context() map[string]log.Valuer {
logFields := log.Fields{}

if m.Amount != nil {
var amount = m.Amount.Value
logFields["amount"] = log.Int(amount)
}

return logFields
}

func (m Match) Empty() bool {
return m.AccountNumber == "" && m.Amount.Empty() &&
string(m.EntryType) == "" && m.IndividualName == "" &&
Expand Down Expand Up @@ -145,6 +157,35 @@ type Action struct {
Return *Return
}

func (a Action) Context() map[string]log.Valuer {
logFields := log.Fields{}

// Safely retrieve several values that are needed for the debug log below
if a.Delay != nil {
var delayTime = a.Delay.String()
logFields["delay"] = log.String(delayTime)
}

if a.Copy != nil {
var copyPath = a.Copy.Path
logFields["copy_path"] = log.String(copyPath)
}

if a.Correction != nil {
var correctionCode = a.Correction.Code
var correctionData = a.Correction.Data
logFields["correction_code"] = log.String(correctionCode)
logFields["correction_data"] = log.String(correctionData)
}

if a.Return != nil {
var returnCode = a.Return.Code
logFields["return_code"] = log.String(returnCode)
}

return logFields
}

func (a *Action) Validate() error {
// Delay is only valid for Return and Correction
if a.Delay != nil && a.Copy != nil {
Expand Down

0 comments on commit a3f9494

Please sign in to comment.