2021-12-15 04:49:03 -05:00
package logging
import (
` golang.org/x/sys/windows/svc/eventlog `
)
2022-01-16 02:05:42 -05:00
// WinLogger is used for logging to the Windows Event Log. These entries are viewable in the Event Viewer application, under "Windows Logs > Application".
2021-12-15 04:49:03 -05:00
type WinLogger struct {
2022-01-16 02:05:42 -05:00
/ *
EnableDebug indicates if the debug filter should be disabled ( true ) or if the filter should be enabled ( false ) .
This prevents potential data leak of sensitive information , as some loggers ( e . g . FileLogger ) will otherwise write all messages .
* /
2021-12-15 04:49:03 -05:00
EnableDebug bool
2022-01-16 02:05:42 -05:00
/ *
Prefix is used as the Event Log "Source" . It ' s named as Prefix to retain compatability with methods in the Logger interface .
* /
Prefix string
/ *
Executable is used as the path for the executable implementing this logger .
If non - empty , it enables the "service" mode of Event Log ( intended for "installed" software that ' s expected
to exist as a specific path reliably ) .
It can be a file within the PATHs or an absolute / relative path ; an attempt to resolve the actual path will be made . If this fails or the file
does not exist , an error will be raised .
* /
Executable string
/ *
ExpandKey is only used if Executable is non - empty and valid and / or ForceService is true .
If true , the WinLogger will be installed / registered with the REG_EXPAND_SZ mode - otherwise it will be installed as REG_SZ .
See the definition for the two at https : //docs.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types for further details.
If you ' re unsure which you want , it ' s probably REG_SZ ( WinLogger . ExpandKey == false ) , which is the default .
* /
ExpandKey bool
/ *
ForceService , if true , will enforce WinLogger to be used as if Executable is populated and valid ( it will use os . Args [ 0 ] as the Executable path ) .
If Executable is empty but ForceService is true and os . Args [ 0 ] is empty or invalid ( not a real path , etc . ) , an error will be raised .
* /
ForceService bool
// RemoveOnClose should be true if the logger should be removed/unregistered from the Registry upon calling WinLogger.Shutdown.
RemoveOnClose bool
// elog is the actual writer to the Event Log.
elog * eventlog . Log
2022-01-16 06:55:29 -05:00
// EIDs is used to look up what event ID to use when writing to a WinLogger.elog.
EIDs * WinEventID
2021-12-15 04:49:03 -05:00
}
2022-01-16 02:05:42 -05:00
/ *
WinEventID is a collection of Event IDs to use for a WinLogger .
Because Event Log only supports three entry types ( informational , warning , or error ) ,
these event IDs allow you to filter the messages in a slightly more granular way . They map to their corresponding method name / Logger level .
However , this means that a WinLogger does not support custom event IDs ( and thus you cannot assign individual event IDs to specific errors ) .
This is the price of convenience .
An additional method set may be added in the future to support this , but this is currently an unplanned feature .
Event IDs * must * be between the constants EIDMin and EIDMax ( inclusive ) unless the WinLogger is used in "service" mode
( see WinLogger . Executable and WinLogger . ForceService ) .
If you need recommended defaults , you may want to use the Event * constants ( e . g . EventAlert , EventDebug , etc . )
or even use the pre - populated DefaultEventID ( which is assigned the above Event * constants ) .
* /
2021-12-15 04:49:03 -05:00
type WinEventID struct {
Alert ,
Crit ,
Debug ,
Emerg ,
Err ,
Info ,
Notice ,
Warning uint32
}