go_goutils/logging/funcs_multilogger_mgr_windows.go

103 lines
2.9 KiB
Go
Raw Normal View History

2022-01-05 05:15:38 -05:00
package logging
2022-01-06 04:16:44 -05:00
import (
`github.com/google/uuid`
)
2022-01-05 05:15:38 -05:00
/*
AddDefaultLogger adds a default Logger (as would be determined by GetLogger) to a MultiLogger.
identifier is a string to use to identify the added Logger in MultiLogger.Loggers.
If empty, one will be automatically generated.
A pointer to a WinEventID struct may be specified for eventIDs to map extended logging levels (as Windows only supports three levels natively).
If it is nil, a default one (DefaultEventID) will be used.
logPaths is an (optional) list of strings to use as paths to test for writing. If the file can be created/written to,
it will be used (assuming you have no higher-level loggers available).
See the documentation for GetLogger for details on other arguments.
2022-01-05 05:15:38 -05:00
Only the first logPaths entry that "works" will be used, later entries will be ignored.
Currently this will almost always return a WinLogger.
*/
func (m *MultiLogger) AddDefaultLogger(identifier string, eventIDs *WinEventID, logFlags int, logPaths ...string) (err error) {
2022-01-05 05:15:38 -05:00
var l Logger
var exists bool
if identifier == "" {
identifier = uuid.New().String()
}
if _, exists = m.Loggers[identifier]; exists {
err = ErrExistingLogger
return
}
2022-01-06 04:16:44 -05:00
if logPaths != nil {
l, err = GetLogger(m.EnableDebug, m.Prefix, eventIDs, logFlags, logPaths...)
2022-01-06 04:16:44 -05:00
} else {
l, err = GetLogger(m.EnableDebug, m.Prefix, eventIDs, logFlags)
2022-01-06 04:16:44 -05:00
}
if err != nil {
2022-01-05 05:15:38 -05:00
return
}
m.Loggers[identifier] = l
return
}
/*
AddWinLogger adds a WinLogger to a MultiLogger. Note that this is a VERY generalized interface to the Windows Event Log.
If you require more robust logging capabilities (e.g. custom event IDs per uniquely identifiable event),
you will want to set up your own logger (golang.org/x/sys/windows/svc/eventlog).
identifier is a string to use to identify the added WinLogger in MultiLogger.Loggers.
If empty, one will be automatically generated.
A blank source will return an error as it's used as the source name. Other functions, struct fields, etc. will refer to this as the "prefix".
A pointer to a WinEventID struct may be specified for eventIDs to map extended logging levels (as Windows only supports three levels natively).
If it is nil, a default one (DefaultEventID) will be used.
See GetLogger for details.
*/
2022-01-16 06:55:29 -05:00
func (m *MultiLogger) AddWinLogger(identifier string, eventIDs *WinEventID) (err error) {
2022-01-05 05:15:38 -05:00
var exists bool
var prefix string
2022-01-05 05:15:38 -05:00
if identifier == "" {
identifier = uuid.New().String()
}
if _, exists = m.Loggers[identifier]; exists {
err = ErrExistingLogger
return
}
if eventIDs == nil {
eventIDs = DefaultEventID
}
m.Loggers[identifier] = &WinLogger{
2022-01-16 06:55:29 -05:00
Prefix: m.Prefix,
2022-01-05 05:15:38 -05:00
EnableDebug: m.EnableDebug,
2022-01-16 06:55:29 -05:00
EIDs: eventIDs,
2022-01-05 05:15:38 -05:00
}
if err = m.Loggers[identifier].Setup(); err != nil {
return
}
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
return
}
2022-01-05 05:15:38 -05:00
m.Loggers[identifier].Info("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
2022-01-05 05:15:38 -05:00
return
}