Skip to content

Commit 74e1bcf

Browse files
committed
Delete logger package files for structured messaging
1 parent 34e5573 commit 74e1bcf

File tree

5 files changed

+76
-58
lines changed

5 files changed

+76
-58
lines changed
File renamed without changes.

logger/zaplogger_logfields.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

logger/zaplogger_logfields_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// zaplogger_structured_messaging_test.go
2+
package logger

errors/http_error_handling.go renamed to status/status.go

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,12 @@
1-
// http_error_handling.go
1+
// status.go
22
// This package provides utility functions and structures for handling and categorizing HTTP error responses.
3-
package errors
3+
package status
44

55
import (
6-
"encoding/json"
76
"fmt"
87
"net/http"
9-
10-
"github.com/deploymenttheory/go-api-http-client/logger"
11-
"go.uber.org/zap"
128
)
139

14-
// APIError represents a structured API error response.
15-
type APIError struct {
16-
StatusCode int // HTTP status code
17-
Type string // A brief identifier for the type of error (e.g., "RateLimit", "BadRequest", etc.)
18-
Message string // Human-readable message
19-
}
20-
21-
// StructuredError represents a structured error response from the API.
22-
type StructuredError struct {
23-
Error struct {
24-
Code string `json:"code"`
25-
Message string `json:"message"`
26-
} `json:"error"`
27-
}
28-
29-
// Error returns a string representation of the APIError.
30-
func (e *APIError) Error() string {
31-
return fmt.Sprintf("API Error (Type: %s, Code: %d): %s", e.Type, e.StatusCode, e.Message)
32-
}
33-
34-
// HandleAPIError handles error responses from the API, converting them into a structured error if possible.
35-
func HandleAPIError(resp *http.Response, log logger.Logger) *APIError {
36-
var structuredErr StructuredError
37-
err := json.NewDecoder(resp.Body).Decode(&structuredErr)
38-
if err == nil && structuredErr.Error.Message != "" {
39-
// Log the structured error details
40-
log.Warn("API returned structured error",
41-
zap.String("status", resp.Status),
42-
zap.String("error_code", structuredErr.Error.Code),
43-
zap.String("error_message", structuredErr.Error.Message),
44-
)
45-
return &APIError{
46-
StatusCode: resp.StatusCode,
47-
Type: structuredErr.Error.Code,
48-
Message: structuredErr.Error.Message,
49-
}
50-
}
51-
52-
// Default error message for non-structured responses or decode failures
53-
errMsg := fmt.Sprintf("Unexpected error with status code: %d", resp.StatusCode)
54-
log.Error("Failed to decode API error message, using default error message",
55-
zap.String("status", resp.Status),
56-
zap.String("error_message", errMsg),
57-
)
58-
59-
return &APIError{
60-
StatusCode: resp.StatusCode,
61-
Type: "UnexpectedError",
62-
Message: errMsg,
63-
}
64-
}
65-
6610
// TranslateStatusCode provides a human-readable message for HTTP status codes.
6711
func TranslateStatusCode(resp *http.Response) string {
6812

0 commit comments

Comments
 (0)