-
Notifications
You must be signed in to change notification settings - Fork 55
/
file.go
120 lines (109 loc) · 2.68 KB
/
file.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package zlog
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/ztime"
"github.com/sohaha/zlsgo/ztime/cron"
)
func openFile(filepa string, archive bool) (file *os.File, fileName, fileDir string, err error) {
fullPath := zfile.RealPath(filepa)
fileDir, fileName = filepath.Split(fullPath)
if archive {
archiveName := ztime.FormatTime(time.Now(), "Y-m-d")
ext := filepath.Ext(fileName)
base := strings.TrimSuffix(fileName, ext)
fileDir = zfile.RealPathMkdir(fileDir+base, true)
fileName = archiveName + ext
fullPath = fileDir + fileName
}
_ = mkdirLog(fileDir)
if zfile.FileExist(fullPath) {
file, err = os.OpenFile(fullPath, os.O_APPEND|os.O_RDWR, 0644)
} else {
file, err = os.OpenFile(fullPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
}
if err != nil {
return nil, "", "", err
}
return
}
// SetFile Setting log file output
func (log *Logger) SetFile(filepath string, archive ...bool) {
log.DisableConsoleColor()
logArchive := len(archive) > 0 && archive[0]
if logArchive {
c := cron.New()
_, _ = c.Add("0 0 * * *", func() {
log.setLogfile(filepath, logArchive)
})
c.Run()
}
log.setLogfile(filepath, logArchive)
}
func (log *Logger) setLogfile(filepath string, archive bool) {
fileObj, fileName, fileDir, _ := openFile(filepath, archive)
log.mu.Lock()
defer log.mu.Unlock()
log.CloseFile()
log.file = fileObj
log.fileDir = fileDir
log.fileName = fileName
if log.fileAndStdout {
log.out = io.MultiWriter(log.file, os.Stdout)
} else {
log.out = fileObj
}
}
func (log *Logger) Discard() {
log.mu.Lock()
log.out = ioutil.Discard
if log.file != nil {
_ = log.file.Close()
}
log.level = LogNot
log.mu.Unlock()
}
func (log *Logger) SetSaveFile(filepath string, archive ...bool) {
log.SetFile(filepath, archive...)
log.mu.Lock()
log.fileAndStdout = true
log.out = io.MultiWriter(log.file, os.Stdout)
log.mu.Unlock()
}
func (log *Logger) CloseFile() {
if log.file != nil {
_ = log.file.Close()
log.file = nil
log.out = os.Stderr
}
}
// func oldLogFile(fileDir, fileName string) string {
// ext := path.Ext(fileName)
// name := strings.TrimSuffix(fileName, ext)
// timeStr := time.Now().Format("2006-01-02")
// oldLogFile := fileDir + "/" + name + "." + timeStr + ext
// judge:
// for {
// if !zfile.FileExist(oldLogFile) {
// break judge
// } else {
// oldLogFile = fileDir + "/" + name + "." + timeStr + "_" + strconv.Itoa(int(time.Now().UnixNano())) + ext
// }
// }
//
// return oldLogFile
// }
func mkdirLog(dir string) (e error) {
if zfile.DirExist(dir) {
return
}
if err := os.MkdirAll(dir, 0775); err != nil && os.IsPermission(err) {
e = err
}
return
}