2022-01-05 05:15:38 -05:00
|
|
|
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,
|
2022-01-16 02:05:42 -05:00
|
|
|
Prefix: logPrefix,
|
2022-01-05 05:15:38 -05:00
|
|
|
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.
|
2022-01-16 02:05:42 -05:00
|
|
|
|
2022-01-16 06:55:29 -05:00
|
|
|
enableStdOut indicates that messages should be logged to STDOUT;
|
|
|
|
it is *strongly encouraged* to set at least one of enableStdOut or enableStdErr to true.
|
|
|
|
|
|
|
|
enableStdErr indicates that messages should be logged to STDERR;
|
|
|
|
it is *strongly encouraged* to set at least one of enableStdErr or enableStdOut to true.
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
See GetLogger's logConfigFlags argument and StdLogger.LogFlags for details on logFlags.
|
2022-01-05 05:15:38 -05:00
|
|
|
*/
|
2022-01-16 06:55:29 -05:00
|
|
|
func (m *MultiLogger) AddStdLogger(identifier string, enableStdOut, enableStdErr bool, logFlags int) (err error) {
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
var exists bool
|
2022-01-16 02:05:42 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Loggers[identifier] = &StdLogger{
|
2022-01-16 06:55:29 -05:00
|
|
|
Logger: nil,
|
|
|
|
EnableDebug: m.EnableDebug,
|
|
|
|
Prefix: m.Prefix,
|
|
|
|
LogFlags: logFlags,
|
|
|
|
EnableStdOut: enableStdOut,
|
|
|
|
EnableStdErr: enableStdErr,
|
2022-01-16 02:05:42 -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
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2022-01-16 06:55:29 -05:00
|
|
|
func (m *MultiLogger) AddFileLogger(identifier string, logFlags int, logfilePath string) (err error) {
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
var exists bool
|
|
|
|
var success bool
|
|
|
|
var dirPath string
|
2022-01-16 02:05:42 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
if exists, err = paths.RealPathExists(&logfilePath); err != nil {
|
|
|
|
return
|
|
|
|
} else if !exists {
|
2022-01-05 05:15:38 -05:00
|
|
|
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,
|
2022-01-16 02:05:42 -05:00
|
|
|
LogFlags: logFlags,
|
2022-01-05 05:15:38 -05:00
|
|
|
},
|
2022-01-16 06:55:29 -05:00
|
|
|
Path: logfilePath,
|
2022-01-16 02:05:42 -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
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
m.Loggers[identifier].Debug("logger initialized of type %T with prefix %v", m.Loggers[identifier], prefix)
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|