go_goutils/logging/funcs_file.go
brent saner e5191383a7
v1.7.0
ADDED:
* logging.Logger objects now are able to return a stdlib *log.Logger.
2024-06-19 18:57:26 -04:00

240 lines
4.2 KiB
Go

package logging
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"strings"
)
// Setup sets up/configures a FileLogger and prepares it for use.
func (l *FileLogger) 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)
}
if l.writer, err = os.OpenFile(l.Path, appendFlags, logPerm); err != nil {
return
}
l.Logger = log.New(l.writer, l.Prefix, l.LogFlags)
// l.Logger.SetOutput(multi)
return
}
// Shutdown cleanly shuts down a FileLogger.
func (l *FileLogger) Shutdown() (err error) {
if err = l.writer.Close(); err != nil {
if !errors.Is(err, fs.ErrClosed) {
return
}
err = nil
return err
}
return
}
/*
GetPrefix returns the prefix used by this FileLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *FileLogger) GetPrefix() (prefix string, err error) {
prefix = l.Prefix
return
}
/*
DoDebug sets the debug state of this FileLogger.
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 *FileLogger) DoDebug(d bool) (err error) {
l.EnableDebug = d
return
}
// GetDebug returns the debug status of this FileLogger.
func (l *FileLogger) GetDebug() (d bool) {
d = l.EnableDebug
return
}
/*
SetPrefix sets the prefix for this FileLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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 FileLogger.
func (l *FileLogger) 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
}
// ToLogger returns a stdlib log.Logger.
func (l *FileLogger) ToLogger(prio logPrio) (stdLibLog *log.Logger) {
stdLibLog = log.New(&logWriter{backend: l, prio: prio}, "", 0)
return
}
// renderWrite prepares/formats a log message to be written to this FileLogger.
func (l *FileLogger) renderWrite(msg, prio string) {
s := fmt.Sprintf("[%v] %v", prio, msg)
l.Logger.Println(s)
return
}