-
Notifications
You must be signed in to change notification settings - Fork 11
/
level.go
62 lines (49 loc) · 1.09 KB
/
level.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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package logs
import (
"fmt"
"strings"
)
type Level int8
// 目前支持的日志类型
const (
LevelInfo Level = iota
LevelTrace
LevelDebug
LevelWarn
LevelError
LevelFatal
)
var levelStrings = map[Level]string{
LevelInfo: "INFO",
LevelTrace: "TRAC",
LevelDebug: "DBUG",
LevelWarn: "WARN",
LevelError: "ERRO",
LevelFatal: "FATL",
}
func AllLevels() []Level {
return []Level{LevelInfo, LevelWarn, LevelTrace, LevelFatal, LevelError, LevelDebug}
}
func IsValidLevel(l Level) bool { return l >= LevelInfo && l <= LevelFatal }
func (l Level) String() string { return levelStrings[l] }
func (l Level) MarshalText() ([]byte, error) { return []byte(l.String()), nil }
func (l *Level) UnmarshalText(data []byte) error {
lv, err := ParseLevel(string(data))
if err != nil {
return err
}
*l = lv
return nil
}
func ParseLevel(s string) (Level, error) {
s = strings.ToUpper(s)
for level, name := range levelStrings {
if s == name {
return level, nil
}
}
return -1, fmt.Errorf("无效的值 %s", s)
}