Skip to content

Commit

Permalink
Derive channel logger from session logger (#141)
Browse files Browse the repository at this point in the history
- This way, each log entry created by channel logger also includes
the fields registered on session logger such as session-id.

Closes #134

Signed-off-by: Anagha Sukumaran <sukumaran.anaghak@in.bosch.com>
  • Loading branch information
anagha-ks authored and manoranjith committed Dec 18, 2020
1 parent 59dbe2a commit 27efa4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 12 additions & 2 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ func NewLoggerWithField(key string, value interface{}) Logger {
if logger == nil {
InitLogger("debug", "") // nolint: errcheck, gosec // err will always be nil in this case.
}
l := logger.WithField(key, value)
return l
return logger.WithField(key, value)
}

// NewDerivedLoggerWithField returns a logger that inherits all properties of the parent logger,
// and add the given fields for each log entry.
//
// Panics if parent logger is nil.
func NewDerivedLoggerWithField(parentLogger Logger, key string, value interface{}) Logger {
if parentLogger == nil {
panic("parent logger should not be nil")
}
return parentLogger.WithField(key, value)
}

// customTextFormatter is defined to override default formating options for log entry.
Expand Down
3 changes: 1 addition & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ func makeAllocation(balInfo perun.BalInfo, chAsset pchannel.Asset) (*pchannel.Al

// addCh adds the channel to session. It locks the session mutex during the operation.
func (s *session) addCh(ch *channel) {
ch.Logger = log.NewLoggerWithField("channel-id", ch.id)
ch.Logger = log.NewDerivedLoggerWithField(s.Logger, "channel-id", ch.id)
s.Lock()
// TODO: (mano) use logger with multiple fields and use session-id, channel-id.
s.chs[ch.id] = ch
s.Unlock()
}
Expand Down

0 comments on commit 27efa4d

Please sign in to comment.