@@ -26,31 +26,41 @@ import (
26
26
)
27
27
28
28
// A logger to log logging logs!
29
- var loggingLogger = logging .MustGetLogger ("logging" )
29
+ var logger = logging .MustGetLogger ("logging" )
30
30
31
- var loggingDefaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32
- var loggingDefaultOutput = os .Stderr
31
+ var defaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32
+ var defaultOutput = os .Stderr
33
33
34
34
// The default logging level, in force until LoggingInit() is called or in
35
35
// case of configuration errors.
36
- var loggingDefaultLevel = logging .INFO
36
+ var fallbackDefaultLevel = logging .INFO
37
37
38
- // LoggingInit is a 'hook' called at the beginning of command processing to
38
+ // LoggingInitFromViper is a 'hook' called at the beginning of command processing to
39
39
// parse logging-related options specified either on the command-line or in
40
40
// config files. Command-line options take precedence over config file
41
41
// options, and can also be passed as suitably-named environment variables. To
42
42
// change module logging levels at runtime call `logging.SetLevel(level,
43
43
// module)`. To debug this routine include logging=debug as the first
44
44
// term of the logging specification.
45
- func LoggingInit (command string ) {
46
- // Parse the logging specification in the form
47
- // [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
48
- defaultLevel := loggingDefaultLevel
49
- var err error
45
+ // TODO this initialization is specific to the peer config format. The viper
46
+ // references should be removed, and this path should be moved into the peer
47
+ func InitFromViper (command string ) {
50
48
spec := viper .GetString ("logging_level" )
51
49
if spec == "" {
52
50
spec = viper .GetString ("logging." + command )
53
51
}
52
+ defaultLevel := InitFromSpec (spec )
53
+ logger .Debugf ("Setting default logging level to %s for command '%s'" , defaultLevel , command )
54
+ }
55
+
56
+ // LoggingInit initializes the logging based on the supplied spec, it is exposed externally
57
+ // so that consumers of the flogging package who do not wish to use the default config structure
58
+ // may parse their own logging specification
59
+ func InitFromSpec (spec string ) logging.Level {
60
+ // Parse the logging specification in the form
61
+ // [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
62
+ defaultLevel := fallbackDefaultLevel
63
+ var err error
54
64
if spec != "" {
55
65
fields := strings .Split (spec , ":" )
56
66
for _ , field := range fields {
@@ -60,46 +70,46 @@ func LoggingInit(command string) {
60
70
// Default level
61
71
defaultLevel , err = logging .LogLevel (field )
62
72
if err != nil {
63
- loggingLogger .Warningf ("Logging level '%s' not recognized, defaulting to %s : %s" , field , loggingDefaultLevel , err )
64
- defaultLevel = loggingDefaultLevel // NB - 'defaultLevel' was overwritten
73
+ logger .Warningf ("Logging level '%s' not recognized, defaulting to %s : %s" , field , defaultLevel , err )
74
+ defaultLevel = fallbackDefaultLevel // NB - 'defaultLevel' was overwritten
65
75
}
66
76
case 2 :
67
77
// <module>[,<module>...]=<level>
68
78
if level , err := logging .LogLevel (split [1 ]); err != nil {
69
- loggingLogger .Warningf ("Invalid logging level in '%s' ignored" , field )
79
+ logger .Warningf ("Invalid logging level in '%s' ignored" , field )
70
80
} else if split [0 ] == "" {
71
- loggingLogger .Warningf ("Invalid logging override specification '%s' ignored - no module specified" , field )
81
+ logger .Warningf ("Invalid logging override specification '%s' ignored - no module specified" , field )
72
82
} else {
73
83
modules := strings .Split (split [0 ], "," )
74
84
for _ , module := range modules {
75
85
logging .SetLevel (level , module )
76
- loggingLogger .Debugf ("Setting logging level for module '%s' to %s" , module , level )
86
+ logger .Debugf ("Setting logging level for module '%s' to %s" , module , level )
77
87
}
78
88
}
79
89
default :
80
- loggingLogger .Warningf ("Invalid logging override '%s' ignored; Missing ':' ?" , field )
90
+ logger .Warningf ("Invalid logging override '%s' ignored; Missing ':' ?" , field )
81
91
}
82
92
}
83
93
}
84
94
// Set the default logging level for all modules
85
95
logging .SetLevel (defaultLevel , "" )
86
- loggingLogger . Debugf ( "Setting default logging level to %s for command '%s'" , defaultLevel , command )
96
+ return defaultLevel
87
97
}
88
98
89
- // DefaultLoggingLevel returns the fallback value for loggers to use if parsing fails
90
- func DefaultLoggingLevel () logging.Level {
91
- return loggingDefaultLevel
99
+ // DefaultLevel returns the fallback value for loggers to use if parsing fails
100
+ func DefaultLevel () logging.Level {
101
+ return fallbackDefaultLevel
92
102
}
93
103
94
104
// Initiate 'leveled' logging using the default format and output location
95
105
func init () {
96
- SetLoggingFormat (loggingDefaultFormat , loggingDefaultOutput )
106
+ SetLoggingFormat (defaultFormat , defaultOutput )
97
107
}
98
108
99
109
// SetLoggingFormat sets the logging format and the location of the log output
100
110
func SetLoggingFormat (formatString string , output io.Writer ) {
101
111
if formatString == "" {
102
- formatString = loggingDefaultFormat
112
+ formatString = defaultFormat
103
113
}
104
114
format := logging .MustStringFormatter (formatString )
105
115
@@ -111,32 +121,32 @@ func SetLoggingFormat(formatString string, output io.Writer) {
111
121
func initLoggingBackend (logFormatter logging.Formatter , output io.Writer ) {
112
122
backend := logging .NewLogBackend (output , "" , 0 )
113
123
backendFormatter := logging .NewBackendFormatter (backend , logFormatter )
114
- logging .SetBackend (backendFormatter ).SetLevel (loggingDefaultLevel , "" )
124
+ logging .SetBackend (backendFormatter ).SetLevel (fallbackDefaultLevel , "" )
115
125
}
116
126
117
- // GetModuleLogLevel gets the current logging level for the specified module
118
- func GetModuleLogLevel (module string ) (string , error ) {
127
+ // GetModuleLevel gets the current logging level for the specified module
128
+ func GetModuleLevel (module string ) (string , error ) {
119
129
// logging.GetLevel() returns the logging level for the module, if defined.
120
130
// otherwise, it returns the default logging level, as set by
121
131
// flogging/logging.go
122
132
level := logging .GetLevel (module ).String ()
123
133
124
- loggingLogger .Debugf ("Module '%s' logger enabled for log level: %s" , module , level )
134
+ logger .Debugf ("Module '%s' logger enabled for log level: %s" , module , level )
125
135
126
136
return level , nil
127
137
}
128
138
129
- // SetModuleLogLevel sets the logging level for the specified module. This is
139
+ // SetModuleLevel sets the logging level for the specified module. This is
130
140
// currently only called from admin.go but can be called from anywhere in the
131
141
// code on a running peer to dynamically change the log level for the module.
132
- func SetModuleLogLevel (module string , logLevel string ) (string , error ) {
142
+ func SetModuleLevel (module string , logLevel string ) (string , error ) {
133
143
level , err := logging .LogLevel (logLevel )
134
144
135
145
if err != nil {
136
- loggingLogger .Warningf ("Invalid logging level: %s - ignored" , logLevel )
146
+ logger .Warningf ("Invalid logging level: %s - ignored" , logLevel )
137
147
} else {
138
148
logging .SetLevel (logging .Level (level ), module )
139
- loggingLogger .Debugf ("Module '%s' logger enabled for log level: %s" , module , level )
149
+ logger .Debugf ("Module '%s' logger enabled for log level: %s" , module , level )
140
150
}
141
151
142
152
logLevelString := level .String ()
0 commit comments