fastpastebin/internal/application/application_logger.go
Stanislav N. aka pztrn 5fc6d3a181
Some checks failed
continuous-integration/drone/push Build is failing
Application superstructure (or supersingleton, if you want).
Got rid of context thing which misleads due to existance of stdlib's
context package.

Also fixed golangci-lint configuration.

Fixes #20.
2022-08-19 21:52:49 +05:00

80 lines
2.1 KiB
Go

package application
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/rs/zerolog"
)
// Puts memory usage into log lines.
func (a *Application) getMemoryUsage(event *zerolog.Event, level zerolog.Level, message string) {
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
event.Str("memalloc", fmt.Sprintf("%dMB", memstats.Alloc/1024/1024))
event.Str("memsys", fmt.Sprintf("%dMB", memstats.Sys/1024/1024))
event.Str("numgc", fmt.Sprintf("%d", memstats.NumGC))
}
// Initializes logger.
func (a *Application) initializeLogger() {
// Устанавливаем форматирование логгера.
//nolint:exhaustruct
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
output.FormatLevel = func(lvlRaw interface{}) string {
var lvl string
if lvlAsString, ok := lvlRaw.(string); ok {
lvlAsString = strings.ToUpper(lvlAsString)
switch lvlAsString {
case "DEBUG":
lvl = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvlAsString)
case "ERROR":
lvl = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvlAsString)
case "FATAL":
lvl = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvlAsString)
case "INFO":
lvl = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvlAsString)
case "PANIC":
lvl = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvlAsString)
case "WARN":
lvl = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvlAsString)
default:
lvl = lvlAsString
}
}
return fmt.Sprintf("| %s |", lvl)
}
a.Log = zerolog.New(output).With().Timestamp().Logger()
a.Log = a.Log.Hook(zerolog.HookFunc(a.getMemoryUsage))
}
// Initialize logger after configuration parse.
func (a *Application) initializeLoggerPost() {
// Set log level.
a.Log.Info().Msgf("Setting logger level: %s", a.Config.Logging.LogLevel)
switch a.Config.Logging.LogLevel {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "PANIC":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
}
}