Simplify log wrapper implementation

This commit is contained in:
Jan Eitzinger 2023-02-09 14:14:58 +01:00
parent 3c6b92e9c2
commit c123a87ece
2 changed files with 70 additions and 190 deletions

View File

@ -88,8 +88,7 @@ func main() {
} }
// Apply config flags for pkg/log // Apply config flags for pkg/log
log.SetLogLevel(flagLogLevel) log.Init(flagLogLevel, flagLogDateTime)
log.SetLogDateTime(flagLogDateTime)
// See https://github.com/google/gops (Runtime overhead is almost zero) // See https://github.com/google/gops (Runtime overhead is almost zero)
if flagGops { if flagGops {
@ -297,7 +296,7 @@ func main() {
if flagDev { if flagDev {
r.Handle("/playground", playground.Handler("GraphQL playground", "/query")) r.Handle("/playground", playground.Handler("GraphQL playground", "/query"))
r.PathPrefix("/swagger/").Handler(httpSwagger.Handler( r.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
httpSwagger.URL("http://" + config.Keys.Addr + "/swagger/doc.json"))).Methods(http.MethodGet) httpSwagger.URL("http://" + config.Keys.Addr + "/swagger/doc.json"))).Methods(http.MethodGet)
} }
secured.Handle("/query", graphQLEndpoint) secured.Handle("/query", graphQLEndpoint)

View File

@ -9,7 +9,6 @@ import (
"io" "io"
"log" "log"
"os" "os"
"time"
) )
// Provides a simple way of logging with different levels. // Provides a simple way of logging with different levels.
@ -18,9 +17,6 @@ import (
// //
// Uses these prefixes: https://www.freedesktop.org/software/systemd/man/sd-daemon.html // Uses these prefixes: https://www.freedesktop.org/software/systemd/man/sd-daemon.html
var logDateTime bool
var logLevel string
var ( var (
DebugWriter io.Writer = os.Stderr DebugWriter io.Writer = os.Stderr
NoteWriter io.Writer = os.Stderr NoteWriter io.Writer = os.Stderr
@ -40,54 +36,57 @@ var (
) )
var ( var (
// No Time/Date
DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, 0) DebugLog *log.Logger = log.New(DebugWriter, DebugPrefix, 0)
InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, 0) InfoLog *log.Logger = log.New(InfoWriter, InfoPrefix, 0)
NoteLog *log.Logger = log.New(NoteWriter, NotePrefix, log.Lshortfile) NoteLog *log.Logger = log.New(NoteWriter, NotePrefix, log.Lshortfile)
WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.Lshortfile) WarnLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.Lshortfile)
ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.Llongfile) ErrLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.Llongfile)
CritLog *log.Logger = log.New(CritWriter, CritPrefix, log.Llongfile) CritLog *log.Logger = log.New(CritWriter, CritPrefix, log.Llongfile)
// Log Time/Date
DebugTimeLog *log.Logger = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
InfoTimeLog *log.Logger = log.New(InfoWriter, InfoPrefix, log.LstdFlags)
NoteTimeLog *log.Logger = log.New(NoteWriter, NotePrefix, log.LstdFlags|log.Lshortfile)
WarnTimeLog *log.Logger = log.New(WarnWriter, WarnPrefix, log.LstdFlags|log.Lshortfile)
ErrTimeLog *log.Logger = log.New(ErrWriter, ErrPrefix, log.LstdFlags|log.Llongfile)
CritTimeLog *log.Logger = log.New(CritWriter, CritPrefix, log.LstdFlags|log.Llongfile)
) )
/* CONFIG */ /* CONFIG */
func SetLogLevel(lvl string) { func Init(lvl string, logdate bool) {
// fmt.Printf("pkg/log: Set LOGLEVEL -> %s\n", lvl) // fmt.Printf("pkg/log: Set LOGLEVEL -> %s\n", lvl)
switch lvl { switch lvl {
case "crit": case "crit":
ErrWriter = io.Discard ErrWriter = io.Discard
fallthrough fallthrough
case "err", "fatal": case "err", "fatal":
WarnWriter = io.Discard WarnWriter = io.Discard
fallthrough fallthrough
case "warn": case "warn":
InfoWriter = io.Discard InfoWriter = io.Discard
fallthrough fallthrough
case "notice": case "notice":
NoteWriter = io.Discard NoteWriter = io.Discard
fallthrough fallthrough
case "info": case "info":
DebugWriter = io.Discard DebugWriter = io.Discard
break break
case "debug": case "debug":
// Nothing to do... // Nothing to do...
break break
default: default:
fmt.Printf("pkg/log: Flag 'loglevel' has invalid value %#v\npkg/log: Will use default loglevel 'debug'\n", lvl) fmt.Printf("pkg/log: Flag 'loglevel' has invalid value %#v\npkg/log: Will use default loglevel 'debug'\n", lvl)
SetLogLevel("debug") //SetLogLevel("debug")
} }
}
func SetLogDateTime(logdate bool) { if logdate == false {
//fmt.Printf("pkg/log: Set DATEBOOL -> %v\n", logdate) DebugLog = log.New(DebugWriter, DebugPrefix, 0)
logDateTime = logdate InfoLog = log.New(InfoWriter, InfoPrefix, 0)
NoteLog = log.New(NoteWriter, NotePrefix, log.Lshortfile)
WarnLog = log.New(WarnWriter, WarnPrefix, log.Lshortfile)
ErrLog = log.New(ErrWriter, ErrPrefix, log.Llongfile)
CritLog = log.New(CritWriter, CritPrefix, log.Llongfile)
} else {
DebugLog = log.New(DebugWriter, DebugPrefix, log.LstdFlags)
InfoLog = log.New(InfoWriter, InfoPrefix, log.LstdFlags)
NoteLog = log.New(NoteWriter, NotePrefix, 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)
}
} }
/* PRINT */ /* PRINT */
@ -104,97 +103,38 @@ func Print(v ...interface{}) {
} }
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
if DebugWriter != io.Discard { DebugLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
DebugTimeLog.Output(2, out)
} else {
DebugLog.Output(2, out)
}
}
} }
func Info(v ...interface{}) { func Info(v ...interface{}) {
if InfoWriter != io.Discard { InfoLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
InfoTimeLog.Output(2, out)
} else {
InfoLog.Output(2, out)
}
}
} }
func Note(v ...interface{}) { func Note(v ...interface{}) {
if NoteWriter != io.Discard { NoteLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
NoteTimeLog.Output(2, out)
} else {
NoteLog.Output(2, out)
}
}
} }
func Warn(v ...interface{}) { func Warn(v ...interface{}) {
if WarnWriter != io.Discard { WarnLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
WarnTimeLog.Output(2, out)
} else {
WarnLog.Output(2, out)
}
}
} }
func Error(v ...interface{}) { func Error(v ...interface{}) {
if ErrWriter != io.Discard { ErrLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
ErrTimeLog.Output(2, out)
} else {
ErrLog.Output(2, out)
}
}
} }
// Writes panic stacktrace, but keeps application alive // Writes panic stacktrace, but keeps application alive
func Panic(v ...interface{}) { func Panic(v ...interface{}) {
if ErrWriter != io.Discard { ErrLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
ErrTimeLog.Output(2, out)
} else {
ErrLog.Output(2, out)
}
}
panic("Panic triggered ...") panic("Panic triggered ...")
} }
func Crit(v ...interface{}) { func Crit(v ...interface{}) {
if CritWriter != io.Discard { CritLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
CritTimeLog.Output(2, out)
} else {
CritLog.Output(2, out)
}
}
} }
// Writes critical log, stops application // Writes critical log, stops application
func Fatal(v ...interface{}) { func Fatal(v ...interface{}) {
if CritWriter != io.Discard { CritLog.Output(2, printStr(v...))
out := printStr(v...)
if logDateTime {
CritTimeLog.Output(2, out)
} else {
CritLog.Output(2, out)
}
}
os.Exit(1) os.Exit(1)
} }
@ -212,109 +152,50 @@ func Printf(format string, v ...interface{}) {
} }
func Debugf(format string, v ...interface{}) { func Debugf(format string, v ...interface{}) {
if DebugWriter != io.Discard { DebugLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
DebugTimeLog.Output(2, out)
} else {
DebugLog.Output(2, out)
}
}
} }
func Infof(format string, v ...interface{}) { func Infof(format string, v ...interface{}) {
if InfoWriter != io.Discard { InfoLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
InfoTimeLog.Output(2, out)
} else {
InfoLog.Output(2, out)
}
}
} }
func Notef(format string, v ...interface{}) { func Notef(format string, v ...interface{}) {
if NoteWriter != io.Discard { NoteLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
NoteTimeLog.Output(2, out)
} else {
NoteLog.Output(2, out)
}
}
} }
func Warnf(format string, v ...interface{}) { func Warnf(format string, v ...interface{}) {
if WarnWriter != io.Discard { WarnLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
WarnTimeLog.Output(2, out)
} else {
WarnLog.Output(2, out)
}
}
} }
func Errorf(format string, v ...interface{}) { func Errorf(format string, v ...interface{}) {
if ErrWriter != io.Discard { ErrLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
ErrTimeLog.Output(2, out)
} else {
ErrLog.Output(2, out)
}
}
} }
// Writes panic stacktrace, but keeps application alive // Writes panic stacktrace, but keeps application alive
func Panicf(format string, v ...interface{}) { func Panicf(format string, v ...interface{}) {
if ErrWriter != io.Discard { ErrLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
ErrTimeLog.Output(2, out)
} else {
ErrLog.Output(2, out)
}
}
panic("Panic triggered ...") panic("Panic triggered ...")
} }
func Critf(format string, v ...interface{}) { func Critf(format string, v ...interface{}) {
if CritWriter != io.Discard { CritLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
CritTimeLog.Output(2, out)
} else {
CritLog.Output(2, out)
}
}
} }
// Writes crit log, stops application // Writes crit log, stops application
func Fatalf(format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) {
if CritWriter != io.Discard { CritLog.Output(2, printStr(v...))
out := printfStr(format, v...)
if logDateTime {
CritTimeLog.Output(2, out)
} else {
CritLog.Output(2, out)
}
}
os.Exit(1) os.Exit(1)
} }
/* SPECIAL */ /* SPECIAL */
func Finfof(w io.Writer, format string, v ...interface{}) { // func Finfof(w io.Writer, format string, v ...interface{}) {
if w != io.Discard { // if w != io.Discard {
if logDateTime { // if logDateTime {
currentTime := time.Now() // currentTime := time.Now()
fmt.Fprintf(InfoWriter, currentTime.String()+InfoPrefix+format+"\n", v...) // fmt.Fprintf(InfoWriter, currentTime.String()+InfoPrefix+format+"\n", v...)
} else { // } else {
fmt.Fprintf(InfoWriter, InfoPrefix+format+"\n", v...) // fmt.Fprintf(InfoWriter, InfoPrefix+format+"\n", v...)
} // }
} // }
} // }