cc-backend/log/log.go

128 lines
2.7 KiB
Go
Raw Normal View History

2022-01-27 10:35:26 +01:00
// Provides a simple way of logging with different levels.
// Time/Data are not logged on purpose because systemd adds
// them for us.
2022-01-27 13:11:48 +01:00
//
// Uses these prefixes: https://www.freedesktop.org/software/systemd/man/sd-daemon.html
2022-01-27 10:35:26 +01:00
package log
import (
"fmt"
"io"
"os"
)
2022-01-31 15:14:37 +01:00
var (
DebugWriter io.Writer = os.Stderr
InfoWriter io.Writer = os.Stderr
WarnWriter io.Writer = os.Stderr
ErrorWriter io.Writer = os.Stderr
)
var (
DebugPrefix string = "<7>[DEBUG]"
InfoPrefix string = "<6>[INFO]"
WarnPrefix string = "<4>[WARNING]"
ErrPrefix string = "<3>[ERROR]"
FatalPrefix string = "<3>[FATAL]"
)
2022-01-27 10:35:26 +01:00
func init() {
if lvl, ok := os.LookupEnv("LOGLEVEL"); ok {
switch lvl {
case "err", "fatal":
WarnWriter = io.Discard
fallthrough
case "warn":
InfoWriter = io.Discard
fallthrough
case "info":
DebugWriter = io.Discard
case "debug":
// Nothing to do...
default:
Warnf("environment variable LOGLEVEL has invalid value %#v", lvl)
}
}
}
func Debug(v ...interface{}) {
if DebugWriter != io.Discard {
2022-01-31 15:14:37 +01:00
v = append([]interface{}{DebugPrefix}, v...)
2022-01-27 10:35:26 +01:00
fmt.Fprintln(DebugWriter, v...)
}
}
func Info(v ...interface{}) {
if InfoWriter != io.Discard {
2022-01-31 15:14:37 +01:00
v = append([]interface{}{InfoPrefix}, v...)
2022-01-27 10:35:26 +01:00
fmt.Fprintln(InfoWriter, v...)
}
}
func Print(v ...interface{}) {
Info(v...)
}
func Warn(v ...interface{}) {
if WarnWriter != io.Discard {
2022-01-31 15:14:37 +01:00
v = append([]interface{}{WarnPrefix}, v...)
2022-01-27 10:35:26 +01:00
fmt.Fprintln(WarnWriter, v...)
}
}
func Error(v ...interface{}) {
if ErrorWriter != io.Discard {
2022-01-31 15:14:37 +01:00
v = append([]interface{}{ErrPrefix}, v...)
2022-01-27 10:35:26 +01:00
fmt.Fprintln(ErrorWriter, v...)
}
}
func Fatal(v ...interface{}) {
if ErrorWriter != io.Discard {
2022-01-31 15:14:37 +01:00
v = append([]interface{}{FatalPrefix}, v...)
2022-01-27 10:35:26 +01:00
fmt.Fprintln(ErrorWriter, v...)
}
2022-01-27 13:11:48 +01:00
os.Exit(1)
2022-01-27 10:35:26 +01:00
}
func Debugf(format string, v ...interface{}) {
if DebugWriter != io.Discard {
2022-01-31 15:14:37 +01:00
fmt.Fprintf(DebugWriter, DebugPrefix+" "+format+"\n", v...)
2022-01-27 10:35:26 +01:00
}
}
func Infof(format string, v ...interface{}) {
if InfoWriter != io.Discard {
2022-01-31 15:14:37 +01:00
fmt.Fprintf(InfoWriter, InfoPrefix+" "+format+"\n", v...)
2022-01-27 10:35:26 +01:00
}
}
func Printf(format string, v ...interface{}) {
Infof(format, v...)
}
2022-01-31 15:14:37 +01:00
func Finfof(w io.Writer, format string, v ...interface{}) {
if w != io.Discard {
fmt.Fprintf(InfoWriter, InfoPrefix+" "+format+"\n", v...)
}
}
2022-01-27 10:35:26 +01:00
func Warnf(format string, v ...interface{}) {
if WarnWriter != io.Discard {
2022-01-31 15:14:37 +01:00
fmt.Fprintf(WarnWriter, WarnPrefix+" "+format+"\n", v...)
2022-01-27 10:35:26 +01:00
}
}
func Errorf(format string, v ...interface{}) {
if ErrorWriter != io.Discard {
2022-01-31 15:14:37 +01:00
fmt.Fprintf(ErrorWriter, ErrPrefix+" "+format+"\n", v...)
2022-01-27 10:35:26 +01:00
}
}
func Fatalf(format string, v ...interface{}) {
if ErrorWriter != io.Discard {
2022-01-31 15:14:37 +01:00
fmt.Fprintf(ErrorWriter, FatalPrefix+" "+format+"\n", v...)
2022-01-27 10:35:26 +01:00
}
2022-01-27 13:11:48 +01:00
os.Exit(1)
2022-01-27 10:35:26 +01:00
}