-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
132 lines (109 loc) · 3.84 KB
/
parsers.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package goklogger
import (
"strings"
"github.com/sirupsen/logrus"
)
// StringToLogrusLevel takes a string representation of a log level (e.g., "info", "debug") and returns
// the corresponding logrus.Level. It is designed to be flexible, accepting any case and trimming
// unnecessary whitespace from the input. This function supports all standard logrus levels:
// "trace", "debug", "info", "warn"/"warning", "error", "fatal", and "panic". If the input does not
// match any of these levels, it defaults to logrus.InfoLevel.
//
// Parameters:
// - level: A string representing the desired log level. This parameter is case-insensitive and
// spaces before and after the level name are ignored.
//
// Returns:
// - A logrus.Level corresponding to the input string. If the input is unrecognized, the function
// defaults to returning logrus.InfoLevel.
//
// Usage:
// This function is particularly useful for parsing log level configurations from environment
// variables or configuration files where the input may vary in case or include additional spaces.
// It ensures that the application can dynamically adjust logging verbosity based on runtime
// configurations.
//
// Example:
// logLevel := ParseLogLevel(" DeBug ")
// log.SetLevel(logLevel)
//
// Note:
// The function treats "warn" and "warning" as equivalent, mapping both to logrus.WarnLevel,
// aligning with logrus's own handling of these terms.
func StringToLogrusLevel(level string) logrus.Level {
level = strings.ToLower(strings.TrimSpace(level))
switch level {
case "trace":
return logrus.TraceLevel
case "debug":
return logrus.DebugLevel
case "info":
return logrus.InfoLevel
case "warn", "warning": // logrus treats "warn" and "warning" as equivalent
return logrus.WarnLevel
case "error":
return logrus.ErrorLevel
case "fatal":
return logrus.FatalLevel
case "panic":
return logrus.PanicLevel
default:
// It's a common practice to default to InfoLevel when an unknown level is provided
return logrus.InfoLevel
}
}
// LogLevel represents logging levels by their severity.
type LogLevel int
const (
TraceLevel LogLevel = iota
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
)
// StringToLogLevel interprets a string as a logging level and converts it to the corresponding
// LogLevel constant defined in this package. The function is designed to be case-insensitive
// and ignores any leading or trailing whitespace in the input string. Recognized levels include
// "trace", "debug", "info", "warn"/"warning", "error", "fatal", and "panic". If the input string
// does not match any of these recognized levels, the function defaults to returning InfoLevel.
//
// This flexibility allows for dynamic adjustment of logging verbosity based on runtime
// configuration, facilitating easy integration with various configuration sources such as
// environment variables or configuration files.
//
// Parameters:
// - level: A string representing the logging level to parse.
//
// Returns:
// - The LogLevel constant corresponding to the provided string. Defaults to InfoLevel if the
// input string is unrecognized.
//
// Example usage:
// logLevel := logger.ParseLogLevel("error")
// logger.SetLevel(logLevel)
//
// The function ensures compatibility with various input formats by treating "warn" and "warning"
// as equivalent, aligning with common logging conventions.
func StringToLogLevel(level string) LogLevel {
level = strings.ToLower(strings.TrimSpace(level))
switch level {
case "trace":
return TraceLevel
case "debug":
return DebugLevel
case "info":
return InfoLevel
case "warn", "warning": // Support both "warn" and "warning" for compatibility
return WarnLevel
case "error":
return ErrorLevel
case "fatal":
return FatalLevel
case "panic":
return PanicLevel
default:
return InfoLevel // Default to InfoLevel for unrecognized strings
}
}