From b246ce4803fec7da4251d78dda226a5a50a75f49 Mon Sep 17 00:00:00 2001 From: Kevin Parsons Date: Mon, 18 Mar 2019 23:55:50 -0700 Subject: [PATCH] Map Logrus level to standard ETW level ETW and Logrus have similar, but different definitiosn of log level. Originally, the plan was to just convert the integer value of the Logrus level to an ETW level, and leave it at that. However, it turns out there are tools that assume you are logging with the standard set of ETW log level. So now we will map the Logrus level to an ETW level, so that these tools better. --- pkg/etwlogrus/hook.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/etwlogrus/hook.go b/pkg/etwlogrus/hook.go index fe0835b..c05b1a2 100644 --- a/pkg/etwlogrus/hook.go +++ b/pkg/etwlogrus/hook.go @@ -40,9 +40,22 @@ func (h *Hook) Levels() []logrus.Level { } } +var logrusToETWLevelMap = map[logrus.Level]etw.Level{ + logrus.PanicLevel: etw.LevelAlways, + logrus.FatalLevel: etw.LevelCritical, + logrus.ErrorLevel: etw.LevelError, + logrus.WarnLevel: etw.LevelWarning, + logrus.InfoLevel: etw.LevelInfo, + logrus.DebugLevel: etw.LevelVerbose, + logrus.TraceLevel: etw.LevelVerbose, +} + // Fire receives each Logrus entry as it is logged, and logs it to ETW. func (h *Hook) Fire(e *logrus.Entry) error { - level := etw.Level(e.Level) + // Logrus defines more levels than ETW typically uses, but analysis is + // easiest when using a consistent set of levels across ETW providers, so we + // map the Logrus levels to ETW levels. + level := logrusToETWLevelMap[e.Level] if !h.provider.IsEnabledForLevel(level) { return nil } @@ -56,9 +69,6 @@ func (h *Hook) Fire(e *logrus.Entry) error { fields = append(fields, getFieldOpt(k, v)) } - // We could try to map Logrus levels to ETW levels, but we would lose some - // fidelity as there are fewer ETW levels. So instead we use the level - // directly. return h.provider.WriteEvent( "LogrusEntry", etw.WithEventOpts(etw.WithLevel(level)),