-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
61 lines (46 loc) · 1.13 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
package contact
import (
"github.com/gin-gonic/gin"
lib "github.com/maxiloEmmmm/go-tool"
"io"
"log"
"os"
"time"
)
var configInstance configIO
var LogPath = "logs"
func InitLog() {
configInstance := configIO{}
gin.DefaultWriter = configInstance
}
type configIO struct {
file *os.File
key string
}
func (config configIO) Write(p []byte) (n int, err error) {
return io.MultiWriter(config.GetLogFile(), os.Stdout).Write(p)
}
func (config *configIO) Close() error {
return config.file.Close()
}
func (config *configIO) GetLogFile() (file *os.File) {
key := time.Now().Format("2006-01-02")
if key != config.key || config.file == nil {
if err := os.MkdirAll(LogPath, 0744); err != nil {
log.Fatalln("日志文件夹创建失败: " + err.Error())
}
file, err := os.OpenFile(lib.StringJoin(LogPath, "/access_", key, ".log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln("日志文件打开失败: " + err.Error())
}
if config.file != nil {
config.file.Close()
}
config.file = file
config.key = key
}
return config.file
}
func LogClose() error {
return configInstance.Close()
}