forked from 181192/custom-error-pages
/
flags.go
52 lines (43 loc) · 1.42 KB
/
flags.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
package util
import (
"os"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// HTTPListenAddress address server listens on host:port
HTTPListenAddress = "http-listen-address"
// Debug if debug logs should be enabled
Debug = "debug"
// LogColor sets log format to human-friendly, colorized output
LogColor = "log-color"
// ErrFilesPath the location on disk of files served by the handler
ErrFilesPath = "error-files-path"
// HideDetails hide request details in response
HideDetails = "hide-details"
)
// InitFlags initialize viper and pflags
func InitFlags() {
pflag.Bool(Debug, false, "enable debug log")
pflag.Bool(LogColor, false, "sets log format to human-friendly, colorized output")
pflag.String(HTTPListenAddress, ":8080", "http server address")
pflag.String(ErrFilesPath, "./themes/knockout", "the location on disk of files served by the handler")
pflag.Bool(HideDetails, false, "hide request details in response")
pflag.Parse()
viper.AutomaticEnv()
viper.BindPFlags(pflag.CommandLine)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
}
// ConfigureLogger configure log output and levels
func ConfigureLogger() {
if viper.GetBool(LogColor) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if viper.GetBool(Debug) {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
}