1- // zaplogger_logfields.go
21package logger
32
43import (
@@ -7,66 +6,92 @@ import (
76 "go.uber.org/zap"
87)
98
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
9+ // LogRequestStart logs the initiation of an HTTP request if the current log level permits.
10+ func (d * defaultLogger ) LogRequestStart (requestID string , userID string , method string , url string , headers map [string ][]string ) {
11+ if d .logLevel <= LogLevelInfo { // Assuming LogLevelInfo is appropriate for request start messages.
12+ fields := []zap.Field {
13+ zap .String ("event" , "request_start" ),
14+ zap .String ("method" , method ),
15+ zap .String ("url" , url ),
16+ zap .String ("request_id" , requestID ),
17+ zap .String ("user_id" , userID ),
18+ zap .Reflect ("headers" , headers ),
19+ }
20+ d .logger .Info ("HTTP request started" , fields ... )
1821 }
19- logger .Info ("HTTP request started" , fields ... )
2022}
2123
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 ),
24+ // LogRequestEnd logs the completion of an HTTP request if the current log level permits.
25+ func (d * defaultLogger ) LogRequestEnd (method string , url string , statusCode int , duration time.Duration ) {
26+ if d .logLevel <= LogLevelInfo { // Assuming LogLevelInfo is appropriate for request end messages.
27+ fields := []zap.Field {
28+ zap .String ("event" , "request_end" ),
29+ zap .String ("method" , method ),
30+ zap .String ("url" , url ),
31+ zap .Int ("status_code" , statusCode ),
32+ zap .Duration ("duration" , duration ),
33+ }
34+ d .logger .Info ("HTTP request completed" , fields ... )
3135 }
32- logger .Info ("HTTP request completed" , fields ... )
3336}
3437
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 ),
38+ // LogError logs an error that occurs during the processing of an HTTP request if the current log level permits.
39+ func (d * defaultLogger ) LogError (method string , url string , statusCode int , err error , stacktrace string ) {
40+ if d .logLevel <= LogLevelError {
41+ fields := []zap.Field {
42+ zap .String ("event" , "request_error" ),
43+ zap .String ("method" , method ),
44+ zap .String ("url" , url ),
45+ zap .Int ("status_code" , statusCode ),
46+ zap .String ("error_message" , err .Error ()),
47+ zap .String ("stacktrace" , stacktrace ),
48+ }
49+ d .logger .Error ("Error during HTTP request" , fields ... )
4550 }
46- logger .Error ("Error during HTTP request" , fields ... )
4751}
4852
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 ),
53+ // LogRetryAttempt logs a retry attempt for an HTTP request if the current log level permits, including wait duration and the error that triggered the retry.
54+ func (d * defaultLogger ) LogRetryAttempt (method string , url string , attempt int , reason string , waitDuration time.Duration , err error ) {
55+ if d .logLevel <= LogLevelWarn { // Assuming LogLevelWarn is appropriate for retry attempt messages.
56+ fields := []zap.Field {
57+ zap .String ("event" , "retry_attempt" ),
58+ zap .String ("method" , method ),
59+ zap .String ("url" , url ),
60+ zap .Int ("attempt" , attempt ),
61+ zap .String ("reason" , reason ),
62+ zap .Duration ("waitDuration" , waitDuration ),
63+ zap .String ("error_message" , err .Error ()),
64+ }
65+ d .logger .Warn ("HTTP request retry" , fields ... )
5866 }
59- logger .Warn ("HTTP request retry" , fields ... )
6067}
6168
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 ),
69+ // LogRateLimiting logs when an HTTP request is rate-limited, including the HTTP method, URL, the value of the 'Retry-After' header, and the actual wait duration.
70+ func (d * defaultLogger ) LogRateLimiting (method string , url string , retryAfter string , waitDuration time.Duration ) {
71+ if d .logLevel <= LogLevelWarn { // Check if the log level permits warning messages.
72+ fields := []zap.Field {
73+ zap .String ("event" , "rate_limited" ),
74+ zap .String ("method" , method ),
75+ zap .String ("url" , url ),
76+ zap .String ("retry_after" , retryAfter ),
77+ zap .Duration ("wait_duration" , waitDuration ),
78+ }
79+ d .logger .Warn ("Rate limit encountered, waiting before retrying" , fields ... )
80+ }
81+ }
82+
83+ // LogResponse logs details about an HTTP response if the current log level permits.
84+ func (d * defaultLogger ) LogResponse (method string , url string , statusCode int , responseBody string , responseHeaders map [string ][]string , duration time.Duration ) {
85+ if d .logLevel <= LogLevelInfo {
86+ fields := []zap.Field {
87+ zap .String ("event" , "response_received" ),
88+ zap .String ("method" , method ),
89+ zap .String ("url" , url ),
90+ zap .Int ("status_code" , statusCode ),
91+ zap .String ("response_body" , responseBody ),
92+ zap .Reflect ("response_headers" , responseHeaders ),
93+ zap .Duration ("duration" , duration ),
94+ }
95+ d .logger .Info ("HTTP response details" , fields ... )
7096 }
71- logger .Warn ("HTTP request rate-limited" , fields ... )
7297}
0 commit comments