-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
44 lines (39 loc) · 840 Bytes
/
handler.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
package logger
import (
"log"
"os"
)
type handler struct {
fileHandler *os.File
logger *log.Logger
}
func (this *handler) Open(filepath string) {
if filepath == "" {
this.logger = log.New(os.Stdout, "", log.Ldate|log.Ltime)
} else {
if fileHandler, err := os.OpenFile(
filepath,
os.O_RDWR|os.O_APPEND|os.O_CREATE,
0666,
); err != nil {
panic(err)
} else {
this.fileHandler = fileHandler
this.logger = log.New(fileHandler, "", log.Ldate|log.Ltime)
}
}
}
func (this *handler) Close() {
if this.fileHandler != nil {
this.fileHandler.Close()
this.fileHandler = nil
}
this.logger = nil
}
func (this *handler) Println(message ...interface{}) {
if this.logger == nil {
panic("This is a bug, logger is nil, please create an issue to tell us.")
} else {
this.logger.Println(message...)
}
}