This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_logging.go
77 lines (60 loc) · 1.82 KB
/
utils_logging.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
package utils
import (
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/rexray/rexray/libstorage/api/types"
)
// LoggingConfig is the logging configuration.
type LoggingConfig struct {
// Level is the log level.
Level log.Level
// Stdout is the path to the file to which to log stdout.
Stdout string
// Stderr is the path to the file to which to log stderr.
Stderr string
// HTTPRequests is a flag indicating whether or not to log HTTP requests.
HTTPRequests bool
// HTTPResponses is a flag indicating whether or not to log HTTP responses.
HTTPResponses bool
}
// ParseLoggingConfig returns a new LoggingConfig instance.
func ParseLoggingConfig(
config gofig.Config,
fields log.Fields,
roots ...string) (*LoggingConfig, error) {
f := func(k string, v interface{}) {
if fields == nil {
return
}
fields[k] = v
}
logConfig := &LoggingConfig{
Level: log.WarnLevel,
}
if lvl, err := log.ParseLevel(
getString(config, types.ConfigLogLevel, roots...)); err == nil {
logConfig.Level = lvl
f(types.ConfigLogLevel, lvl)
}
stdOutPath := getString(config, types.ConfigLogStdout, roots...)
if stdOutPath != "" {
logConfig.Stdout = stdOutPath
f(types.ConfigLogStdout, stdOutPath)
}
stdErrPath := getString(config, types.ConfigLogStderr, roots...)
if stdErrPath != "" {
logConfig.Stderr = stdErrPath
f(types.ConfigLogStderr, stdErrPath)
}
if isSet(config, types.ConfigLogHTTPRequests, roots...) {
logConfig.HTTPRequests = getBool(
config, types.ConfigLogHTTPRequests, roots...)
f(types.ConfigLogHTTPRequests, logConfig.HTTPRequests)
}
if isSet(config, types.ConfigLogHTTPResponses, roots...) {
logConfig.HTTPResponses = getBool(
config, types.ConfigLogHTTPResponses, roots...)
f(types.ConfigLogHTTPResponses, logConfig.HTTPResponses)
}
return logConfig, nil
}