Update logging package to bring a bit of a speed improvement.

This commit is contained in:
2022-12-19 11:22:58 +00:00
parent 952749d7ff
commit bd0c5ec373
4 changed files with 47 additions and 407 deletions
+28 -33
View File
@@ -6,7 +6,6 @@ import (
"os"
"time"
"github.com/lukaszraczylo/pandati"
"github.com/rs/zerolog"
)
@@ -17,14 +16,14 @@ type LogConfig struct {
func NewLogger() *LogConfig {
log := new(LogConfig)
switch dll := os.Getenv("LOG_LEVEL"); dll {
case "debug":
dll := os.Getenv("LOG_LEVEL")
if dll == "debug" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "warn":
} else if dll == "warn" {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
} else if dll == "error" {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
zerolog.TimeFieldFormat = time.RFC3339
@@ -37,36 +36,12 @@ func NewLogger() *LogConfig {
return log
}
func (l *LogConfig) Critical(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stderr)
log(w.Fatal(), message, v...)
}
func (l *LogConfig) Error(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stderr)
log(w.Error(), message, v...)
}
func (l *LogConfig) Warning(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Warn(), message, v...)
}
func (l *LogConfig) Info(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Info(), message, v...)
}
func (l *LogConfig) Debug(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Debug(), message, v...)
}
func log(contextLog *zerolog.Event, message string, v ...map[string]interface{}) {
func (l *LogConfig) Log(level zerolog.Level, message string, v ...map[string]interface{}) {
contextLog := l.Logger.WithLevel(level)
if len(v) > 0 {
for _, m := range v {
for k, v := range m {
if !pandati.IsZero(v) && !pandati.IsZero(k) {
if v != nil && k != "" {
contextLog.Str(k, fmt.Sprintf("%v", v))
}
}
@@ -75,3 +50,23 @@ func log(contextLog *zerolog.Event, message string, v ...map[string]interface{})
// contextLog.Str("_stack_trace", pandati.Trace())
contextLog.Msg(message)
}
func (l *LogConfig) Critical(message string, v ...map[string]interface{}) {
l.Log(zerolog.FatalLevel, message, v...)
}
func (l *LogConfig) Error(message string, v ...map[string]interface{}) {
l.Log(zerolog.ErrorLevel, message, v...)
}
func (l *LogConfig) Warning(message string, v ...map[string]interface{}) {
l.Log(zerolog.WarnLevel, message, v...)
}
func (l *LogConfig) Info(message string, v ...map[string]interface{}) {
l.Log(zerolog.InfoLevel, message, v...)
}
func (l *LogConfig) Debug(message string, v ...map[string]interface{}) {
l.Log(zerolog.DebugLevel, message, v...)
}