From a3f9494b4ba43bc53a7e1955d3f21a9e2c23702c Mon Sep 17 00:00:00 2001 From: Jason Bornstein <131717043+jasonbornsteinMOOV@users.noreply.github.com> Date: Thu, 3 Aug 2023 09:00:34 -0400 Subject: [PATCH] logging enhancements --- pkg/response/file_transformer.go | 3 ++ pkg/response/match/matcher.go | 56 ++++---------------------------- pkg/service/model_config.go | 41 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/pkg/response/file_transformer.go b/pkg/response/file_transformer.go index fa5e6ad6..28e74d7a 100644 --- a/pkg/response/file_transformer.go +++ b/pkg/response/file_transformer.go @@ -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) diff --git a/pkg/response/match/matcher.go b/pkg/response/match/matcher.go index 37c56b38..9230b97d 100644 --- a/pkg/response/match/matcher.go +++ b/pkg/response/match/matcher.go @@ -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 @@ -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{ diff --git a/pkg/service/model_config.go b/pkg/service/model_config.go index d195213c..96ee6b54 100644 --- a/pkg/service/model_config.go +++ b/pkg/service/model_config.go @@ -8,6 +8,7 @@ import ( "time" "github.com/moov-io/ach" + "github.com/moov-io/base/log" ) type GlobalConfig struct { @@ -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 == "" && @@ -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 {