-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
170 lines (159 loc) · 2.84 KB
/
logger.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package logger
import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
type (
BasicLogger interface {
SetOutput(w io.Writer)
Print(v ...any)
Printf(format string, v ...any)
}
DebugLogger interface {
Debug(v ...any)
}
DebugfLogger interface {
Debugf(format string, v ...any)
}
InfoLogger interface {
Info(v ...any)
}
InfofLogger interface {
Infof(format string, v ...any)
}
WarnLogger interface {
Warn(v ...any)
}
WarnfLogger interface {
Warnf(format string, v ...any)
}
ErrorLogger interface {
Error(v ...any)
}
ErrorfLogger interface {
Errorf(format string, v ...any)
}
TraceLogger interface {
Trace(v ...any)
}
TracefLogger interface {
Tracef(format string, v ...any)
}
FatalLogger interface {
Fatal(v ...any)
}
FatalfLogger interface {
Fatalf(format string, v ...any)
}
PanicLogger interface {
Panic(v ...any)
}
PanicfLogger interface {
Panicf(format string, v ...any)
}
LevelsLogger interface {
SetLevel(Level)
Level()(Level)
}
Logger interface {
BasicLogger
DebugLogger
DebugfLogger
InfoLogger
InfofLogger
WarnLogger
WarnfLogger
ErrorLogger
ErrorfLogger
TraceLogger
TracefLogger
FatalLogger
FatalfLogger
PanicLogger
PanicfLogger
LevelsLogger
}
)
func Unwrap(l BasicLogger)(v any){
if u, ok := l.(interface{ Unwrap()(BasicLogger) }); ok {
return Unwrap(u.Unwrap())
}
if u, ok := l.(interface{ Unwrap()(any) }); ok {
return u.Unwrap()
}
return l
}
func OutputToFile(l BasicLogger, filename string, outs ...io.Writer)(out io.Writer, err error){
if err = checkAndMoveLogs(filename); err != nil {
return
}
dir := filepath.Dir(filename)
if !fileExist(dir) {
if err = os.MkdirAll(dir, 0755); err != nil {
return
}
}
if out, err = os.OpenFile(filename, os.O_RDWR | os.O_CREATE | os.O_EXCL, 0666); err != nil {
return
}
if len(outs) > 0 {
out = io.MultiWriter(append(outs, out)...)
}
if l != nil {
l.SetOutput(out)
}
return
}
func fileExist(filename string)(bool){
if _, err := os.Stat(filename); err != nil {
return errors.Is(err, os.ErrExist)
}
return true
}
func copyLogToGzip(filename string, r io.Reader)(err error){
t := filename + ".2.gz"
{
i := 2
n := t
for fileExist(n) {
i++
n = fmt.Sprintf(filename + ".%d.gz", i)
}
for i > 2 {
i--
m := fmt.Sprintf(filename + ".%d.gz", i)
if err = os.Rename(m, n); err != nil {
return
}
n = m
}
}
fd, err := os.Create(t)
if err != nil {
return
}
defer fd.Close()
w := gzip.NewWriter(fd)
defer w.Close()
_, err = io.Copy(w, r)
return
}
func checkAndMoveLogs(filename string)(err error){
if !fileExist(filename) {
return
}
t := filename + ".1"
fd, err := os.Open(t)
if err == nil {
if err = copyLogToGzip(filename, fd); err != nil {
return
}
}else if !errors.Is(err, os.ErrNotExist) {
return
}
return os.Rename(filename, t)
}