Blame view
main.go
3.9 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 64 65 66 |
func (l *Logger) Log(format string, v ...interface{}) { if l.outputFile != nil { l.mu.Lock() defer l.mu.Unlock() |
98708cdaf new version, does... |
67 |
|
98cf2ceeb new interaface; a... |
68 69 70 |
msg := fmt.Sprintf(format, v...) s := time.Now().Format(time.RFC3339) + ": " + msg + " " |
df74f4a7e new filename format |
71 72 |
if l.shouldSplit(len(s)) { l.split() |
98cf2ceeb new interaface; a... |
73 |
} |
df74f4a7e new filename format |
74 |
l.outputFile.WriteString(s) |
23ce66f0f optional init par... |
75 |
} |
98708cdaf new version, does... |
76 |
} |
df74f4a7e new filename format |
77 78 79 80 81 |
func (l *Logger) LogRequest(req *http.Request, userid string) { if l.outputFile != nil { if userid == "" { userid = "-" } |
98cf2ceeb new interaface; a... |
82 |
|
df74f4a7e new filename format |
83 |
var b strings.Builder |
cccb4b47c log http response |
84 85 |
b.WriteString("Request: ") |
df74f4a7e new filename format |
86 87 |
// CLF-like header ts := time.Now().Format(time.RFC3339) |
cccb4b47c log http response |
88 89 |
fmt.Fprintf(&b, "%s %s ", req.RemoteAddr, ts) |
df74f4a7e new filename format |
90 |
|
cccb4b47c log http response |
91 92 |
body, err := httputil.DumpRequest(req, true) if err != nil { |
df74f4a7e new filename format |
93 94 |
fmt.Fprintf(os.Stderr, "%v ", err) |
cccb4b47c log http response |
95 96 |
} else { b.WriteString(string(body)) |
df74f4a7e new filename format |
97 98 99 100 101 102 |
} b.WriteString(" ") msg := b.String() |
cccb4b47c log http response |
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
l.mu.Lock() defer l.mu.Unlock() if l.shouldSplit(len(msg)) { l.split() } l.outputFile.WriteString(msg) } } func (l *Logger) LogResponse(w http.ResponseWriter, duration time.Duration) { if l.outputFile != nil { var b strings.Builder b.WriteString("Response: ") for k, v := range w.Header() { b.WriteString(fmt.Sprintf("%s: %s ", k, v)) } b.WriteString(fmt.Sprintf(" Completed in: %v ", duration)) msg := b.String() |
df74f4a7e new filename format |
127 128 129 130 131 132 |
l.mu.Lock() defer l.mu.Unlock() if l.shouldSplit(len(msg)) { l.split() |
23ce66f0f optional init par... |
133 |
} |
df74f4a7e new filename format |
134 |
l.outputFile.WriteString(msg) |
98708cdaf new version, does... |
135 |
} |
98708cdaf new version, does... |
136 |
} |
df74f4a7e new filename format |
137 138 139 140 |
func (l *Logger) Trace(format string, v ...interface{}) { if l.outputFile != nil { l.mu.Lock() defer l.mu.Unlock() |
23ce66f0f optional init par... |
141 |
_, file, line, ok := runtime.Caller(1) |
98cf2ceeb new interaface; a... |
142 143 144 |
s := "" msg := fmt.Sprintf(format, v...) |
23ce66f0f optional init par... |
145 |
if ok { |
98cf2ceeb new interaface; a... |
146 147 |
s = fmt.Sprintf("%s: %s %d: %s ", time.Now().Format(time.RFC3339), file, line, msg) |
23ce66f0f optional init par... |
148 |
} else { |
98cf2ceeb new interaface; a... |
149 150 151 |
s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + " ") } |
df74f4a7e new filename format |
152 153 |
if l.shouldSplit(len(s)) { l.split() |
23ce66f0f optional init par... |
154 |
} |
df74f4a7e new filename format |
155 |
l.outputFile.WriteString(s) |
98708cdaf new version, does... |
156 |
} |
a99c98307 first commit |
157 |
} |
df74f4a7e new filename format |
158 159 160 |
func (l *Logger) Close() { if l.outputFile != nil { err := l.outputFile.Close() |
23ce66f0f optional init par... |
161 162 163 164 |
if err != nil { fmt.Printf("logger: on exit: couldn't close event log file ") } |
a99c98307 first commit |
165 |
} |
a99c98307 first commit |
166 |
} |
98cf2ceeb new interaface; a... |
167 |
func (l *Logger) split() { |
98cf2ceeb new interaface; a... |
168 169 170 171 172 173 174 |
// close old file err := l.outputFile.Close() if err != nil { fmt.Printf("logger: split: couldn't close event file ") return } |
df74f4a7e new filename format |
175 176 |
l.splitCount++ |
98cf2ceeb new interaface; a... |
177 178 |
// open new file var errnew error |
df74f4a7e new filename format |
179 |
path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) |
98cf2ceeb new interaface; a... |
180 |
l.outputFile, errnew = os.Create(path) |
a99c98307 first commit |
181 |
|
98cf2ceeb new interaface; a... |
182 183 184 |
if errnew != nil { fmt.Printf("logger: split: couldn't create event log file ") |
a99c98307 first commit |
185 186 |
} } |
98cf2ceeb new interaface; a... |
187 188 189 |
func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) |
a99c98307 first commit |
190 |
} |