go_goutils/logging/funcs_multilogger_mgr.go
brent s 39e0a1fd43
Windows Event Log, error output
Adding more Event Log support, and modifying the loggers so they return
errors in their operational functions.
2022-01-16 02:05:42 -05:00

156 lines
3.6 KiB
Go

package logging
import (
`path`
`github.com/google/uuid`
`r00t2.io/sysutils/paths`
)
/*
GetMultiLogger returns a MultiLogger.
If you call GetLogger, you will only get a single ("best") logger your system supports.
If you want to log to multiple Logger destinations at once (or want to log to an explicit Logger type),
use GetMultiLogger.
Remember to add at least one Logger (e.g. MultiLogger.AddStdLogger), otherwise no entries will actually be logged.
If you want to modify e.g. if debug is enabled for a specific Logger, reference the Logger directly (e.g. MultiLogger.Loggers[identifier].SetDebug(false)).
*/
func GetMultiLogger(enableDebug bool, prefix string) (m *MultiLogger) {
m = &MultiLogger{
EnableDebug: enableDebug,
Prefix: logPrefix,
Loggers: make(map[string]Logger),
}
if prefix != "\x00" {
m.Prefix = prefix
}
return
}
/*
AddStdLogger adds a StdLogger to a MultiLogger.
identifier is a string to use to identify the added StdLogger in MultiLogger.Loggers.
If empty, one will be automatically generated.
See GetLogger's logConfigFlags argument and StdLogger.LogFlags for details on logFlags.
*/
func (m *MultiLogger) AddStdLogger(identifier string, logFlags int) (err error) {
var exists bool
var prefix string
if identifier == "" {
identifier = uuid.New().String()
}
if _, exists = m.Loggers[identifier]; exists {
err = ErrExistingLogger
return
}
m.Loggers[identifier] = &StdLogger{
Logger: nil,
EnableDebug: m.EnableDebug,
Prefix: m.Prefix,
LogFlags: logFlags,
}
if err = m.Loggers[identifier].Setup(); err != nil {
return
}
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
return
}
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
return
}
/*
AddFileLogger adds a FileLogger to a MultiLogger.
identifier is a string to use to identify the added FileLogger in MultiLogger.Loggers.
If empty, one will be automatically generated.
logfilePath is a string for the path to the desired logfile.
*/
func (m *MultiLogger) AddFileLogger(identifier string, enableStdOut, enableStdErr bool, logFlags int, logfilePath string) (err error) {
var exists bool
var success bool
var dirPath string
var prefix string
if identifier == "" {
identifier = uuid.New().String()
}
if _, exists = m.Loggers[identifier]; exists {
err = ErrExistingLogger
return
}
if exists, err = paths.RealPathExists(&logfilePath); err != nil {
return
} else if !exists {
if success, err = testOpen(logfilePath); err != nil {
return
} else if !success {
dirPath = path.Dir(logfilePath)
if err = paths.MakeDirIfNotExist(dirPath); err != nil {
return
}
if success, err = testOpen(dirPath); err != nil {
return
} else if !success {
err = ErrInvalidFile
return
}
}
}
m.Loggers[identifier] = &FileLogger{
StdLogger: StdLogger{
Logger: nil,
EnableDebug: m.EnableDebug,
Prefix: m.Prefix,
LogFlags: logFlags,
},
Path: logfilePath,
EnableStdOut: enableStdOut,
EnableStdErr: enableStdErr,
}
if err = m.Loggers[identifier].Setup(); err != nil {
return
}
if prefix, err = m.Loggers[identifier].GetPrefix(); err != nil {
return
}
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
return
}
// RemoveLogger will let you remove a Logger from MultiLogger.Loggers.
func (m *MultiLogger) RemoveLogger(identifier string) (err error) {
var exists bool
if _, exists = m.Loggers[identifier]; !exists {
err = ErrNoEntry
return
}
delete(m.Loggers, identifier)
return
}