-
Notifications
You must be signed in to change notification settings - Fork 53
/
file.go
82 lines (72 loc) · 1.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
package zlog
import (
"github.com/sohaha/zlsgo/zfile"
"io"
"os"
"path"
"strconv"
"strings"
"time"
)
func openFile(fileDir, fileName string) (file *os.File, err error) {
_ = mkdirLog(fileDir)
fullPath := fileDir + "/" + fileName
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
}
// SetLogFile Setting log file output
func (log *Logger) SetLogFile(fileDir string, fileName string) {
file, _ := openFile(fileDir, fileName)
log.DisableConsoleColor()
log.mu.Lock()
defer log.mu.Unlock()
log.CloseFile()
log.file = file
log.out = file
log.FileMaxSize = 0
log.fileDir = fileDir
log.fileName = fileName
}
func (log *Logger) SetSaveLogFile(fileDir string, fileName string) {
log.SetLogFile(fileDir, fileName)
log.fileAndStdout = true
log.out = io.MultiWriter(log.file, os.Stdout)
}
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
}