2019-11-06 12:33:46 +00:00
|
|
|
package main
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
import (
|
2021-05-25 10:23:29 +00:00
|
|
|
"context"
|
2022-03-25 13:06:33 +00:00
|
|
|
"fmt"
|
2021-05-25 10:23:29 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
2022-03-25 13:06:33 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2020-11-09 13:43:23 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 22:46:33 +00:00
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
v = settings()
|
|
|
|
l = newLogger(v)
|
|
|
|
)
|
2021-05-25 10:23:29 +00:00
|
|
|
globalContext, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
2021-03-30 22:46:33 +00:00
|
|
|
app := newApp(globalContext, WithLogger(l), WithConfig(v))
|
|
|
|
go app.Serve(globalContext)
|
|
|
|
app.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:06:33 +00:00
|
|
|
// newLogger constructs a zap.Logger instance for current application.
|
|
|
|
// Panics on failure.
|
|
|
|
//
|
|
|
|
// Logger is built from zap's production logging configuration with:
|
|
|
|
// * parameterized level (debug by default)
|
|
|
|
// * console encoding
|
|
|
|
// * ISO8601 time encoding
|
|
|
|
//
|
|
|
|
// Logger records a stack trace for all messages at or above fatal level.
|
|
|
|
//
|
|
|
|
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
2020-11-09 13:43:23 +00:00
|
|
|
func newLogger(v *viper.Viper) *zap.Logger {
|
2022-03-25 13:06:33 +00:00
|
|
|
var lvl zapcore.Level
|
|
|
|
lvlStr := v.GetString(cfgLoggerLevel)
|
|
|
|
err := lvl.UnmarshalText([]byte(lvlStr))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("incorrect logger level configuration %s (%v), "+
|
|
|
|
"value should be one of %v", lvlStr, err, [...]zapcore.Level{
|
|
|
|
zapcore.DebugLevel,
|
|
|
|
zapcore.InfoLevel,
|
|
|
|
zapcore.WarnLevel,
|
|
|
|
zapcore.ErrorLevel,
|
|
|
|
zapcore.DPanicLevel,
|
|
|
|
zapcore.PanicLevel,
|
|
|
|
zapcore.FatalLevel,
|
|
|
|
}))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2022-03-25 13:06:33 +00:00
|
|
|
|
|
|
|
c := zap.NewProductionConfig()
|
|
|
|
c.Level = zap.NewAtomicLevelAt(lvl)
|
|
|
|
c.Encoding = "console"
|
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
|
|
|
|
l, err := c.Build(
|
|
|
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
|
|
|
)
|
2020-11-09 13:43:23 +00:00
|
|
|
if err != nil {
|
2022-03-25 13:06:33 +00:00
|
|
|
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
2020-11-09 13:43:23 +00:00
|
|
|
}
|
2022-03-25 13:06:33 +00:00
|
|
|
|
2020-11-09 13:43:23 +00:00
|
|
|
return l
|
|
|
|
}
|