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