cc-backend/pkg/log/log.go

193 lines
4.8 KiB
Go
Raw Permalink Normal View History

// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
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"
)
// Provides a simple way of logging with different levels.
// Time/Date are not logged because systemd adds
// them for us (Default, can be changed by flag '--logdate true').
//
// Uses these prefixes: https://www.freedesktop.org/software/systemd/man/sd-daemon.html
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
CritWriter io.Writer = os.Stderr
2022-01-31 15:14:37 +01:00
)
var (
DebugPrefix string = "<7>[DEBUG] "
InfoPrefix string = "<6>[INFO] "
WarnPrefix string = "<4>[WARNING] "
ErrPrefix string = "<3>[ERROR] "
CritPrefix string = "<2>[CRITICAL] "
2022-02-08 11:04:21 +01:00
)
var (
DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, log.LstdFlags|log.Lshortfile)
WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile)
ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile)
CritLog *log.Logger = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile)
2022-01-31 15:14:37 +01:00
)
2022-01-27 10:35:26 +01:00
2023-06-09 15:02:22 +02:00
var loglevel string = "info"
/* CONFIG */
2023-02-09 14:14:58 +01:00
func Init(lvl string, logdate bool) {
2023-06-01 10:12:45 +02:00
switch lvl {
2023-02-09 14:14:58 +01:00
case "crit":
ErrWriter = io.Discard
fallthrough
case "err", "fatal":
WarnWriter = io.Discard
fallthrough
case "warn":
InfoWriter = io.Discard
fallthrough
case "info":
DebugWriter = io.Discard
case "debug":
// Nothing to do...
break
default:
fmt.Printf("pkg/log: Flag 'loglevel' has invalid value %#v\npkg/log: Will use default loglevel 'debug'\n", lvl)
//SetLogLevel("debug")
}
2023-02-09 15:43:38 +01:00
if !logdate {
2023-02-09 14:14:58 +01:00
DebugLog = log.New(DebugWriter, DebugPrefix, 0)
2023-02-15 11:50:51 +01:00
InfoLog = log.New(InfoWriter, InfoPrefix, log.Lshortfile)
2023-02-09 14:14:58 +01:00
WarnLog = log.New(WarnWriter, WarnPrefix, log.Lshortfile)
ErrLog = log.New(ErrWriter, ErrPrefix, log.Llongfile)
CritLog = log.New(CritWriter, CritPrefix, log.Llongfile)
2023-06-01 10:12:45 +02:00
} else {
DebugLog = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
InfoLog = log.New(InfoWriter, InfoPrefix, log.LstdFlags|log.Lshortfile)
WarnLog = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile)
ErrLog = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile)
CritLog = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile)
2022-01-27 10:35:26 +01:00
}
2023-06-09 15:02:22 +02:00
loglevel = lvl
2022-01-27 10:35:26 +01:00
}
/* PRINT */
// Private helper
func printStr(v ...interface{}) string {
return fmt.Sprint(v...)
}
// Uses Info() -> If errorpath required at some point:
// Will need own writer with 'Output(2, out)' to correctly render path
func Print(v ...interface{}) {
Info(v...)
}
2022-01-27 10:35:26 +01:00
func Debug(v ...interface{}) {
2023-02-09 14:14:58 +01:00
DebugLog.Output(2, printStr(v...))
2022-01-27 10:35:26 +01:00
}
func Info(v ...interface{}) {
2023-02-09 14:14:58 +01:00
InfoLog.Output(2, printStr(v...))
2022-01-27 10:35:26 +01:00
}
func Warn(v ...interface{}) {
2023-02-09 14:14:58 +01:00
WarnLog.Output(2, printStr(v...))
2022-01-27 10:35:26 +01:00
}
func Error(v ...interface{}) {
2023-02-09 14:14:58 +01:00
ErrLog.Output(2, printStr(v...))
2022-01-27 10:35:26 +01:00
}
// Writes panic stacktrace, but keeps application alive
func Panic(v ...interface{}) {
2023-02-09 14:14:58 +01:00
ErrLog.Output(2, printStr(v...))
panic("Panic triggered ...")
}
func Crit(v ...interface{}) {
2023-02-09 14:14:58 +01:00
CritLog.Output(2, printStr(v...))
}
// Writes critical log, stops application
func Fatal(v ...interface{}) {
2023-02-09 14:14:58 +01:00
CritLog.Output(2, printStr(v...))
os.Exit(1)
}
/* PRINT FORMAT*/
// Private helper
2023-02-09 16:24:29 +01:00
func printfStr(format string, v ...interface{}) string {
return fmt.Sprintf(format, v...)
}
// Uses Infof() -> If errorpath required at some point:
// Will need own writer with 'Output(2, out)' to correctly render path
func Printf(format string, v ...interface{}) {
Infof(format, v...)
}
2022-01-27 10:35:26 +01:00
func Debugf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
DebugLog.Output(2, printfStr(format, v...))
2022-01-27 10:35:26 +01:00
}
func Infof(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
InfoLog.Output(2, printfStr(format, v...))
2022-01-27 10:35:26 +01:00
}
func Warnf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
WarnLog.Output(2, printfStr(format, v...))
2022-01-27 10:35:26 +01:00
}
func Errorf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
ErrLog.Output(2, printfStr(format, v...))
2022-01-27 10:35:26 +01:00
}
// Writes panic stacktrace, but keeps application alive
func Panicf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
ErrLog.Output(2, printfStr(format, v...))
panic("Panic triggered ...")
}
func Critf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
CritLog.Output(2, printfStr(format, v...))
}
// Writes crit log, stops application
func Fatalf(format string, v ...interface{}) {
2023-02-09 16:24:29 +01:00
CritLog.Output(2, printfStr(format, v...))
os.Exit(1)
}
2023-06-09 15:02:22 +02:00
func Loglevel() string {
return loglevel
}
/* SPECIAL */
2023-02-09 14:14:58 +01:00
// func Finfof(w io.Writer, format string, v ...interface{}) {
// if w != io.Discard {
// if logDateTime {
// currentTime := time.Now()
// fmt.Fprintf(InfoWriter, currentTime.String()+InfoPrefix+format+"\n", v...)
// } else {
// fmt.Fprintf(InfoWriter, InfoPrefix+format+"\n", v...)
// }
// }
// }