forked from ufo-project/minepool-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
136 lines (116 loc) · 2.88 KB
/
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"build"
"fmt"
"io"
"log"
"os"
)
var (
Debug Logger
Info *log.Logger
Warning *log.Logger
Alert *log.Logger
ShareLog *log.Logger
BlockLog *log.Logger
)
const (
DEBUG_LEVEL = 7
INFO_LEVEL = 6
WARNING_LEVEL = 4
ALERT_LEVEL = 1
)
func InitLog(infoFile, errorFile, shareFile, blockFile string) {
log.Println("infoFile:", infoFile)
log.Println("errorFile:", errorFile)
log.Println("shareFile:", shareFile)
log.Println("blockFile:", blockFile)
infoFd, err := os.OpenFile(infoFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}
errorFd, err := os.OpenFile(errorFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}
shareFd, err := os.OpenFile(shareFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}
blockFd, err := os.OpenFile(blockFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}
Debug.l = log.New(io.MultiWriter(os.Stdout, infoFd), "[DEBUG] ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
Info = log.New(io.MultiWriter(os.Stdout, infoFd), "[INFO] ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
Warning = log.New(io.MultiWriter(os.Stdout, infoFd, errorFd), "[WARN] ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
Alert = log.New(io.MultiWriter(os.Stdout, infoFd, errorFd), "[ALERT] ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
ShareLog = log.New(io.MultiWriter(shareFd), "", log.Ldate|log.Lmicroseconds)
BlockLog = log.New(io.MultiWriter(blockFd, os.Stdout), "", log.Ldate|log.Lmicroseconds)
}
type Logger struct {
l *log.Logger
}
func (l *Logger) Print(v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprint(v...))
}
func (l *Logger) Println(v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprintln(v...))
}
func (l *Logger) Printf(format string, v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprintf(format, v...))
}
func (l *Logger) Fatal(v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprint(v...))
os.Exit(1)
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprintf(format, v...))
os.Exit(1)
}
func (l *Logger) Fatalln(v ...interface{}) {
if !build.DEBUG {
return
}
l.l.Output(2, fmt.Sprintln(v...))
os.Exit(1)
}
func (l *Logger) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
if !build.DEBUG {
return
}
l.l.Output(2, s)
panic(s)
}
func (l *Logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
if !build.DEBUG {
return
}
l.l.Output(2, s)
panic(s)
}
func (l *Logger) Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
if !build.DEBUG {
return
}
l.l.Output(2, s)
panic(s)
}