-
Notifications
You must be signed in to change notification settings - Fork 2
/
logs.go
70 lines (56 loc) · 1.68 KB
/
logs.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
package utils
import (
"fmt"
"time"
"github.com/fatih/color"
"github.com/spf13/viper"
)
const (
LevelInfo = iota
LevelSuccess
LevelFailed
LevelWarning
LevelError
LevelFatal
LevelDebug
)
func get_current_time() string {
return time.Now().Format("15:04:05")
}
func log_record(level int, detail string) {
if level > viper.GetInt("loglevel") {
return
}
FileWrite("result.log", detail+"\n")
}
func log_print(level int, detail string) {
if level > viper.GetInt("loglevel") {
return
}
fmt.Println(detail)
}
func LogBeautify(x string, colorAttr color.Attribute, y string, level int) {
log_print(level, fmt.Sprintf("[%s] [%s] %s", get_current_time(), color.New(colorAttr).Sprintf("%s", x), y))
log_record(level, fmt.Sprintf("[%s] [%s] %s", get_current_time(), x, y))
}
func Debug(format string, args ...interface{}) {
LogBeautify("DEBUG", color.BgYellow, fmt.Sprintf(format, args...), LevelDebug)
}
func Info(format string, args ...interface{}) {
LogBeautify("INFO", color.FgCyan, fmt.Sprintf(format, args...), LevelInfo)
}
func Success(format string, args ...interface{}) {
LogBeautify("SUCCESS", color.FgHiGreen, fmt.Sprintf(format, args...), LevelSuccess)
}
func Failed(format string, args ...interface{}) {
LogBeautify("FAILED", color.BgRed, fmt.Sprintf(format, args...), LevelFailed)
}
func Warning(format string, args ...interface{}) {
LogBeautify("WARN", color.FgYellow, fmt.Sprintf(format, args...), LevelWarning)
}
func Error(format string, args ...interface{}) {
LogBeautify("ERROR", color.FgRed, fmt.Sprintf(format, args...), LevelError)
}
func Fatal(format string, args ...interface{}) {
LogBeautify("FATAL", color.FgHiRed, fmt.Sprintf(format, args...), LevelFatal)
}