Commit 92485819d1dce07b76d7ef21ebecd8ad6ae9d8af
1 parent
feba660942
Exists in
master
lint
Showing
1 changed file
with
48 additions
and
30 deletions
Show diff stats
main.go
... | ... | @@ -12,6 +12,7 @@ import ( |
12 | 12 | "time" |
13 | 13 | ) |
14 | 14 | |
15 | +// Block ... | |
15 | 16 | const ( |
16 | 17 | MaxLogSize5MB int64 = 5 * 1024 * 1024 |
17 | 18 | MaxLogSize1MB int64 = 1 * 1024 * 1024 |
... | ... | @@ -21,6 +22,7 @@ const ( |
21 | 22 | logDirName = "log" |
22 | 23 | ) |
23 | 24 | |
25 | +// Logger ... | |
24 | 26 | type Logger struct { |
25 | 27 | mu *sync.Mutex |
26 | 28 | outputFile *os.File |
... | ... | @@ -32,6 +34,7 @@ type Logger struct { |
32 | 34 | splitCount int |
33 | 35 | } |
34 | 36 | |
37 | +// New ... | |
35 | 38 | func New(name string, maxFileSize int64) (logger *Logger, err error) { |
36 | 39 | logger = &Logger{} |
37 | 40 | |
... | ... | @@ -60,6 +63,7 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { |
60 | 63 | return logger, nil |
61 | 64 | } |
62 | 65 | |
66 | +// Log ... | |
63 | 67 | func (l *Logger) Log(format string, v ...interface{}) { |
64 | 68 | if l.outputFile == nil { |
65 | 69 | return |
... | ... | @@ -76,6 +80,46 @@ func (l *Logger) Log(format string, v ...interface{}) { |
76 | 80 | l.outputFile.WriteString(s) |
77 | 81 | } |
78 | 82 | |
83 | +// Print ... | |
84 | +func (l *Logger) Print(format string, v ...interface{}) { | |
85 | + msg := fmt.Sprintf(format, v...) | |
86 | + fmt.Printf("%s: %s\n", time.Now().Format(time.RFC3339), msg) | |
87 | +} | |
88 | + | |
89 | +// PrintTrace ... | |
90 | +func (l *Logger) PrintTrace(format string, v ...interface{}) { | |
91 | + _, file, line, _ := runtime.Caller(1) | |
92 | + | |
93 | + msg := fmt.Sprintf(format, v...) | |
94 | + fmt.Printf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | |
95 | +} | |
96 | + | |
97 | +// Trace ... | |
98 | +func (l *Logger) Trace(format string, v ...interface{}) { | |
99 | + if l.outputFile == nil { | |
100 | + return | |
101 | + } | |
102 | + | |
103 | + l.mu.Lock() | |
104 | + defer l.mu.Unlock() | |
105 | + | |
106 | + _, file, line, _ := runtime.Caller(1) | |
107 | + msg := fmt.Sprintf(format, v...) | |
108 | + s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | |
109 | + | |
110 | + if l.shouldSplit(len(s)) { | |
111 | + l.split() | |
112 | + } | |
113 | + l.outputFile.WriteString(s) | |
114 | +} | |
115 | + | |
116 | +// PrintAndTrace ... | |
117 | +func (l *Logger) PrintAndTrace(format string, v ...interface{}) { | |
118 | + l.Print(format, v) | |
119 | + l.Trace(format, v) | |
120 | +} | |
121 | + | |
122 | +// LogHTTPRequest ... | |
79 | 123 | func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { |
80 | 124 | if userID == "" { |
81 | 125 | userID = "-" |
... | ... | @@ -98,10 +142,12 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { |
98 | 142 | |
99 | 143 | const splitLine = "==============================================================" |
100 | 144 | |
145 | +// LogHTTPResponse ... | |
101 | 146 | func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { |
102 | 147 | return fmt.Sprintf("Response:\n%d %v %dB\n%s\n", status, duration, size, splitLine) |
103 | 148 | } |
104 | 149 | |
150 | +// CombineHTTPLogs ... | |
105 | 151 | func (l *Logger) CombineHTTPLogs(in string, out string) { |
106 | 152 | if l.outputFile == nil { |
107 | 153 | return |
... | ... | @@ -117,36 +163,7 @@ func (l *Logger) CombineHTTPLogs(in string, out string) { |
117 | 163 | l.outputFile.WriteString(msg) |
118 | 164 | } |
119 | 165 | |
120 | -func (l *Logger) PrintTrace(format string, v ...interface{}) { | |
121 | - _, file, line, _ := runtime.Caller(1) | |
122 | - | |
123 | - msg := fmt.Sprintf(format, v...) | |
124 | - fmt.Printf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | |
125 | -} | |
126 | - | |
127 | -func (l *Logger) Print(format string, v ...interface{}) { | |
128 | - msg := fmt.Sprintf(format, v...) | |
129 | - fmt.Printf("%s: %s\n", time.Now().Format(time.RFC3339), msg) | |
130 | -} | |
131 | - | |
132 | -func (l *Logger) Trace(format string, v ...interface{}) { | |
133 | - if l.outputFile == nil { | |
134 | - return | |
135 | - } | |
136 | - | |
137 | - l.mu.Lock() | |
138 | - defer l.mu.Unlock() | |
139 | - | |
140 | - _, file, line, _ := runtime.Caller(1) | |
141 | - msg := fmt.Sprintf(format, v...) | |
142 | - s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | |
143 | - | |
144 | - if l.shouldSplit(len(s)) { | |
145 | - l.split() | |
146 | - } | |
147 | - l.outputFile.WriteString(s) | |
148 | -} | |
149 | - | |
166 | +// Close ... | |
150 | 167 | func (l *Logger) Close() { |
151 | 168 | if l.outputFile == nil { |
152 | 169 | return |
... | ... | @@ -185,3 +202,4 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool { |
185 | 202 | stats, _ := l.outputFile.Stat() |
186 | 203 | return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
187 | 204 | } |
205 | + | ... | ... |