-
Notifications
You must be signed in to change notification settings - Fork 53
/
file.go
81 lines (73 loc) · 1.9 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
package zlog
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/ztime"
)
func openFile(filepa string, archive bool) (file *zfile.MemoryFile, fileName, fileDir string, err error) {
fullPath := zfile.RealPath(filepa)
fileDir, fileName = filepath.Split(fullPath)
opt := []zfile.MemoryFileOption{zfile.MemoryFileAutoFlush(1)}
if archive {
ext := filepath.Ext(fileName)
base := strings.TrimSuffix(fileName, ext)
fileDir = zfile.RealPathMkdir(fileDir+base, true)
fullPath = fileDir + fileName
opt = append(opt, zfile.MemoryFileFlushBefore(func(f *zfile.MemoryFile) error {
archiveName := ztime.FormatTime(time.Now(), "Y-m-d")
fileName = archiveName + ext
f.SetName(fileDir + fileName)
return nil
}))
}
f := zfile.NewMemoryFile(fullPath, opt...)
return f, fileName, fileDir, nil
}
// SetFile Setting log file output
func (log *Logger) SetFile(filepath string, archive ...bool) {
log.DisableConsoleColor()
logArchive := len(archive) > 0 && archive[0]
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
}
}