-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (71 loc) · 2.37 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
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
package main
import (
"fmt"
"io"
"github.com/bleemeo/squirreldb/config"
"github.com/bleemeo/squirreldb/daemon"
"github.com/bleemeo/squirreldb/logger"
zlogsentry "github.com/archdx/zerolog-sentry"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// variable set by GoReleaser
//
//nolint:gochecknoglobals
var (
version string
commit string
date string
)
// setupSentryLogger sets the default zerolog logger to a Sentry logger, or a simple
// console logger if Sentry is disabled. The closer returned should be closed at the
// end of the program to flush the events to Sentry.
func setupSentryLogger(cfg config.Config) io.Closer {
consoleWriter := logger.NewConsoleWriter(cfg.Log.DisableColor)
logLevel := zerolog.Level(cfg.Log.Level) //nolint:gosec
release := zlogsentry.WithRelease(fmt.Sprintf("squirreldb@%s-%s", version, commit))
if cfg.Sentry.DSN == "" {
log.Logger = logger.NewLogger(consoleWriter, logLevel)
return nil
}
sentryWriter, err := logger.NewSentryWriter(cfg.Sentry.DSN, zerolog.ErrorLevel, release)
if err != nil {
log.Err(err).Msg("Failed to initialize sentry")
log.Logger = logger.NewLogger(consoleWriter, logLevel)
return nil
}
// Set up logger with level filter.
multiWriter := zerolog.MultiLevelWriter(consoleWriter, sentryWriter)
log.Logger = logger.NewLogger(multiWriter, logLevel)
return sentryWriter
}
func main() {
daemon.Version = version
daemon.Commit = commit
daemon.Date = date
// Change the default time format of zerolog to allow millisecond precision.
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
// Initialize the logger temporarily before loading the config.
log.Logger = logger.NewLogger(logger.NewConsoleWriter(false), zerolog.TraceLevel)
cfg, warnings, err := daemon.Config()
if err != nil {
log.Fatal().Err(err).Msg("Failed to read config")
}
if warnings != nil {
log.Warn().Err(warnings).Msg("Got warnings while loading config")
}
if sentryCloser := setupSentryLogger(cfg); sentryCloser != nil {
defer sentryCloser.Close()
}
defer logger.ProcessPanic()
squirreldb := &daemon.SquirrelDB{
Config: cfg,
Logger: log.With().Str("component", "daemon").Logger(),
}
log.Info().Msgf("Starting SquirrelDB %s (commit %s)", version, commit)
err = daemon.RunWithSignalHandler(squirreldb.Run)
if err != nil {
log.Err(err).Msg("Failed to run SquirrelDB")
}
log.Debug().Msg("SquirrelDB is stopped")
}