go_goutils/logging/funcs_std.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

218 lines
3.8 KiB
Go

package logging
import (
"fmt"
`log`
`os`
`strings`
)
/*
Setup sets up/configures a StdLogger and prepares it for use.
err will always be nil; it's there for interface-compat.
*/
func (l *StdLogger) Setup() (err error) {
// This uses a shared handle across the import. We don't want that.
// l.Logger = log.Default()
if l.Prefix != "" {
l.Prefix = strings.TrimRight(l.Prefix, " ") + " "
// l.Logger.SetPrefix(l.Prefix)
}
// (stdlib).log.std is returned by log.Default(), which uses os.Stderr.
l.Logger = log.New(os.Stderr, l.Prefix, l.LogFlags)
return
}
/*
Shutdown cleanly shuts down a StdLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *StdLogger) Shutdown() (err error) {
// NOOP
return
}
/*
GetPrefix returns the prefix used by this StdLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *StdLogger) GetPrefix() (prefix string, err error) {
prefix = l.Prefix
return
}
/*
DoDebug sets the debug state of this StdLogger.
Note that this merely acts as a *safety filter* for debug messages to avoid sensitive information being written to the log.
err will always be nil; it's there for interface-compat.
*/
func (l *StdLogger) DoDebug(d bool) (err error) {
l.EnableDebug = d
return
}
/*
SetPrefix sets the prefix for this StdLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *StdLogger) SetPrefix(prefix string) (err error) {
l.Prefix = prefix
if prefix != "" {
l.Prefix = strings.TrimRight(l.Prefix, " ") + " "
}
l.Logger.SetPrefix(l.Prefix)
return
}
// Alert writes an ALERT-level message to this StdLogger.
func (l *StdLogger) Alert(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "ALERT")
return
}
// Crit writes an CRITICAL-level message to this StdLogger.
func (l *StdLogger) Crit(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "CRITICAL")
return
}
// Debug writes a DEBUG-level message to this StdLogger.
func (l *StdLogger) Debug(s string, v ...interface{}) (err error) {
if !l.EnableDebug {
return
}
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "DEBUG")
return
}
// Emerg writes an EMERGENCY-level message to this StdLogger.
func (l *StdLogger) Emerg(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "EMERGENCY")
return
}
// Err writes an ERROR-level message to this StdLogger.
func (l *StdLogger) Err(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "ERROR")
return
}
// Info writes an INFO-level message to this StdLogger.
func (l *StdLogger) Info(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "INFO")
return
}
// Notice writes a NOTICE-level message to this StdLogger.
func (l *StdLogger) Notice(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "NOTICE")
return
}
// Warning writes a WARNING/WARN-level message to this StdLogger.
func (l *StdLogger) Warning(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, "WARNING")
return
}
// renderWrite prepares/formats a log message to be written to this StdLogger.
func (l *StdLogger) renderWrite(msg, prio string) {
s := fmt.Sprintf("[%v] %v", prio, msg)
l.Logger.Println(s)
return
}