-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only the Sentry hook uses the values of the passed context. Therefore, we removed the values from our log statements when we shifted them from an extra `WithField` call to the context. We fix this behavior by introducing a Logrus Hook that copies a fixed set of context values to the logging data.
- Loading branch information
Showing
5 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package logging | ||
|
||
import ( | ||
"github.com/openHPI/poseidon/pkg/dto" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// ContextHook logs the values referenced by the of dto.LoggedContextKeys. | ||
// By default Logrus does not log the values stored in the passed context. | ||
type ContextHook struct{} | ||
|
||
// Fire is triggered on new log entries. | ||
func (hook *ContextHook) Fire(entry *logrus.Entry) error { | ||
if entry.Context != nil { | ||
injectContextValuesIntoData(entry) | ||
} | ||
return nil | ||
} | ||
|
||
func injectContextValuesIntoData(entry *logrus.Entry) { | ||
for _, key := range dto.LoggedContextKeys { | ||
value := entry.Context.Value(key) | ||
_, valueExisting := entry.Data[string(key)] | ||
if !valueExisting && value != nil { | ||
entry.Data[string(key)] = value | ||
} | ||
} | ||
} | ||
|
||
// Levels returns all levels this hook should be registered to. | ||
func (hook *ContextHook) Levels() []logrus.Level { | ||
return []logrus.Level{ | ||
logrus.PanicLevel, | ||
logrus.FatalLevel, | ||
logrus.ErrorLevel, | ||
logrus.WarnLevel, | ||
logrus.InfoLevel, | ||
logrus.DebugLevel, | ||
logrus.TraceLevel, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters