This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
131 lines (113 loc) · 3.02 KB
/
config.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
package log
import (
"fmt"
)
// Config describes the logging settings.
type Config struct {
// Level describes the minimum level to log at
Level Level `json:"level" yaml:"level" default:"notice"`
// Format describes the log message format
Format Format `json:"format" yaml:"format" default:"ljson"`
}
// region Level
// Level syslog-style log level identifiers
type Level int8
// Supported values for Level
const (
LevelDebug Level = 7
LevelInfo Level = 6
LevelNotice Level = 5
LevelWarning Level = 4
LevelError Level = 3
LevelCritical Level = 2
LevelAlert Level = 1
LevelEmergency Level = 0
)
// String Convert the int level to the string representation
func (level Level) String() (LevelString, error) {
switch level {
case LevelDebug:
return LevelDebugString, nil
case LevelInfo:
return LevelInfoString, nil
case LevelNotice:
return LevelNoticeString, nil
case LevelWarning:
return LevelWarningString, nil
case LevelError:
return LevelErrorString, nil
case LevelCritical:
return LevelCriticalString, nil
case LevelAlert:
return LevelAlertString, nil
case LevelEmergency:
return LevelEmergencyString, nil
}
return "", fmt.Errorf("invalid log level (%d)", level)
}
// Validate if the log level has a valid value
func (level Level) Validate() error {
if level < LevelEmergency || level > LevelDebug {
return fmt.Errorf("invalid log level (%d)", level)
}
return nil
}
// endregion
// region LevelString
// LevelString is a type for supported log level strings
type LevelString string
// List of valid string values for log levels
const (
LevelDebugString LevelString = "debug"
LevelInfoString LevelString = "info"
LevelNoticeString LevelString = "notice"
LevelWarningString LevelString = "warning"
LevelErrorString LevelString = "error"
LevelCriticalString LevelString = "crit"
LevelAlertString LevelString = "alert"
LevelEmergencyString LevelString = "emerg"
)
// ToLevel convert the string level to the int representation
func (level LevelString) ToLevel() (Level, error) {
switch level {
case LevelDebugString:
return LevelDebug, nil
case LevelInfoString:
return LevelInfo, nil
case LevelNoticeString:
return LevelNotice, nil
case LevelWarningString:
return LevelWarning, nil
case LevelErrorString:
return LevelError, nil
case LevelCriticalString:
return LevelCritical, nil
case LevelAlertString:
return LevelAlert, nil
case LevelEmergencyString:
return LevelEmergency, nil
}
return -1, fmt.Errorf("invalid log level (%s)", level)
}
// endregion
// region Format
// Format is the logging format to use.
//swagger:enum
type Format string
const (
// FormatLJSON is a newline-delimited JSON log format.
FormatLJSON Format = "ljson"
// FormatText prints the logs as plain text.
FormatText Format = "text"
)
// Validate returns an error if the format is invalid.
func (format Format) Validate() error {
switch format {
case FormatLJSON:
case FormatText:
default:
return fmt.Errorf("invalid log format: %s", format)
}
return nil
}
// endregion