Blame view
main.go
4.33 KB
a99c98307 first commit |
1 2 3 4 |
package gologger import ( "fmt" |
df74f4a7e new filename format |
5 |
"net/http" |
cccb4b47c log http response |
6 |
"net/http/httputil" |
a99c98307 first commit |
7 |
"os" |
e025a8738 platform agnostic... |
8 |
"path/filepath" |
98708cdaf new version, does... |
9 |
"runtime" |
aa60a45ee changed log file ... |
10 |
"strings" |
3ba7b5695 added mutex locks... |
11 |
"sync" |
baa4468b7 Comments go befor... |
12 |
"time" |
a99c98307 first commit |
13 |
) |
434e7da25 changed initializ... |
14 15 16 17 18 19 |
const ( MaxLogSize5MB int64 = 5 * 1024 * 1024 MaxLogSize1MB int64 = 1 * 1024 * 1024 MaxLogSize500KB int64 = 500 * 1024 MaxLogSize100KB int64 = 100 * 1024 MaxLogSize512B int64 = 512 |
98cf2ceeb new interaface; a... |
20 |
logDirName = "log" |
434e7da25 changed initializ... |
21 |
) |
a99c98307 first commit |
22 |
|
5eaa49d2a new design; allow... |
23 |
type Logger struct { |
98cf2ceeb new interaface; a... |
24 25 |
mu *sync.Mutex outputFile *os.File |
23ce66f0f optional init par... |
26 |
|
98cf2ceeb new interaface; a... |
27 |
outputFileName string |
3e936a3c2 output file name ... |
28 |
fullName string |
98cf2ceeb new interaface; a... |
29 |
maxFileSize int64 |
df74f4a7e new filename format |
30 31 |
splitCount int |
a99c98307 first commit |
32 |
} |
98cf2ceeb new interaface; a... |
33 |
func New(name string, maxFileSize int64) (logger *Logger, err error) { |
5eaa49d2a new design; allow... |
34 |
logger = &Logger{} |
23ce66f0f optional init par... |
35 |
|
98cf2ceeb new interaface; a... |
36 37 38 |
logger.outputFileName = name + "-log" logger.mu = &sync.Mutex{} logger.maxFileSize = maxFileSize |
a99c98307 first commit |
39 |
|
98cf2ceeb new interaface; a... |
40 41 42 43 44 |
err = os.Mkdir(logDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { fmt.Printf("logger: mkdir: couldn't create event log directory ") |
5eaa49d2a new design; allow... |
45 |
return nil, err |
a99c98307 first commit |
46 47 |
} } |
df74f4a7e new filename format |
48 |
date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) |
3e936a3c2 output file name ... |
49 50 51 |
logger.outputFileName += "_" + date logger.fullName = logger.outputFileName + ".txt" path := filepath.Join(logDirName, logger.fullName) |
98cf2ceeb new interaface; a... |
52 53 54 55 56 |
logger.outputFile, err = os.Create(path) if err != nil { fmt.Printf("logger: new: couldn't create event log file ") return nil, err |
e025a8738 platform agnostic... |
57 |
} |
5eaa49d2a new design; allow... |
58 |
return logger, nil |
98708cdaf new version, does... |
59 |
} |
df74f4a7e new filename format |
60 |
func (l *Logger) Print(format string, v ...interface{}) { |
98cf2ceeb new interaface; a... |
61 62 63 |
msg := fmt.Sprintf(format, v...) fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + " ") |
327c98634 added Print() for... |
64 |
} |
df74f4a7e new filename format |
65 |
func (l *Logger) Log(format string, v ...interface{}) { |
372beaf62 refactored if sta... |
66 67 68 69 70 71 72 73 74 75 76 77 |
if l.outputFile == nil { return } l.mu.Lock() defer l.mu.Unlock() msg := fmt.Sprintf(format, v...) s := time.Now().Format(time.RFC3339) + ": " + msg + " " if l.shouldSplit(len(s)) { l.split() |
23ce66f0f optional init par... |
78 |
} |
372beaf62 refactored if sta... |
79 |
l.outputFile.WriteString(s) |
98708cdaf new version, does... |
80 |
} |
372beaf62 refactored if sta... |
81 82 83 84 |
func (l *Logger) RequestLog(req *http.Request, userid string) string { if l.outputFile == nil { return "" } |
98cf2ceeb new interaface; a... |
85 |
|
372beaf62 refactored if sta... |
86 87 88 |
if userid == "" { userid = "-" } |
df74f4a7e new filename format |
89 |
|
372beaf62 refactored if sta... |
90 91 92 93 94 95 96 |
var b strings.Builder b.WriteString("Request: ") // CLF-like header ts := time.Now().Format(time.RFC3339) fmt.Fprintf(&b, "%s %s ", req.RemoteAddr, ts) |
cccb4b47c log http response |
97 |
|
3e936a3c2 output file name ... |
98 99 100 101 102 103 104 105 106 107 108 109 110 |
headers := req.Header content := headers.Get("Content-Type") if content != "application/json" { b.WriteString(fmt.Sprintf("%s %s ", req.Method, req.URL)) for k, h := range headers { b.WriteString(k + ": ") for _, h2 := range h { b.WriteString(h2 + " ") } b.WriteString(" ") } |
372beaf62 refactored if sta... |
111 |
} else { |
3e936a3c2 output file name ... |
112 113 114 115 116 117 118 |
body, err := httputil.DumpRequest(req, true) if err != nil { fmt.Fprintf(os.Stderr, "%v ", err) } else { b.WriteString(string(body)) } |
372beaf62 refactored if sta... |
119 120 121 122 |
} b.WriteString(" ") |
cccb4b47c log http response |
123 |
|
372beaf62 refactored if sta... |
124 125 126 127 |
msg := b.String() return msg } |
ed6549a33 minor output form... |
128 |
func (l *Logger) ResponseLog(status int, duration time.Duration, size int64) string { |
372beaf62 refactored if sta... |
129 130 |
if l.outputFile == nil { return "" |
cccb4b47c log http response |
131 |
} |
372beaf62 refactored if sta... |
132 133 |
var b strings.Builder |
ed6549a33 minor output form... |
134 135 136 137 138 |
fmt.Fprintf(&b, "Response: %d %v %dB ", status, duration, size) b.WriteString("============================================================== ") |
372beaf62 refactored if sta... |
139 140 141 |
msg := b.String() return msg |
cccb4b47c log http response |
142 |
} |
372beaf62 refactored if sta... |
143 144 145 146 |
func (l *Logger) LogHTTPTraffic(in string, out string) { if l.outputFile == nil { return } |
df74f4a7e new filename format |
147 |
|
372beaf62 refactored if sta... |
148 |
msg := in + out |
df74f4a7e new filename format |
149 |
|
372beaf62 refactored if sta... |
150 151 152 153 154 |
l.mu.Lock() defer l.mu.Unlock() if l.shouldSplit(len(msg)) { l.split() |
98708cdaf new version, does... |
155 |
} |
372beaf62 refactored if sta... |
156 |
l.outputFile.WriteString(msg) |
98708cdaf new version, does... |
157 |
} |
df74f4a7e new filename format |
158 |
func (l *Logger) Trace(format string, v ...interface{}) { |
372beaf62 refactored if sta... |
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
if l.outputFile == nil { return } l.mu.Lock() defer l.mu.Unlock() _, file, line, ok := runtime.Caller(1) s := "" msg := fmt.Sprintf(format, v...) if ok { s = fmt.Sprintf("%s: %s %d: %s ", time.Now().Format(time.RFC3339), file, line, msg) } else { s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + " ") |
98708cdaf new version, does... |
175 |
} |
372beaf62 refactored if sta... |
176 177 178 179 |
if l.shouldSplit(len(s)) { l.split() } l.outputFile.WriteString(s) |
a99c98307 first commit |
180 |
} |
df74f4a7e new filename format |
181 |
func (l *Logger) Close() { |
372beaf62 refactored if sta... |
182 183 184 185 186 187 188 189 |
if l.outputFile == nil { return } err := l.outputFile.Close() if err != nil { fmt.Printf("logger: on exit: couldn't close event log file ") |
a99c98307 first commit |
190 |
} |
a99c98307 first commit |
191 |
} |
98cf2ceeb new interaface; a... |
192 |
func (l *Logger) split() { |
372beaf62 refactored if sta... |
193 194 195 |
if l.outputFile == nil { return } |
98cf2ceeb new interaface; a... |
196 197 198 199 200 201 202 |
// close old file err := l.outputFile.Close() if err != nil { fmt.Printf("logger: split: couldn't close event file ") return } |
df74f4a7e new filename format |
203 204 |
l.splitCount++ |
98cf2ceeb new interaface; a... |
205 206 |
// open new file var errnew error |
3e936a3c2 output file name ... |
207 |
path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") |
98cf2ceeb new interaface; a... |
208 |
l.outputFile, errnew = os.Create(path) |
a99c98307 first commit |
209 |
|
98cf2ceeb new interaface; a... |
210 211 212 |
if errnew != nil { fmt.Printf("logger: split: couldn't create event log file ") |
a99c98307 first commit |
213 214 |
} } |
98cf2ceeb new interaface; a... |
215 216 217 |
func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
a99c98307 first commit |
218 |
} |