Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set ETW event name and options for logrus hook (#245)
* ETW name and options functionality Added functionality to set the event (task) name and options (such as event or associated event ID) for ETW events created by the hook based on the logrus.Entry fields. Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com> * PR: export and comment Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
- Loading branch information
Showing
2 changed files
with
117 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:build windows | ||
|
||
package etwlogrus | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/Microsoft/go-winio/pkg/etw" | ||
) | ||
|
||
// etw provider | ||
|
||
// WithNewETWProvider registers a new ETW provider and sets the hook to log using it. | ||
// The provider will be closed when the hook is closed. | ||
func WithNewETWProvider(n string) HookOpt { | ||
return func(h *Hook) error { | ||
provider, err := etw.NewProvider(n, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.provider = provider | ||
h.closeProvider = true | ||
return nil | ||
} | ||
} | ||
|
||
// WithExistingETWProvider configures the hook to use an existing ETW provider. | ||
// The provider will not be closed when the hook is closed. | ||
func WithExistingETWProvider(p *etw.Provider) HookOpt { | ||
return func(h *Hook) error { | ||
h.provider = p | ||
h.closeProvider = false | ||
return nil | ||
} | ||
} | ||
|
||
// WithGetName sets the ETW EventName of an event to the value returned by f | ||
// If the name is empty, the default event name will be used. | ||
func WithGetName(f func(*logrus.Entry) string) HookOpt { | ||
return func(h *Hook) error { | ||
h.getName = f | ||
return nil | ||
} | ||
} | ||
|
||
// WithAdditionalEventOpts allows additional ETW event properties (keywords, tags, etc.) to be specified | ||
func WithEventOpts(f func(*logrus.Entry) []etw.EventOpt) HookOpt { | ||
return func(h *Hook) error { | ||
h.getEventsOpts = f | ||
return nil | ||
} | ||
} |