Commit 31ebd1bdef059442b548ee6a5353887193659559

Authored by Marko Tikvić
1 parent 901a92955a
Exists in master

replaces log separator '=' with '#'

Showing 1 changed file with 1 additions and 1 deletions   Show diff stats
1 package gologger 1 package gologger
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "fmt" 5 "fmt"
6 "net/http" 6 "net/http"
7 "net/http/httputil" 7 "net/http/httputil"
8 "os" 8 "os"
9 "strings" 9 "strings"
10 "time" 10 "time"
11 ) 11 )
12 12
13 // LogHTTPRequest ... 13 // LogHTTPRequest ...
14 func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { 14 func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string {
15 if userID == "" { 15 if userID == "" {
16 userID = "-" 16 userID = "-"
17 } 17 }
18 18
19 var b strings.Builder 19 var b strings.Builder
20 20
21 b.WriteString("Request:\n") 21 b.WriteString("Request:\n")
22 // CLF-like header 22 // CLF-like header
23 fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(dateTimeFormat)) 23 fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(dateTimeFormat))
24 24
25 body, err := httputil.DumpRequest(req, true) 25 body, err := httputil.DumpRequest(req, true)
26 if err != nil { 26 if err != nil {
27 fmt.Fprintf(os.Stderr, "%v\n", err) 27 fmt.Fprintf(os.Stderr, "%v\n", err)
28 } 28 }
29 29
30 const sepStr = "\r\n\r\n" 30 const sepStr = "\r\n\r\n"
31 sepIndex := bytes.Index(body, []byte(sepStr)) 31 sepIndex := bytes.Index(body, []byte(sepStr))
32 if sepIndex == -1 { 32 if sepIndex == -1 {
33 b.WriteString(string(body) + "\n\n") 33 b.WriteString(string(body) + "\n\n")
34 } else { 34 } else {
35 sepIndex += len(sepStr) 35 sepIndex += len(sepStr)
36 payload, _ := printJSON(body[sepIndex:]) 36 payload, _ := printJSON(body[sepIndex:])
37 b.WriteString(string(body[:sepIndex]) + string(payload) + "\n\n") 37 b.WriteString(string(body[:sepIndex]) + string(payload) + "\n\n")
38 } 38 }
39 39
40 return b.String() 40 return b.String()
41 } 41 }
42 42
43 const splitLine = "==============================================================" 43 const splitLine = "##############################################################"
44 44
45 // LogHTTPResponse ... 45 // LogHTTPResponse ...
46 func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { 46 func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string {
47 return fmt.Sprintf("Response:\n%d %v %dB\n%s\n", status, duration, size, splitLine) 47 return fmt.Sprintf("Response:\n%d %v %dB\n%s\n", status, duration, size, splitLine)
48 } 48 }
49 49
50 // CombineHTTPLogs ... 50 // CombineHTTPLogs ...
51 func (l *Logger) CombineHTTPLogs(in string, out string) { 51 func (l *Logger) CombineHTTPLogs(in string, out string) {
52 if l.outputFile == nil { 52 if l.outputFile == nil {
53 return 53 return
54 } 54 }
55 55
56 l.mu.Lock() 56 l.mu.Lock()
57 defer l.mu.Unlock() 57 defer l.mu.Unlock()
58 58
59 msg := in + out 59 msg := in + out
60 if l.shouldSplit(len(msg)) { 60 if l.shouldSplit(len(msg)) {
61 l.split() 61 l.split()
62 } 62 }
63 l.outputFile.WriteString(msg) 63 l.outputFile.WriteString(msg)
64 } 64 }
65 65