This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_log.go
136 lines (123 loc) · 3 KB
/
file_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gsimplelog
import (
"fmt"
"os"
"sync"
"path/filepath"
"time"
"io"
"strconv"
)
func NewFileLogger(filePrefix, filePath string, level, multiSize int) (ILogger, error) {
err := os.MkdirAll(filePath, os.ModePerm)
if err != nil {
fmt.Println("创建日志目录失败 ", err.Error())
return nil, err
}
lf := &LogFile{
path: filePath,
prefix: filePrefix,
multiSize: multiSize,
}
err = lf.Create()
if err != nil {
return nil, err
}
l := &FileLogger{
Level: level,
Out: lf,
}
return l, nil
}
type FileLogger struct {
Out io.WriteCloser
Level int
}
func (l *FileLogger) SetLevel(level int) {
l.Level = level
}
func (l *FileLogger) Trace(params ...interface{}) {
if l.Level <= LogTrace {
l.Out.Write([]byte((fmt.Sprintf("%s [TRACE] %s\n", Now(), fmt.Sprint(params ...)))))
}
}
func (l *FileLogger) Debug(params ...interface{}) {
if l.Level <= LogDebug {
l.Out.Write([]byte((fmt.Sprintf("%s [DEBUG] %s\n", Now(), fmt.Sprint(params ...)))))
}
}
func (l *FileLogger) Info(params ...interface{}) {
if l.Level <= LogInfo {
l.Out.Write([]byte((fmt.Sprintf("%s [INFO] %s\n", Now(), fmt.Sprint(params ...)))))
}
}
func (l *FileLogger) Error(params ...interface{}) {
if l.Level <= LogError {
l.Out.Write([]byte((fmt.Sprintf("%s [ERROR] %s\n", Now(), fmt.Sprint(params ...)))))
}
}
func (l *FileLogger) Tracef(format string, params ...interface{}) {
if l.Level <= LogTrace {
l.Out.Write([]byte((fmt.Sprintf("%s [TRACE] %s\n", Now(), fmt.Sprintf(format, params...)))))
}
}
func (l *FileLogger) Debugf(format string, params ...interface{}) {
if l.Level <= LogDebug {
l.Out.Write([]byte((fmt.Sprintf("%s [DEBUG] %s\n", Now(), fmt.Sprintf(format, params...)))))
}
}
func (l *FileLogger) Infof(format string, params ...interface{}) {
if l.Level <= LogInfo {
l.Out.Write([]byte((fmt.Sprintf("%s [INFO] %s\n", Now(), fmt.Sprintf(format, params...)))))
}
}
func (l *FileLogger) Errorf(format string, params ...interface{}) {
if l.Level <= LogError {
l.Out.Write([]byte((fmt.Sprintf("%s [ERROR] %s\n", Now(), fmt.Sprintf(format, params...)))))
}
}
func (l *FileLogger) Close() error {
return l.Out.Close()
}
type LogFile struct {
path string
prefix string
file *os.File
multiSize int
size int
index int
sync.RWMutex
}
func (l *LogFile) Write(b []byte) (int, error) {
l.Lock()
defer l.Unlock()
n, err := l.file.Write(b)
l.size += n
if l.size > l.multiSize {
l.file.Close()
l.Create()
l.size = 0
}
return n, err
}
func (l *LogFile) Create() error {
l.index ++
var filename string
if l.index <= 1 {
filename = l.prefix + "_" + time.Now().Format("20060102") + ".log"
} else {
filename = l.prefix + "_" + time.Now().Format("20060102") + "_" + strconv.Itoa(l.index) + ".log"
}
fullpath := filepath.Join(l.path, filename)
file, err := os.OpenFile(fullpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
l.file = file
return nil
}
func (l *LogFile) Close() error {
l.Lock()
defer l.Unlock()
return l.file.Close()
}