Commit 3e936a3c2f80ff83b96672d6474b2358499ade44

Authored by Marko Tikvić
1 parent ed6549a336
Exists in master

output file name sequence fix

Showing 1 changed file with 22 additions and 7 deletions   Show diff stats
... ... @@ -26,6 +26,7 @@ type Logger struct {
26 26 outputFile *os.File
27 27  
28 28 outputFileName string
  29 + fullName string
29 30 maxFileSize int64
30 31  
31 32 splitCount int
... ... @@ -47,8 +48,9 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) {
47 48 }
48 49  
49 50 date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1)
50   - logger.outputFileName += "_" + date + ".txt"
51   - path := filepath.Join(logDirName, logger.outputFileName)
  51 + logger.outputFileName += "_" + date
  52 + logger.fullName = logger.outputFileName + ".txt"
  53 + path := filepath.Join(logDirName, logger.fullName)
52 54 logger.outputFile, err = os.Create(path)
53 55 if err != nil {
54 56 fmt.Printf("logger: new: couldn't create event log file\n")
... ... @@ -94,11 +96,24 @@ func (l *Logger) RequestLog(req *http.Request, userid string) string {
94 96 ts := time.Now().Format(time.RFC3339)
95 97 fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts)
96 98  
97   - body, err := httputil.DumpRequest(req, true)
98   - if err != nil {
99   - fmt.Fprintf(os.Stderr, "%v\n", err)
  99 + headers := req.Header
  100 + content := headers.Get("Content-Type")
  101 + if content != "application/json" {
  102 + b.WriteString(fmt.Sprintf("%s %s\n", req.Method, req.URL))
  103 + for k, h := range headers {
  104 + b.WriteString(k + ": ")
  105 + for _, h2 := range h {
  106 + b.WriteString(h2 + " ")
  107 + }
  108 + b.WriteString("\n")
  109 + }
100 110 } else {
101   - b.WriteString(string(body))
  111 + body, err := httputil.DumpRequest(req, true)
  112 + if err != nil {
  113 + fmt.Fprintf(os.Stderr, "%v\n", err)
  114 + } else {
  115 + b.WriteString(string(body))
  116 + }
102 117 }
103 118 b.WriteString("\n\n")
104 119  
... ... @@ -184,7 +199,7 @@ func (l *Logger) split() {
184 199 l.splitCount++
185 200 // open new file
186 201 var errnew error
187   - path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount))
  202 + path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt")
188 203 l.outputFile, errnew = os.Create(path)
189 204  
190 205 if errnew != nil {
... ...