-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlevel.go
106 lines (91 loc) · 2.22 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
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
// Copyright (C) 2017, ccpaging <ccpaging@gmail.com>. All rights reserved.
package nxlog4go
import (
"fmt"
"strings"
"github.com/ccpaging/nxlog4go/color"
)
// logging levels used by the logger
const (
FINEST int = iota
FINE
DEBUG
TRACE
INFO
WARN
ERROR
CRITICAL
)
type levelString struct {
short string
lower string
color color.Color
}
var levelMap map[int]*levelString
func init() {
levelMap = make(map[int]*levelString)
levelMap[FINEST] = &levelString{"FNST", "finest", color.Gray}
levelMap[FINE] = &levelString{"FINE", "fine", color.Green}
levelMap[DEBUG] = &levelString{"DEBG", "debug", color.Magenta}
levelMap[TRACE] = &levelString{"TRAC", "trace", color.Cyan}
levelMap[INFO] = &levelString{"INFO", "info", color.White}
levelMap[WARN] = &levelString{"WARN", "warn", color.LightYellow}
levelMap[ERROR] = &levelString{"EROR", "error", color.Red}
levelMap[CRITICAL] = &levelString{"CRIT", "critical", color.LightRed}
}
// Level is the integer logging levels
type Level int
// String return the string of integer Level
func (l Level) String() string {
ls, ok := levelMap[int(l)]
if ok {
return ls.short
}
return fmt.Sprintf("Level(%d)", l)
}
func (l Level) string2int(s string) int {
s = strings.ToLower(s)
for i, ls := range levelMap {
if s == strings.ToLower(ls.short) || s == ls.lower {
return i
}
}
if s == "warning" {
return WARN
}
return int(l)
}
// IntE casts an interface to a level int.
func (l Level) IntE(v interface{}) (int, error) {
if _, ok := v.(int); ok {
return v.(int), nil
}
if _, ok := v.(Level); ok {
return int(v.(Level)), nil
}
if _, ok := v.(string); ok {
return l.string2int(v.(string)), nil
}
return INFO, fmt.Errorf("unknown level value %#v of type %T", v, v)
}
// Int casts an interface to a level int.
func (l Level) Int(v interface{}) int {
n, _ := l.IntE(v)
return n
}
// ColorBytes return the ANSI color bytes by level
func (l Level) colorBytes(n int) []byte {
ls, ok := levelMap[int(n)]
if ok {
return ls.color.Bytes()
}
return color.Red.Bytes()
}
// Colorize return the ANSI color wrap bytes by level
func (l Level) colorize(s string) []byte {
ls, ok := levelMap[int(l)]
if ok {
return ls.color.Wrap([]byte(s))
}
return color.Red.Wrap([]byte(s))
}