-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggers.go
111 lines (95 loc) · 2.44 KB
/
loggers.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
package rabbit
import (
"fmt"
"log"
"os"
"github.com/mattn/go-isatty"
)
const (
green = "\033[97;42m"
white = "\033[90;47m"
yellow = "\033[90;43m"
red = "\033[97;41m"
blue = "\033[97;44m"
magenta = "\033[97;45m"
cyan = "\033[97;46m"
reset = "\033[0m"
)
const (
LevelDebug = iota
LevelInfo
LevelWarning
LevelError
)
var LogLevel = LevelDebug
var EnabledConsoleColor = false
func colorize(color, message any) string {
if !EnabledConsoleColor {
return fmt.Sprintf("%v", message)
}
return fmt.Sprintf("%s%v%s", color, message, reset)
}
func colorLevel(level int) string {
switch level {
case LevelDebug:
return colorize(blue, "DEBUG")
case LevelInfo:
return colorize(green, "INFO")
case LevelWarning:
return colorize(yellow, "WARNING")
case LevelError:
return colorize(red, "ERROR")
default:
return colorize(white, "???")
}
}
func SetLogLevel(level int) {
LogLevel = level
out := log.Default().Writer()
if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
EnabledConsoleColor = false
} else {
EnabledConsoleColor = true
}
}
func Debugf(format string, v ...any) {
if LogLevel <= LevelDebug {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelDebug), fmt.Sprintf(format, v...)))
}
}
func Debugln(v ...any) {
if LogLevel <= LevelDebug {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelDebug), fmt.Sprintln(v...)))
}
}
func Infof(format string, v ...any) {
if LogLevel <= LevelInfo {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelInfo), fmt.Sprintf(format, v...)))
}
}
func Infoln(v ...any) {
if LogLevel <= LevelInfo {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelInfo), fmt.Sprintln(v...)))
}
}
func Warningf(format string, v ...any) {
if LogLevel <= LevelWarning {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelWarning), fmt.Sprintf(format, v...)))
}
}
func Warningln(v ...any) {
if LogLevel <= LevelWarning {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelWarning), fmt.Sprintln(v...)))
}
}
func Errorf(format string, v ...any) {
if LogLevel <= LevelError {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelError), fmt.Sprintf(format, v...)))
}
}
func Errorln(v ...any) {
if LogLevel <= LevelError {
log.Default().Output(2, fmt.Sprintf("[%s] %v", colorLevel(LevelError), fmt.Sprintln(v...)))
}
}