Blame view
main.go
3.86 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 |
} |
093191eb2 much cleaner, muc... |
81 |
func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { |
1f7fbc601 logging user ID |
82 83 |
if userID == "" { userID = "-" |
372beaf62 refactored if sta... |
84 |
} |
df74f4a7e new filename format |
85 |
|
372beaf62 refactored if sta... |
86 |
var b strings.Builder |
093191eb2 much cleaner, muc... |
87 |
|
372beaf62 refactored if sta... |
88 89 90 |
b.WriteString("Request: ") // CLF-like header |
093191eb2 much cleaner, muc... |
91 92 |
fmt.Fprintf(&b, "%s %s %s ", req.RemoteAddr, userID, time.Now().Format(time.RFC3339)) |
cccb4b47c log http response |
93 |
|
093191eb2 much cleaner, muc... |
94 95 96 97 |
body, err := httputil.DumpRequest(req, true) if err != nil { fmt.Fprintf(os.Stderr, "%v ", err) |
cccb4b47c log http response |
98 |
} |
093191eb2 much cleaner, muc... |
99 100 101 |
b.WriteString(string(body) + " ") |
372beaf62 refactored if sta... |
102 |
|
093191eb2 much cleaner, muc... |
103 104 |
return b.String() } |
372beaf62 refactored if sta... |
105 |
|
093191eb2 much cleaner, muc... |
106 107 108 109 110 111 |
func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { splitLine := "============================================================== " return fmt.Sprintf("Response: %d %v %dB %s", status, duration, size, splitLine) |
cccb4b47c log http response |
112 |
} |
093191eb2 much cleaner, muc... |
113 |
func (l *Logger) CombineHTTPLogs(in string, out string) { |
372beaf62 refactored if sta... |
114 115 116 |
if l.outputFile == nil { return } |
df74f4a7e new filename format |
117 |
|
372beaf62 refactored if sta... |
118 119 |
l.mu.Lock() defer l.mu.Unlock() |
093191eb2 much cleaner, muc... |
120 |
msg := in + out |
372beaf62 refactored if sta... |
121 122 |
if l.shouldSplit(len(msg)) { l.split() |
98708cdaf new version, does... |
123 |
} |
372beaf62 refactored if sta... |
124 |
l.outputFile.WriteString(msg) |
98708cdaf new version, does... |
125 |
} |
df74f4a7e new filename format |
126 |
func (l *Logger) Trace(format string, v ...interface{}) { |
372beaf62 refactored if sta... |
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
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... |
143 |
} |
372beaf62 refactored if sta... |
144 145 146 147 |
if l.shouldSplit(len(s)) { l.split() } l.outputFile.WriteString(s) |
a99c98307 first commit |
148 |
} |
df74f4a7e new filename format |
149 |
func (l *Logger) Close() { |
372beaf62 refactored if sta... |
150 151 152 153 154 155 156 157 |
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 |
158 |
} |
a99c98307 first commit |
159 |
} |
98cf2ceeb new interaface; a... |
160 |
func (l *Logger) split() { |
372beaf62 refactored if sta... |
161 162 163 |
if l.outputFile == nil { return } |
98cf2ceeb new interaface; a... |
164 165 166 167 168 169 170 |
// close old file err := l.outputFile.Close() if err != nil { fmt.Printf("logger: split: couldn't close event file ") return } |
df74f4a7e new filename format |
171 172 |
l.splitCount++ |
98cf2ceeb new interaface; a... |
173 174 |
// open new file var errnew error |
3e936a3c2 output file name ... |
175 |
path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") |
98cf2ceeb new interaface; a... |
176 |
l.outputFile, errnew = os.Create(path) |
a99c98307 first commit |
177 |
|
98cf2ceeb new interaface; a... |
178 179 180 |
if errnew != nil { fmt.Printf("logger: split: couldn't create event log file ") |
a99c98307 first commit |
181 182 |
} } |
98cf2ceeb new interaface; a... |
183 184 185 |
func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
a99c98307 first commit |
186 |
} |