Blame view
main.go
3.93 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 28 |
outputFileName string maxFileSize int64 |
df74f4a7e new filename format |
29 30 |
splitCount int |
a99c98307 first commit |
31 |
} |
98cf2ceeb new interaface; a... |
32 |
func New(name string, maxFileSize int64) (logger *Logger, err error) { |
5eaa49d2a new design; allow... |
33 |
logger = &Logger{} |
23ce66f0f optional init par... |
34 |
|
98cf2ceeb new interaface; a... |
35 36 37 |
logger.outputFileName = name + "-log" logger.mu = &sync.Mutex{} logger.maxFileSize = maxFileSize |
a99c98307 first commit |
38 |
|
98cf2ceeb new interaface; a... |
39 40 41 42 43 |
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... |
44 |
return nil, err |
a99c98307 first commit |
45 46 |
} } |
df74f4a7e new filename format |
47 48 49 |
date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) logger.outputFileName += "_" + date + ".txt" path := filepath.Join(logDirName, logger.outputFileName) |
98cf2ceeb new interaface; a... |
50 51 52 53 54 |
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... |
55 |
} |
5eaa49d2a new design; allow... |
56 |
return logger, nil |
98708cdaf new version, does... |
57 |
} |
df74f4a7e new filename format |
58 |
func (l *Logger) Print(format string, v ...interface{}) { |
98cf2ceeb new interaface; a... |
59 60 61 |
msg := fmt.Sprintf(format, v...) fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + " ") |
327c98634 added Print() for... |
62 |
} |
df74f4a7e new filename format |
63 |
func (l *Logger) Log(format string, v ...interface{}) { |
372beaf62 refactored if sta... |
64 65 66 67 68 69 70 71 72 73 74 75 |
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... |
76 |
} |
372beaf62 refactored if sta... |
77 |
l.outputFile.WriteString(s) |
98708cdaf new version, does... |
78 |
} |
372beaf62 refactored if sta... |
79 80 81 82 |
func (l *Logger) RequestLog(req *http.Request, userid string) string { if l.outputFile == nil { return "" } |
98cf2ceeb new interaface; a... |
83 |
|
372beaf62 refactored if sta... |
84 85 86 |
if userid == "" { userid = "-" } |
df74f4a7e new filename format |
87 |
|
372beaf62 refactored if sta... |
88 89 90 91 92 93 94 |
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 |
95 |
|
372beaf62 refactored if sta... |
96 97 98 99 100 101 102 103 104 105 |
body, err := httputil.DumpRequest(req, true) if err != nil { fmt.Fprintf(os.Stderr, "%v ", err) } else { b.WriteString(string(body)) } b.WriteString(" ") |
cccb4b47c log http response |
106 |
|
372beaf62 refactored if sta... |
107 108 109 110 111 112 113 114 |
msg := b.String() return msg } func (l *Logger) ResponseLog(status int, duration time.Duration) string { if l.outputFile == nil { return "" |
cccb4b47c log http response |
115 |
} |
372beaf62 refactored if sta... |
116 117 118 119 120 121 122 123 124 125 |
var b strings.Builder fmt.Fprintf(&b, "Response: %d %v ", status, duration) b.WriteString("============================================================== ") msg := b.String() return msg |
cccb4b47c log http response |
126 |
} |
372beaf62 refactored if sta... |
127 128 129 130 |
func (l *Logger) LogHTTPTraffic(in string, out string) { if l.outputFile == nil { return } |
df74f4a7e new filename format |
131 |
|
372beaf62 refactored if sta... |
132 |
msg := in + out |
df74f4a7e new filename format |
133 |
|
372beaf62 refactored if sta... |
134 135 136 137 138 |
l.mu.Lock() defer l.mu.Unlock() if l.shouldSplit(len(msg)) { l.split() |
98708cdaf new version, does... |
139 |
} |
372beaf62 refactored if sta... |
140 |
l.outputFile.WriteString(msg) |
98708cdaf new version, does... |
141 |
} |
df74f4a7e new filename format |
142 |
func (l *Logger) Trace(format string, v ...interface{}) { |
372beaf62 refactored if sta... |
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
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... |
159 |
} |
372beaf62 refactored if sta... |
160 161 162 163 |
if l.shouldSplit(len(s)) { l.split() } l.outputFile.WriteString(s) |
a99c98307 first commit |
164 |
} |
df74f4a7e new filename format |
165 |
func (l *Logger) Close() { |
372beaf62 refactored if sta... |
166 167 168 169 170 171 172 173 |
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 |
174 |
} |
a99c98307 first commit |
175 |
} |
98cf2ceeb new interaface; a... |
176 |
func (l *Logger) split() { |
372beaf62 refactored if sta... |
177 178 179 |
if l.outputFile == nil { return } |
98cf2ceeb new interaface; a... |
180 181 182 183 184 185 186 |
// close old file err := l.outputFile.Close() if err != nil { fmt.Printf("logger: split: couldn't close event file ") return } |
df74f4a7e new filename format |
187 188 |
l.splitCount++ |
98cf2ceeb new interaface; a... |
189 190 |
// open new file var errnew error |
df74f4a7e new filename format |
191 |
path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) |
98cf2ceeb new interaface; a... |
192 |
l.outputFile, errnew = os.Create(path) |
a99c98307 first commit |
193 |
|
98cf2ceeb new interaface; a... |
194 195 196 |
if errnew != nil { fmt.Printf("logger: split: couldn't create event log file ") |
a99c98307 first commit |
197 198 |
} } |
98cf2ceeb new interaface; a... |
199 200 201 |
func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
a99c98307 first commit |
202 |
} |