-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
67 lines (56 loc) · 1.81 KB
/
config.go
File metadata and controls
67 lines (56 loc) · 1.81 KB
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
package log
import (
"fmt"
"os"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var DefaultOptions = Options{
LogLevel: "info",
JSON: false,
Concise: false,
Tags: nil,
}
type Options struct {
// LogLevel defines the minimum level of severity that app should log.
//
// Must be one of: ["trace", "debug", "info", "warn", "error", "critical"]
LogLevel string
// JSON enables structured logging output in json. Make sure to enable this
// in production mode so log aggregators can receive data in parsable format.
//
// In local development mode, its appropriate to set this value to false to
// receive pretty output and stacktraces to stdout.
JSON bool
// Concise mode includes fewer log details during the request flow. For example
// exluding details like request content length, user-agent and other details.
// This is useful if during development your console is too noisy.
Concise bool
// Tags are additional fields included at the root level of all logs.
// These can be useful for example the commit hash of a build, or an environment
// name like prod/stg/dev
Tags map[string]string
}
// Configure will set new global/default options for the httplog and behaviour
// of underlying zerolog pkg and its global logger.
func Configure(opts Options) {
if opts.LogLevel == "" {
opts.LogLevel = "info"
}
DefaultOptions = opts
// Config the zerolog global logger
logLevel, err := zerolog.ParseLevel(strings.ToLower(opts.LogLevel))
if err != nil {
fmt.Printf("log: error! %v\n", err)
os.Exit(1)
}
zerolog.SetGlobalLevel(logLevel)
zerolog.LevelFieldName = "level"
zerolog.TimestampFieldName = "timestamp"
zerolog.TimeFieldFormat = time.RFC3339Nano
if !opts.JSON {
Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339, NoColor: true})
}
}