Commit cccb4b47c00c510cb8071d3619b343b8fdeb9527
1 parent
df74f4a7ee
Exists in
master
log http response
Showing
1 changed file
with
27 additions
and
13 deletions
Show diff stats
main.go
1 | package gologger | 1 | package gologger |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "io/ioutil" | ||
6 | "net/http" | 5 | "net/http" |
6 | "net/http/httputil" | ||
7 | "os" | 7 | "os" |
8 | "path/filepath" | 8 | "path/filepath" |
9 | "runtime" | 9 | "runtime" |
10 | "strings" | 10 | "strings" |
11 | "sync" | 11 | "sync" |
12 | "time" | 12 | "time" |
13 | ) | 13 | ) |
14 | 14 | ||
15 | const ( | 15 | const ( |
16 | MaxLogSize5MB int64 = 5 * 1024 * 1024 | 16 | MaxLogSize5MB int64 = 5 * 1024 * 1024 |
17 | MaxLogSize1MB int64 = 1 * 1024 * 1024 | 17 | MaxLogSize1MB int64 = 1 * 1024 * 1024 |
18 | MaxLogSize500KB int64 = 500 * 1024 | 18 | MaxLogSize500KB int64 = 500 * 1024 |
19 | MaxLogSize100KB int64 = 100 * 1024 | 19 | MaxLogSize100KB int64 = 100 * 1024 |
20 | MaxLogSize512B int64 = 512 | 20 | MaxLogSize512B int64 = 512 |
21 | logDirName = "log" | 21 | logDirName = "log" |
22 | ) | 22 | ) |
23 | 23 | ||
24 | type Logger struct { | 24 | type Logger struct { |
25 | mu *sync.Mutex | 25 | mu *sync.Mutex |
26 | outputFile *os.File | 26 | outputFile *os.File |
27 | 27 | ||
28 | outputFileName string | 28 | outputFileName string |
29 | maxFileSize int64 | 29 | maxFileSize int64 |
30 | 30 | ||
31 | splitCount int | 31 | splitCount int |
32 | } | 32 | } |
33 | 33 | ||
34 | func New(name string, maxFileSize int64) (logger *Logger, err error) { | 34 | func New(name string, maxFileSize int64) (logger *Logger, err error) { |
35 | logger = &Logger{} | 35 | logger = &Logger{} |
36 | 36 | ||
37 | logger.outputFileName = name + "-log" | 37 | logger.outputFileName = name + "-log" |
38 | logger.mu = &sync.Mutex{} | 38 | logger.mu = &sync.Mutex{} |
39 | logger.maxFileSize = maxFileSize | 39 | logger.maxFileSize = maxFileSize |
40 | 40 | ||
41 | err = os.Mkdir(logDirName, os.ModePerm) | 41 | err = os.Mkdir(logDirName, os.ModePerm) |
42 | if err != nil { | 42 | if err != nil { |
43 | if !os.IsExist(err) { | 43 | if !os.IsExist(err) { |
44 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") | 44 | fmt.Printf("logger: mkdir: couldn't create event log directory\n") |
45 | return nil, err | 45 | return nil, err |
46 | } | 46 | } |
47 | } | 47 | } |
48 | 48 | ||
49 | date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) | 49 | date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) |
50 | logger.outputFileName += "_" + date + ".txt" | 50 | logger.outputFileName += "_" + date + ".txt" |
51 | path := filepath.Join(logDirName, logger.outputFileName) | 51 | path := filepath.Join(logDirName, logger.outputFileName) |
52 | logger.outputFile, err = os.Create(path) | 52 | logger.outputFile, err = os.Create(path) |
53 | if err != nil { | 53 | if err != nil { |
54 | fmt.Printf("logger: new: couldn't create event log file\n") | 54 | fmt.Printf("logger: new: couldn't create event log file\n") |
55 | return nil, err | 55 | return nil, err |
56 | } | 56 | } |
57 | 57 | ||
58 | return logger, nil | 58 | return logger, nil |
59 | } | 59 | } |
60 | 60 | ||
61 | func (l *Logger) Print(format string, v ...interface{}) { | 61 | func (l *Logger) Print(format string, v ...interface{}) { |
62 | msg := fmt.Sprintf(format, v...) | 62 | msg := fmt.Sprintf(format, v...) |
63 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") | 63 | fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n") |
64 | } | 64 | } |
65 | 65 | ||
66 | func (l *Logger) Log(format string, v ...interface{}) { | 66 | func (l *Logger) Log(format string, v ...interface{}) { |
67 | if l.outputFile != nil { | 67 | if l.outputFile != nil { |
68 | l.mu.Lock() | 68 | l.mu.Lock() |
69 | defer l.mu.Unlock() | 69 | defer l.mu.Unlock() |
70 | 70 | ||
71 | msg := fmt.Sprintf(format, v...) | 71 | msg := fmt.Sprintf(format, v...) |
72 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" | 72 | s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" |
73 | if l.shouldSplit(len(s)) { | 73 | if l.shouldSplit(len(s)) { |
74 | l.split() | 74 | l.split() |
75 | } | 75 | } |
76 | l.outputFile.WriteString(s) | 76 | l.outputFile.WriteString(s) |
77 | } | 77 | } |
78 | } | 78 | } |
79 | 79 | ||
80 | func (l *Logger) LogRequest(req *http.Request, userid string) { | 80 | func (l *Logger) LogRequest(req *http.Request, userid string) { |
81 | if l.outputFile != nil { | 81 | if l.outputFile != nil { |
82 | if userid == "" { | 82 | if userid == "" { |
83 | userid = "-" | 83 | userid = "-" |
84 | } | 84 | } |
85 | 85 | ||
86 | var b strings.Builder | 86 | var b strings.Builder |
87 | b.WriteString("Request:\n") | ||
87 | // CLF-like header | 88 | // CLF-like header |
88 | ts := time.Now().Format(time.RFC3339) | 89 | ts := time.Now().Format(time.RFC3339) |
89 | fmt.Fprintf(&b, "%s - %s [%s] \"%s %s\" %s\n", req.RemoteAddr, userid, ts, req.Method, req.URL.Path, req.Proto) | 90 | fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts) |
90 | 91 | ||
91 | b.WriteString("\nURL encoded:\n") | 92 | body, err := httputil.DumpRequest(req, true) |
92 | for k, v := range req.Form { | 93 | if err != nil { |
93 | fmt.Fprintf(&b, "%s = %s\n", k, v) | ||
94 | } | ||
95 | |||
96 | b.WriteString("\nBody:\n") | ||
97 | buf, err := ioutil.ReadAll(req.Body) | ||
98 | if err == nil { | ||
99 | b.WriteString(string(buf)) | ||
100 | } else { | ||
101 | fmt.Fprintf(os.Stderr, "%v\n", err) | 94 | fmt.Fprintf(os.Stderr, "%v\n", err) |
95 | } else { | ||
96 | b.WriteString(string(body)) | ||
102 | } | 97 | } |
103 | b.WriteString("\n\n") | 98 | b.WriteString("\n\n") |
104 | 99 | ||
105 | msg := b.String() | 100 | msg := b.String() |
106 | //fmt.Println(msg) | 101 | |
102 | l.mu.Lock() | ||
103 | defer l.mu.Unlock() | ||
104 | |||
105 | if l.shouldSplit(len(msg)) { | ||
106 | l.split() | ||
107 | } | ||
108 | l.outputFile.WriteString(msg) | ||
109 | } | ||
110 | } | ||
111 | |||
112 | func (l *Logger) LogResponse(w http.ResponseWriter, duration time.Duration) { | ||
113 | if l.outputFile != nil { | ||
114 | var b strings.Builder | ||
115 | b.WriteString("Response:\n") | ||
116 | for k, v := range w.Header() { | ||
117 | b.WriteString(fmt.Sprintf("%s: %s\n", k, v)) | ||
118 | } | ||
119 | b.WriteString(fmt.Sprintf("\nCompleted in: %v\n", duration)) | ||
120 | msg := b.String() | ||
107 | 121 | ||
108 | l.mu.Lock() | 122 | l.mu.Lock() |
109 | defer l.mu.Unlock() | 123 | defer l.mu.Unlock() |
110 | 124 | ||
111 | if l.shouldSplit(len(msg)) { | 125 | if l.shouldSplit(len(msg)) { |
112 | l.split() | 126 | l.split() |
113 | } | 127 | } |
114 | l.outputFile.WriteString(msg) | 128 | l.outputFile.WriteString(msg) |
115 | } | 129 | } |
116 | } | 130 | } |
117 | 131 | ||
118 | func (l *Logger) Trace(format string, v ...interface{}) { | 132 | func (l *Logger) Trace(format string, v ...interface{}) { |
119 | if l.outputFile != nil { | 133 | if l.outputFile != nil { |
120 | l.mu.Lock() | 134 | l.mu.Lock() |
121 | defer l.mu.Unlock() | 135 | defer l.mu.Unlock() |
122 | _, file, line, ok := runtime.Caller(1) | 136 | _, file, line, ok := runtime.Caller(1) |
123 | 137 | ||
124 | s := "" | 138 | s := "" |
125 | msg := fmt.Sprintf(format, v...) | 139 | msg := fmt.Sprintf(format, v...) |
126 | if ok { | 140 | if ok { |
127 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) | 141 | s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) |
128 | } else { | 142 | } else { |
129 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") | 143 | s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n") |
130 | } | 144 | } |
131 | if l.shouldSplit(len(s)) { | 145 | if l.shouldSplit(len(s)) { |
132 | l.split() | 146 | l.split() |
133 | } | 147 | } |
134 | l.outputFile.WriteString(s) | 148 | l.outputFile.WriteString(s) |
135 | } | 149 | } |
136 | } | 150 | } |
137 | 151 | ||
138 | func (l *Logger) Close() { | 152 | func (l *Logger) Close() { |
139 | if l.outputFile != nil { | 153 | if l.outputFile != nil { |
140 | err := l.outputFile.Close() | 154 | err := l.outputFile.Close() |
141 | if err != nil { | 155 | if err != nil { |
142 | fmt.Printf("logger: on exit: couldn't close event log file\n") | 156 | fmt.Printf("logger: on exit: couldn't close event log file\n") |
143 | } | 157 | } |
144 | } | 158 | } |
145 | } | 159 | } |
146 | 160 | ||
147 | func (l *Logger) split() { | 161 | func (l *Logger) split() { |
148 | // close old file | 162 | // close old file |
149 | err := l.outputFile.Close() | 163 | err := l.outputFile.Close() |
150 | if err != nil { | 164 | if err != nil { |
151 | fmt.Printf("logger: split: couldn't close event file\n") | 165 | fmt.Printf("logger: split: couldn't close event file\n") |
152 | return | 166 | return |
153 | } | 167 | } |
154 | 168 | ||
155 | l.splitCount++ | 169 | l.splitCount++ |
156 | // open new file | 170 | // open new file |
157 | var errnew error | 171 | var errnew error |
158 | path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) | 172 | path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)) |
159 | l.outputFile, errnew = os.Create(path) | 173 | l.outputFile, errnew = os.Create(path) |
160 | 174 | ||
161 | if errnew != nil { | 175 | if errnew != nil { |