-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
log.go
44 lines (37 loc) · 989 Bytes
/
log.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
package main
import (
"fmt"
"runtime"
"strings"
"time"
)
func chopPath(original string) string {
i := strings.LastIndex(original, "/")
if i == -1 {
return original
}
return original[i+1:]
}
// Debug prints debug messages
func Debug(vars ...interface{}) {
printWithColor("\x1b[44;1m", "DEBUG", vars)
}
// Error prints error messages
func Error(vars ...interface{}) {
printWithColor("\x1b[41;1m", "ERROR", vars)
}
// Warning prints warning messages
func Warning(vars ...interface{}) {
printWithColor("\x1b[43;1m", "WARNING", vars)
}
func printWithColor(color, ptype string, vars ...interface{}) {
function, file, line, _ := runtime.Caller(2)
msg := ""
for _, v := range vars {
txt := fmt.Sprintf("%v", v)
txt = strings.Trim(txt, "[")
txt = strings.Trim(txt, "]")
msg = fmt.Sprintf("%v", txt)
}
fmt.Printf("%v[%v][%s:%d][%s][%v]\x1b[0;1m %v\n", color, time.Now().Format("15:04:05.000"), chopPath(file), line, runtime.FuncForPC(function).Name(), ptype, msg)
}