/
template.go
153 lines (132 loc) · 3.42 KB
/
template.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/playnet-public/gocmd-template/pkg/template"
raven "github.com/getsentry/raven-go"
"github.com/golang/glog"
"github.com/kolide/kit/version"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
app = "GoCMD Template"
appKey = "gocmd-template"
)
var (
maxprocsPtr = flag.Int("maxprocs", runtime.NumCPU(), "max go procs")
sentryDsn = flag.String("sentrydsn", "", "sentry dsn key")
dbgPtr = flag.Bool("debug", false, "debug printing")
versionPtr = flag.Bool("version", true, "show or hide version info")
sentry *raven.Client
)
func main() {
flag.Parse()
if *versionPtr {
fmt.Printf("-- PlayNet %s --\n", app)
version.PrintFull()
}
runtime.GOMAXPROCS(*maxprocsPtr)
// prepare glog
defer glog.Flush()
glog.CopyStandardLogTo("info")
// prepare zap logging
log := newLogger(*dbgPtr).With(
zap.String("app", appKey),
zap.String("version", version.Version().Version),
)
defer log.Sync()
log.Info("preparing")
var err error
// prepare sentry error logging
sentry, err = raven.New(*sentryDsn)
if err != nil {
panic(err)
}
err = raven.SetDSN(*sentryDsn)
if err != nil {
panic(err)
}
errs := make(chan error)
// catch system interrupts
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
// catch errors and throw fatal //TODO: is this good?
go func() {
ret := <-errs
if ret != nil {
log.Fatal(ret.Error())
}
}()
// run main code
log.Info("starting")
raven.CapturePanicAndWait(func() {
if err := do(log); err != nil {
log.Fatal("fatal error encountered", zap.Error(err))
raven.CaptureErrorAndWait(err, map[string]string{"isFinal": "true"})
errs <- err
}
}, nil)
log.Info("finished")
}
func do(log *zap.Logger) error {
// zap for production logging
log.Info("fetching time",
zap.String("time", time.Now().String()),
zap.Bool("success", true),
)
// glog for debug levels
glog.V(5).Infoln("fetching template return")
tmpl := template.Template(appKey)
log.Info("got template", zap.String("result", tmpl))
glog.V(5).Infoln("causing some errors")
tmpl, err := template.Error()
if err != nil {
// note that using errors.Wrap is advised to provide valuable context
err = errors.Wrap(err, "failed to get template")
log.Error("template failed",
zap.String("result", tmpl),
// we are using string here on purpose to prevent too verbose error printing
zap.String("error", err.Error()),
)
}
return err
}
//TODO: Move this to playnet common libs
func newLogger(dbg bool) *zap.Logger {
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logger := zap.New(core)
if dbg {
logger = logger.WithOptions(
zap.AddCaller(),
zap.AddStacktrace(zap.ErrorLevel),
)
} else {
logger = logger.WithOptions(
zap.AddStacktrace(zap.FatalLevel),
)
}
return logger
}