-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
80 lines (68 loc) · 1.71 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
package enlog
import (
"fmt"
"io"
"log"
"os"
)
type logger struct {
handler *log.Logger
prefix string
useFile bool
filePath string
color uint8
}
// NewHandler returns *log.Logger as a log handler
func (lg *logger) NewHandler() *log.Logger {
mws := []io.Writer{os.Stdout}
if lg.useFile {
file, err := os.OpenFile(lg.filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("Error opening file: %v", err)
lg.useFile = false
} else {
mws = append(mws, file)
}
}
// defer infoFile.Close()
out := io.MultiWriter(mws...)
return log.New(out, fmt.Sprintf("%s: ", lg.prefix), log.Ldate|log.LstdFlags|log.Lshortfile)
}
// GetPrefix returns prefix of specified logger type
func (lg *logger) GetPrefix() string {
return lg.prefix
}
// SetPrefix sets prefix of specified logger type
func (lg *logger) SetPrefix(prefix string) {
lg.prefix = prefix
lg.handler = lg.NewHandler()
}
// GetUseFile returns information
// if file is used to save log
func (lg *logger) GetUseFile() bool {
return lg.useFile
}
// SetUseFile sets information
// if file is used to save log
func (lg *logger) SetUseFile(useFile bool) {
lg.useFile = useFile
lg.handler = lg.NewHandler()
}
// GetFilePath returns filepath of specified logger type
func (lg *logger) GetFilePath() string {
return lg.filePath
}
// SetFilePath sets filepath of specified logger type
func (lg *logger) SetFilePath(filePath string) {
lg.filePath = filePath
lg.handler = lg.NewHandler()
}
// GetColor returns color of specified logger type
func (lg *logger) GetColor() uint8 {
return lg.color
}
// SetColor sets color of specified logger type
func (lg *logger) SetColor(color uint8) {
lg.color = color
lg.handler = lg.NewHandler()
}