forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.go
68 lines (57 loc) · 1.54 KB
/
levels.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package flogging
import (
"fmt"
"math"
"go.uber.org/zap/zapcore"
)
const (
// DisabledLevel represents a disabled log level. Logs at this level should
// never be emitted.
DisabledLevel = zapcore.Level(math.MinInt8)
// PayloadLevel is used to log the extremely detailed message level debug
// information.
PayloadLevel = zapcore.Level(zapcore.DebugLevel - 1)
)
// NameToLevel converts a level name to a zapcore.Level. If the level name is
// unknown, zapcore.InfoLevel is returned.
func NameToLevel(level string) zapcore.Level {
l, err := nameToLevel(level)
if err != nil {
return zapcore.InfoLevel
}
return l
}
func nameToLevel(level string) (zapcore.Level, error) {
switch level {
case "PAYLOAD", "payload":
return PayloadLevel, nil
case "DEBUG", "debug":
return zapcore.DebugLevel, nil
case "INFO", "info":
return zapcore.InfoLevel, nil
case "WARNING", "WARN", "warning", "warn":
return zapcore.WarnLevel, nil
case "ERROR", "error":
return zapcore.ErrorLevel, nil
case "DPANIC", "dpanic":
return zapcore.DPanicLevel, nil
case "PANIC", "panic":
return zapcore.PanicLevel, nil
case "FATAL", "fatal":
return zapcore.FatalLevel, nil
case "NOTICE", "notice":
return zapcore.InfoLevel, nil // future
case "CRITICAL", "critical":
return zapcore.ErrorLevel, nil // future
default:
return DisabledLevel, fmt.Errorf("invalid log level: %s", level)
}
}
func IsValidLevel(level string) bool {
_, err := nameToLevel(level)
return err == nil
}