-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
100 lines (83 loc) · 2.61 KB
/
logger.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
package api
import (
"bytes"
"io"
"log"
"os"
"github.com/fatih/color"
)
var (
Warning *log.Logger
Info *log.Logger
Debug *log.Logger
Error *log.Logger
Gin *log.Logger
Stats *log.Logger
errorColor = color.New(color.Bold, color.FgRed).SprintFunc()
infoColor = color.New(color.Bold, color.FgWhite).SprintFunc()
debugColor = color.New(color.Bold, color.FgGreen).SprintFunc()
warningColor = color.New(color.Bold, color.FgYellow).SprintFunc()
versionColor = color.New(color.Bold, color.FgHiCyan).SprintFunc()
ginColor = color.New(color.Bold, color.FgBlue).SprintFunc()
wrt io.Writer
)
var route string
//InitLog initialize loggers with route and colors (predefined)
func InitLog(debugEnabled bool, desiredLogRoute, version string) {
route = desiredLogRoute
f, err := os.OpenFile(route, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
wrt = io.MultiWriter(os.Stdout, f)
log.SetOutput(wrt)
Info = log.New(wrt, infoColor("\n[INFO] "), log.Ldate|log.Ltime|log.Lshortfile)
Info.SetOutput(wrt)
Warning = log.New(wrt, warningColor("\n[WARNING] "), log.Ldate|log.Ltime|log.Lshortfile)
Warning.SetOutput(wrt)
Debug = log.New(wrt, debugColor("\n[DEBUG] "), log.Ldate|log.Ltime|log.Lshortfile)
Debug.SetOutput(wrt)
if !debugEnabled {
var buff bytes.Buffer
Debug.SetOutput(&buff)
}
Error = log.New(wrt, errorColor("\n[ERROR] "), log.Ldate|log.Ltime|log.Lshortfile)
Error.SetOutput(wrt)
Gin = log.New(wrt, ginColor("\n[GIN] "), log.Ldate|log.Ltime)
Gin.SetOutput(wrt)
Stats = log.New(wrt, debugColor("\n[STATS] "), log.Ldate|log.Ltime|log.Lshortfile)
Stats.SetOutput(wrt)
if !debugEnabled {
var buff bytes.Buffer
Stats.SetOutput(&buff)
}
PrintVersion(version)
}
// PrintVersion print engine with version example "CERBERUS V1.0.0"
func PrintVersion(engine string) {
os.Remove("SDK.ver")
f, err := os.OpenFile("SDK.ver", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
versionLog := log.New(os.Stdout, versionColor("\n[VERSION] "), 0)
versionLog.SetOutput(os.Stdout)
versionLog.Println(engine)
vLog := log.New(f, "", 0)
vLog.SetOutput(f)
vLog.Println(engine)
}
// EnableDebug use this function to enable/disable debug POST InitLog
func EnableDebug(activated bool) {
f, err := os.OpenFile(route, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
wrt = io.MultiWriter(os.Stdout, f)
var buff bytes.Buffer
if activated {
Debug.SetOutput(wrt)
} else {
Debug.SetOutput(&buff)
}
}