2019-03-07 08:43:28 +05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Puts memory usage into log lines.
|
2021-11-20 22:19:58 +05:00
|
|
|
func (c *Context) getMemoryUsage(event *zerolog.Event, level zerolog.Level, message string) {
|
2022-06-26 22:26:39 +05:00
|
|
|
var memstats runtime.MemStats
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-06-26 22:26:39 +05:00
|
|
|
runtime.ReadMemStats(&memstats)
|
2019-03-07 08:43:28 +05:00
|
|
|
|
2022-06-26 22:26:39 +05:00
|
|
|
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))
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes logger.
|
|
|
|
func (c *Context) initializeLogger() {
|
|
|
|
// Устанавливаем форматирование логгера.
|
2022-06-26 22:26:39 +05:00
|
|
|
// nolint:exhaustruct
|
2019-03-07 08:43:28 +05:00
|
|
|
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
|
2021-11-20 22:19:58 +05:00
|
|
|
output.FormatLevel = func(lvlRaw interface{}) string {
|
2022-06-26 22:26:39 +05:00
|
|
|
var lvl string
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-06-26 22:26:39 +05:00
|
|
|
if lvlAsString, ok := lvlRaw.(string); ok {
|
|
|
|
lvlAsString = strings.ToUpper(lvlAsString)
|
|
|
|
switch lvlAsString {
|
2019-03-07 08:43:28 +05:00
|
|
|
case "DEBUG":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
case "ERROR":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
case "FATAL":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
case "INFO":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
case "PANIC":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
case "WARN":
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvlAsString)
|
2019-03-07 08:43:28 +05:00
|
|
|
default:
|
2022-06-26 22:26:39 +05:00
|
|
|
lvl = lvlAsString
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2022-06-26 22:26:39 +05:00
|
|
|
return fmt.Sprintf("| %s |", lvl)
|
2019-03-07 08:43:28 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger = zerolog.New(output).With().Timestamp().Logger()
|
|
|
|
|
|
|
|
c.Logger = c.Logger.Hook(zerolog.HookFunc(c.getMemoryUsage))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize logger after configuration parse.
|
|
|
|
func (c *Context) initializeLoggerPost() {
|
|
|
|
// Set log level.
|
|
|
|
c.Logger.Info().Msgf("Setting logger level: %s", c.Config.Logging.LogLevel)
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-03-07 08:43:28 +05:00
|
|
|
switch c.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)
|
|
|
|
}
|
|
|
|
}
|