cc-backend/pkg/log/log.go

126 lines
2.4 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"
2022-02-08 11:04:21 +01:00
"log"
2022-01-27 10:35:26 +01:00
"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
2022-02-08 11:04:21 +01:00
ErrWriter io.Writer = os.Stderr
2022-01-31 15:14:37 +01:00
)
var (
2022-02-08 11:04:21 +01:00
DebugPrefix string = "<7>[DEBUG] "
InfoPrefix string = "<6>[INFO] "
WarnPrefix string = "<4>[WARNING] "
ErrPrefix string = "<3>[ERROR] "
)
var (
DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, 0)
InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, 0)
WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, 0)
ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, 0)
2022-01-31 15:14:37 +01:00
)
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-02-08 11:04:21 +01:00
DebugLog.Print(v...)
2022-01-27 10:35:26 +01:00
}
}
func Info(v ...interface{}) {
if InfoWriter != io.Discard {
2022-02-08 11:04:21 +01:00
InfoLog.Print(v...)
2022-01-27 10:35:26 +01:00
}
}
func Print(v ...interface{}) {
Info(v...)
}
func Warn(v ...interface{}) {
if WarnWriter != io.Discard {
2022-02-08 11:04:21 +01:00
WarnLog.Print(v...)
2022-01-27 10:35:26 +01:00
}
}
func Error(v ...interface{}) {
2022-02-08 11:04:21 +01:00
if ErrWriter != io.Discard {
ErrLog.Print(v...)
2022-01-27 10:35:26 +01:00
}
}
func Fatal(v ...interface{}) {
2022-02-08 11:04:21 +01:00
Error(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-02-08 11:04:21 +01:00
DebugLog.Printf(format, v...)
2022-01-27 10:35:26 +01:00
}
}
func Infof(format string, v ...interface{}) {
if InfoWriter != io.Discard {
2022-02-08 11:04:21 +01:00
InfoLog.Printf(format, 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 {
2022-02-14 14:51:02 +01:00
fmt.Fprintf(InfoWriter, InfoPrefix+format+"\n", v...)
2022-01-31 15:14:37 +01:00
}
}
2022-01-27 10:35:26 +01:00
func Warnf(format string, v ...interface{}) {
if WarnWriter != io.Discard {
2022-02-08 11:04:21 +01:00
WarnLog.Printf(format, v...)
2022-01-27 10:35:26 +01:00
}
}
func Errorf(format string, v ...interface{}) {
2022-02-08 11:04:21 +01:00
if ErrWriter != io.Discard {
ErrLog.Printf(format, v...)
2022-01-27 10:35:26 +01:00
}
}
func Fatalf(format string, v ...interface{}) {
2022-02-08 11:04:21 +01:00
Errorf(format, v...)
2022-01-27 13:11:48 +01:00
os.Exit(1)
2022-01-27 10:35:26 +01:00
}