-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
40 lines (32 loc) · 870 Bytes
/
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
package main
import (
"fmt"
"os"
"time"
)
const logDirPath = "logs"
func GetLogFile() *os.File {
if _, err := os.Stat(logDirPath); os.IsNotExist(err) {
os.Mkdir(logDirPath, os.ModeDir)
}
f, err := os.OpenFile(fmt.Sprintf("%v/access.log", logDirPath), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}
func ErrorLogger(err error) {
if _, err := os.Stat(logDirPath); os.IsNotExist(err) {
os.Mkdir(logDirPath, os.ModeDir)
}
f, err := os.OpenFile(fmt.Sprintf("%v/error.log", logDirPath), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
f.WriteString(fmt.Sprintf("%v %v\n", getCurrentTime(), err.Error()))
return
}
func getCurrentTime() string {
t := time.Now()
return fmt.Sprintf("%02d-%02d-%02d %02d:%02d:%02d", t.Year()%100, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}