-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogging.go
More file actions
91 lines (79 loc) · 2.38 KB
/
logging.go
File metadata and controls
91 lines (79 loc) · 2.38 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package logging
import (
"fmt"
"strings"
logging "github.com/ipfs/go-log/v2"
)
// Configure sets up logging configuration from the given string. This string is
// expected to have a space-delimited set of level directives, which are each
// evaluated in order by the evaluateLevelDirective function.
//
// If the level directive string is empty, a default level of info for all keep*
// subsystems is imposed.
func Configure(levelDirectiveString string) error {
// Default to info logs for keep.
if len(levelDirectiveString) == 0 {
levelDirectiveString = "keep*=info"
}
levelDirectives := strings.Split(levelDirectiveString, " ")
for _, directive := range levelDirectives {
err := evaluateLevelDirective(directive)
if err != nil {
return fmt.Errorf(
"Failed to parse log level directive [%s]: [%v]\n"+
"Directives can be any of:\n"+
" - a global log level, e.g. 'debug'\n"+
" - a subsystem=level pair, e.g. 'keep-relay=info'\n"+
" - a subsystem*=level prefix pair, e.g. 'keep*=warn'\n",
directive,
err,
)
}
}
return nil
}
// Takes a levelDirective that can have one of three formats:
//
// <log-level> |
// <subsystem>=<log-level> |
// <subsystem-prefix>*=<log-level>
//
// In the first form, the given log-level is set on all subsystems.
//
// In the second form, the given log-level is set on the given subsystem.
//
// In the third form, the given log-level is set on any subsystem that starts
// with the given subsystem-prefix.
//
// Supported log levels are as per the ipfs/go-logging library.
func evaluateLevelDirective(levelDirective string) error {
splitLevel := strings.Split(levelDirective, "=")
switch len(splitLevel) {
case 1:
level := splitLevel[0]
err := logging.SetLogLevel("*", level)
if err != nil {
return err
}
case 2:
levelSubsystem := splitLevel[0]
level := splitLevel[1]
if strings.HasSuffix(levelSubsystem, "*") {
subsystemPrefix := strings.TrimSuffix(levelSubsystem, "*")
// Wildcard suffix, check for matching subsystems.
for _, subsystem := range logging.GetSubsystems() {
if strings.HasPrefix(subsystem, subsystemPrefix) {
err := logging.SetLogLevel(subsystem, level)
if err != nil {
return err
}
}
}
} else {
return logging.SetLogLevel(levelSubsystem, level)
}
default:
return fmt.Errorf("more than two =-delimited components in directive")
}
return nil
}