-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
59 lines (51 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"os"
"time"
"github.com/Southclaws/ScavengeSurvive/runner"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
const logFileNameFormat = `logs/server-2006-01-02.log`
const devLogTimeFormat = `15:04:05.000`
func main() {
// Logger config is quite important for this app so it goes first in main().
var cfg runner.Config
envconfig.MustProcess("", &cfg)
var encoder zapcore.Encoder
if cfg.Production {
ec := zap.NewProductionEncoderConfig()
ec.EncodeTime = zapcore.ISO8601TimeEncoder
encoder = zapcore.NewJSONEncoder(ec)
} else {
ec := zap.NewDevelopmentEncoderConfig()
ec.EncodeLevel = zapcore.CapitalColorLevelEncoder
ec.EncodeTime = zapcore.TimeEncoderOfLayout(devLogTimeFormat)
encoder = zapcore.NewConsoleEncoder(ec)
}
zap.ReplaceGlobals(zap.New(zapcore.NewTee(
zapcore.NewCore(
encoder,
zapcore.Lock(os.Stdout),
cfg.LogLevel,
),
zapcore.NewCore(
encoder,
zapcore.AddSync(&lumberjack.Logger{
Filename: time.Now().Format(logFileNameFormat),
MaxSize: 100, // MB
}),
cfg.LogLevel,
),
)))
zap.L().Info("logger configured", zap.Any("config", cfg))
// Now run the app itself.
if err := runner.Run(cfg); err != nil {
zap.L().Info("unexpected exit", zap.String("error", err.Error()))
} else {
zap.L().Info("exited gracefully")
}
}