-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
76 lines (62 loc) · 1.48 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
package accter
import (
"fmt"
"sync"
"time"
)
type Logger struct {
timeformat string
appName string
level Level
}
type Level int
const (
_ Level = iota
Error
Warn
Info
Debug
Trace
)
func (l *Logger) trace(message string, args ...interface{}) {
if l.level >= Trace {
l.log(fmt.Sprintf(" %s | TRACE | %s", l.createTimeStamp(), fmt.Sprintf(message, args...)))
}
}
func (l *Logger) debug(message string, args ...interface{}) {
if l.level >= Debug {
l.log(fmt.Sprintf(" %s | DEBUG | %s", l.createTimeStamp(), fmt.Sprintf(message, args...)))
}
}
func (l *Logger) info(message string, args ...interface{}) {
if l.level >= Info {
l.log(fmt.Sprintf(" %s | INFO | %s", l.createTimeStamp(), fmt.Sprintf(message, args...)))
}
}
func (l *Logger) warn(message string, args ...interface{}) {
if l.level >= Warn {
l.log(fmt.Sprintf(" %s | WARN | %s", l.createTimeStamp(), fmt.Sprintf(message, args...)))
}
}
func (l *Logger) error(message string, args ...interface{}) {
if l.level >= Error {
l.log(fmt.Sprintf(" %s | ERROR | %s", l.createTimeStamp(), fmt.Sprintf(message, args...)))
}
}
func (l *Logger) createTimeStamp() string {
return time.Now().Format(l.timeformat)
}
func (l *Logger) log(message string) {
fmt.Printf("[%s]%s\n", l.appName, message)
}
var logger *Logger
var once sync.Once
func CreateLogger(level Level) {
once.Do(func() {
logger = &Logger{
timeformat: "2006-01-02 15:04:05.000",
appName: "ACCTER",
level: level,
}
})
}