|
| 1 | +package httpserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// JSONLogEntry represents a single HTTP request log entry in JSON format |
| 13 | +type JSONLogEntry struct { |
| 14 | + Timestamp string `json:"timestamp"` |
| 15 | + RemoteAddr string `json:"remote_addr"` |
| 16 | + Method string `json:"method"` |
| 17 | + URL string `json:"url"` |
| 18 | + Proto string `json:"proto"` |
| 19 | + StatusCode int `json:"status_code"` |
| 20 | + Size int `json:"size"` |
| 21 | + UserAgent string `json:"user_agent,omitempty"` |
| 22 | + Headers map[string]string `json:"headers,omitempty"` |
| 23 | + RequestBody string `json:"request_body,omitempty"` |
| 24 | + ResponseBody string `json:"response_body,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +// JSONLogger handles writing JSON log entries to a file |
| 28 | +type JSONLogger struct { |
| 29 | + file *os.File |
| 30 | + mutex sync.Mutex |
| 31 | + enable bool |
| 32 | +} |
| 33 | + |
| 34 | +// NewJSONLogger creates a new JSON logger instance |
| 35 | +func NewJSONLogger(filePath string) (*JSONLogger, error) { |
| 36 | + if filePath == "" { |
| 37 | + return &JSONLogger{enable: false}, nil |
| 38 | + } |
| 39 | + |
| 40 | + file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("failed to open JSON log file: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + return &JSONLogger{ |
| 46 | + file: file, |
| 47 | + enable: true, |
| 48 | + }, nil |
| 49 | +} |
| 50 | + |
| 51 | +// LogRequest logs an HTTP request to the JSON file |
| 52 | +func (jl *JSONLogger) LogRequest(r *http.Request, statusCode, size int, userAgent string, headers map[string]string, requestBody, responseBody string) error { |
| 53 | + if !jl.enable { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + entry := JSONLogEntry{ |
| 58 | + Timestamp: time.Now().Format(time.RFC3339), |
| 59 | + RemoteAddr: r.RemoteAddr, |
| 60 | + Method: r.Method, |
| 61 | + URL: r.URL.String(), |
| 62 | + Proto: r.Proto, |
| 63 | + StatusCode: statusCode, |
| 64 | + Size: size, |
| 65 | + UserAgent: userAgent, |
| 66 | + Headers: headers, |
| 67 | + RequestBody: requestBody, |
| 68 | + ResponseBody: responseBody, |
| 69 | + } |
| 70 | + |
| 71 | + jl.mutex.Lock() |
| 72 | + defer jl.mutex.Unlock() |
| 73 | + |
| 74 | + jsonData, err := json.Marshal(entry) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to marshal JSON log entry: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + _, err = jl.file.Write(append(jsonData, '\n')) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to write JSON log entry: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// Close closes the JSON log file |
| 88 | +func (jl *JSONLogger) Close() error { |
| 89 | + if jl.file != nil { |
| 90 | + return jl.file.Close() |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
0 commit comments