go_goutils/logging/funcs_file.go

196 lines
3.4 KiB
Go

package logging
import (
"fmt"
"io"
"log"
"os"
)
// Setup sets up/configures a FileLogger and prepares it for use.
func (l *FileLogger) Setup() {
var err error
l.Logger = log.Default()
l.Logger.SetPrefix(l.Prefix)
if l.writer, err = os.OpenFile(l.Path, appendFlags, logPerm); err != nil {
log.Panicf("could not open log file \"%v\" for writing: %v\n", l.Path, err)
}
// https://stackoverflow.com/a/36719588/733214
multi := io.MultiWriter(os.Stdout, l.writer)
l.Logger.SetOutput(multi)
}
// Shutdown cleanly shuts down a FileLogger.
func (l *FileLogger) Shutdown() {
var err error
if err = l.writer.Close(); err != nil {
log.Panicf("could not close log file \"%v\": %v\n", l.Path, err)
}
}
// GetPrefix returns the prefix used by this FileLogger.
func (l *FileLogger) GetPrefix() string {
return l.Prefix
}
/*
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.
*/
func (l *FileLogger) DoDebug(d bool) {
l.EnableDebug = d
}
// SetPrefix sets the prefix for this FileLogger.
func (l *FileLogger) SetPrefix(prefix string) {
l.Prefix = prefix
l.Logger.SetPrefix(prefix)
}
// 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
}
// 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
}