-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
125 lines (107 loc) · 2.46 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
package common
import (
"fmt"
slog "log"
"os"
"strings"
"time"
)
var Log = func() ILogger {
l := log{}
l.Enable(true)
return &l
}()
var logfile *os.File
func init() {
var err error
logPath := GetAbsolutePath(LOG_PATH)
logfile, err = os.OpenFile(GetAbsolutePath(logPath, "access.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
slog.Printf("ERROR log file: %+v", err)
return
}
logfile.WriteString("")
}
type log struct {
enable bool
debug bool
info bool
warn bool
error bool
}
func (l *log) Info(format string, v ...interface{}) {
if l.info && l.enable {
message := fmt.Sprintf("%s SYST INFO ", l.now())
message = fmt.Sprintf(message+format+"\n", v...)
logfile.WriteString(message)
fmt.Printf(strings.Replace(message, "%", "%%", -1))
}
}
func (l *log) Warning(format string, v ...interface{}) {
if l.warn && l.enable {
message := fmt.Sprintf("%s SYST WARN ", l.now())
message = fmt.Sprintf(message+format+"\n", v...)
logfile.WriteString(message)
fmt.Printf(strings.Replace(message, "%", "%%", -1))
}
}
func (l *log) Error(format string, v ...interface{}) {
if l.error && l.enable {
message := fmt.Sprintf("%s SYST ERROR ", l.now())
message = fmt.Sprintf(message+format+"\n", v...)
logfile.WriteString(message)
fmt.Printf(strings.Replace(message, "%", "%%", -1))
}
}
func (l *log) Debug(format string, v ...interface{}) {
if l.debug && l.enable {
message := fmt.Sprintf("%s SYST DEBUG ", l.now())
message = fmt.Sprintf(message+format+"\n", v...)
logfile.WriteString(message)
fmt.Printf(strings.Replace(message, "%", "%%", -1))
}
}
func (l *log) Stdout(format string, v ...interface{}) {
message := fmt.Sprintf("%s ", l.now())
message = fmt.Sprintf(message+format+"\n", v...)
logfile.WriteString(message)
fmt.Printf(strings.Replace(message, "%", "%%", -1))
}
func (l *log) now() string {
return time.Now().Format("2006/01/02 15:04:05")
}
func (l *log) Close() {
logfile.Close()
}
func (l *log) SetVisibility(str string) {
switch str {
case "WARNING":
l.debug = false
l.info = false
l.warn = true
l.error = true
case "ERROR":
l.debug = false
l.info = false
l.warn = false
l.error = true
case "DEBUG":
l.debug = true
l.info = true
l.warn = true
l.error = true
case "INFO":
l.debug = false
l.info = true
l.warn = true
l.error = true
default:
l.debug = false
l.info = true
l.warn = true
l.error = true
}
}
func (l *log) Enable(val bool) {
l.enable = val
}