|
| 1 | +// zaplogger_logfields.go |
| 2 | +package logger |
| 3 | + |
| 4 | +import ( |
| 5 | + "time" |
| 6 | + |
| 7 | + "go.uber.org/zap" |
| 8 | +) |
| 9 | + |
| 10 | +// LogRequestStart logs the initiation of an HTTP request, including the HTTP method, URL, and headers. |
| 11 | +// This function is intended to be called at the beginning of an HTTP request lifecycle. |
| 12 | +func LogRequestStart(logger *zap.Logger, requestID string, userID string, method string, url string, headers map[string][]string) { |
| 13 | + fields := []zap.Field{ |
| 14 | + zap.String("event", "request_start"), |
| 15 | + zap.String("method", method), |
| 16 | + zap.String("url", url), |
| 17 | + zap.Reflect("headers", headers), // Consider sanitizing or selectively logging headers |
| 18 | + } |
| 19 | + logger.Info("HTTP request started", fields...) |
| 20 | +} |
| 21 | + |
| 22 | +// LogRequestEnd logs the completion of an HTTP request, including the HTTP method, URL, status code, and duration. |
| 23 | +// This function is intended to be called at the end of an HTTP request lifecycle. |
| 24 | +func LogRequestEnd(logger *zap.Logger, method string, url string, statusCode int, duration time.Duration) { |
| 25 | + fields := []zap.Field{ |
| 26 | + zap.String("event", "request_end"), |
| 27 | + zap.String("method", method), |
| 28 | + zap.String("url", url), |
| 29 | + zap.Int("status_code", statusCode), |
| 30 | + zap.Duration("duration", duration), |
| 31 | + } |
| 32 | + logger.Info("HTTP request completed", fields...) |
| 33 | +} |
| 34 | + |
| 35 | +// LogError logs an error that occurs during the processing of an HTTP request, including the HTTP method, URL, status code, error message, and stack trace. |
| 36 | +// This function is intended to be called when an error is encountered during an HTTP request lifecycle. |
| 37 | +func LogError(logger *zap.Logger, method string, url string, statusCode int, err error, stacktrace string) { |
| 38 | + fields := []zap.Field{ |
| 39 | + zap.String("event", "request_error"), |
| 40 | + zap.String("method", method), |
| 41 | + zap.String("url", url), |
| 42 | + zap.Int("status_code", statusCode), |
| 43 | + zap.String("error_message", err.Error()), |
| 44 | + zap.String("stacktrace", stacktrace), |
| 45 | + } |
| 46 | + logger.Error("Error during HTTP request", fields...) |
| 47 | +} |
| 48 | + |
| 49 | +// LogRetryAttempt logs a retry attempt for an HTTP request, including the HTTP method, URL, attempt number, and reason for the retry. |
| 50 | +// This function is intended to be called when an HTTP request is retried. |
| 51 | +func LogRetryAttempt(logger *zap.Logger, method string, url string, attempt int, reason string) { |
| 52 | + fields := []zap.Field{ |
| 53 | + zap.String("event", "retry_attempt"), |
| 54 | + zap.String("method", method), |
| 55 | + zap.String("url", url), |
| 56 | + zap.Int("attempt", attempt), |
| 57 | + zap.String("reason", reason), |
| 58 | + } |
| 59 | + logger.Warn("HTTP request retry", fields...) |
| 60 | +} |
| 61 | + |
| 62 | +// LogRateLimiting logs when an HTTP request is rate-limited, including the HTTP method, URL, and the value of the 'Retry-After' header. |
| 63 | +// This function is intended to be called when an HTTP request encounters rate limiting. |
| 64 | +func LogRateLimiting(logger *zap.Logger, method string, url string, retryAfter string) { |
| 65 | + fields := []zap.Field{ |
| 66 | + zap.String("event", "rate_limited"), |
| 67 | + zap.String("method", method), |
| 68 | + zap.String("url", url), |
| 69 | + zap.String("retry_after", retryAfter), |
| 70 | + } |
| 71 | + logger.Warn("HTTP request rate-limited", fields...) |
| 72 | +} |
0 commit comments