2021-02-26 15:52:29 -05:00
|
|
|
package logging
|
|
|
|
|
2021-02-26 20:27:35 -05:00
|
|
|
import (
|
2021-02-27 00:51:58 -05:00
|
|
|
"fmt"
|
2022-09-07 06:03:28 -04:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2021-02-26 20:27:35 -05:00
|
|
|
)
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
/*
|
2022-09-07 06:03:28 -04:00
|
|
|
Setup sets up/configures a StdLogger and prepares it for use.
|
|
|
|
err will always be nil; it's there for interface-compat.
|
2022-01-16 02:05:42 -05:00
|
|
|
*/
|
|
|
|
func (l *StdLogger) Setup() (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
2022-01-16 06:55:29 -05:00
|
|
|
var multi io.Writer
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
// 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)
|
|
|
|
}
|
2022-01-16 06:55:29 -05:00
|
|
|
// (stdlib).log.std is returned by log.Default(), which uses os.Stderr but we have flags for that.
|
|
|
|
// https://stackoverflow.com/a/36719588/733214
|
|
|
|
switch {
|
|
|
|
case l.EnableStdErr && l.EnableStdOut:
|
|
|
|
multi = io.MultiWriter(os.Stdout, os.Stderr)
|
|
|
|
case l.EnableStdErr:
|
|
|
|
multi = os.Stderr
|
|
|
|
case l.EnableStdOut:
|
|
|
|
multi = os.Stdout
|
|
|
|
default:
|
|
|
|
multi = nil
|
|
|
|
}
|
|
|
|
if multi != nil {
|
|
|
|
l.Logger = log.New(multi, l.Prefix, l.LogFlags)
|
|
|
|
} else {
|
|
|
|
// This honestly should throw an error.
|
|
|
|
l.Logger = &log.Logger{}
|
|
|
|
l.Logger.SetPrefix(l.Prefix)
|
|
|
|
l.Logger.SetFlags(l.LogFlags)
|
|
|
|
}
|
2022-01-16 02:05:42 -05:00
|
|
|
|
|
|
|
return
|
2021-02-26 20:27:35 -05:00
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
/*
|
2022-09-07 06:03:28 -04:00
|
|
|
Shutdown cleanly shuts down a StdLogger.
|
|
|
|
err will always be nil; it's there for interface-compat.
|
2022-01-16 02:05:42 -05:00
|
|
|
*/
|
|
|
|
func (l *StdLogger) Shutdown() (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
// NOOP
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
return
|
2021-02-26 20:27:35 -05:00
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
/*
|
2022-09-07 06:03:28 -04:00
|
|
|
GetPrefix returns the prefix used by this StdLogger.
|
|
|
|
err will always be nil; it's there for interface-compat.
|
2022-01-16 02:05:42 -05:00
|
|
|
*/
|
|
|
|
func (l *StdLogger) GetPrefix() (prefix string, err error) {
|
2022-01-05 05:15:38 -05:00
|
|
|
|
|
|
|
prefix = l.Prefix
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-09-07 06:03:28 -04:00
|
|
|
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.
|
2022-01-05 05:15:38 -05:00
|
|
|
*/
|
2022-01-16 02:05:42 -05:00
|
|
|
func (l *StdLogger) DoDebug(d bool) (err error) {
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
l.EnableDebug = d
|
2022-01-16 02:05:42 -05:00
|
|
|
|
|
|
|
return
|
2021-02-26 15:52:29 -05:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:03:28 -04:00
|
|
|
// GetDebug returns the debug status of this StdLogger.
|
|
|
|
func (l *StdLogger) GetDebug() (d bool) {
|
|
|
|
|
|
|
|
d = l.EnableDebug
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-16 02:05:42 -05:00
|
|
|
/*
|
2022-09-07 06:03:28 -04:00
|
|
|
SetPrefix sets the prefix for this StdLogger.
|
|
|
|
err will always be nil; it's there for interface-compat.
|
2022-01-16 02:05:42 -05:00
|
|
|
*/
|
|
|
|
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
|
2021-02-26 15:52:29 -05:00
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Alert writes an ALERT-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Alert(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "ALERT")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Crit writes an CRITICAL-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Crit(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "CRITICAL")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Debug writes a DEBUG-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Debug(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
if !l.EnableDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "DEBUG")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Emerg writes an EMERGENCY-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Emerg(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "EMERGENCY")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Err writes an ERROR-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Err(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "ERROR")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Info writes an INFO-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Info(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "INFO")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Notice writes a NOTICE-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Notice(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "NOTICE")
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// Warning writes a WARNING/WARN-level message to this StdLogger.
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *StdLogger) Warning(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
l.renderWrite(msg, "WARNING")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:15:38 -05:00
|
|
|
// renderWrite prepares/formats a log message to be written to this StdLogger.
|
2021-02-26 20:27:35 -05:00
|
|
|
func (l *StdLogger) renderWrite(msg, prio string) {
|
|
|
|
|
|
|
|
s := fmt.Sprintf("[%v] %v", prio, msg)
|
|
|
|
|
|
|
|
l.Logger.Println(s)
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|