Skip to content

Commit 8df8a6f

Browse files
committed
Add mock implementations for structured logging methods
1 parent a831eec commit 8df8a6f

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

httpclient/httpclient_mocklogger.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package httpclient
33

44
import (
5+
"time"
6+
57
"github.com/deploymenttheory/go-api-http-client/logger"
68
"github.com/stretchr/testify/mock"
79
"go.uber.org/zap"
@@ -28,6 +30,8 @@ func (m *MockLogger) SetLevel(level logger.LogLevel) {
2830
m.Called(level)
2931
}
3032

33+
// Mock implementations for unstructured logging methods
34+
3135
func (m *MockLogger) Debug(msg string, fields ...zap.Field) {
3236
m.Called(msg, fields)
3337
}
@@ -40,7 +44,6 @@ func (m *MockLogger) Warn(msg string, fields ...zap.Field) {
4044
m.Called(msg, fields)
4145
}
4246

43-
// Error method
4447
func (m *MockLogger) Error(msg string, fields ...zap.Field) error {
4548
args := m.Called(msg, fields)
4649
return args.Error(0)
@@ -63,3 +66,29 @@ func (m *MockLogger) GetLogLevel() logger.LogLevel {
6366
args := m.Called()
6467
return args.Get(0).(logger.LogLevel)
6568
}
69+
70+
// Mock implementations for structured logging methods
71+
72+
func (m *MockLogger) LogRequestStart(requestID string, userID string, method string, url string, headers map[string][]string) {
73+
m.Called(requestID, userID, method, url, headers)
74+
}
75+
76+
func (m *MockLogger) LogRequestEnd(method string, url string, statusCode int, duration time.Duration) {
77+
m.Called(method, url, statusCode, duration)
78+
}
79+
80+
func (m *MockLogger) LogError(method string, url string, statusCode int, err error, stacktrace string) {
81+
m.Called(method, url, statusCode, err, stacktrace)
82+
}
83+
84+
func (m *MockLogger) LogRetryAttempt(method string, url string, attempt int, reason string, waitDuration time.Duration, err error) {
85+
m.Called(method, url, attempt, reason, waitDuration, err)
86+
}
87+
88+
func (m *MockLogger) LogRateLimiting(method string, url string, retryAfter string, waitDuration time.Duration) {
89+
m.Called(method, url, retryAfter, waitDuration)
90+
}
91+
92+
func (m *MockLogger) LogResponse(method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration) {
93+
m.Called(method, url, statusCode, responseBody, responseHeaders, duration)
94+
}

logger/zaplogger_logfields.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// LogRequestStart logs the initiation of an HTTP request if the current log level permits.
1010
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.
11+
if d.logLevel <= LogLevelInfo {
1212
fields := []zap.Field{
1313
zap.String("event", "request_start"),
1414
zap.String("method", method),
@@ -23,7 +23,7 @@ func (d *defaultLogger) LogRequestStart(requestID string, userID string, method
2323

2424
// LogRequestEnd logs the completion of an HTTP request if the current log level permits.
2525
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.
26+
if d.logLevel <= LogLevelInfo {
2727
fields := []zap.Field{
2828
zap.String("event", "request_end"),
2929
zap.String("method", method),
@@ -52,7 +52,7 @@ func (d *defaultLogger) LogError(method string, url string, statusCode int, err
5252

5353
// 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.
5454
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.
55+
if d.logLevel <= LogLevelWarn {
5656
fields := []zap.Field{
5757
zap.String("event", "retry_attempt"),
5858
zap.String("method", method),
@@ -68,7 +68,7 @@ func (d *defaultLogger) LogRetryAttempt(method string, url string, attempt int,
6868

6969
// 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.
7070
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.
71+
if d.logLevel <= LogLevelWarn {
7272
fields := []zap.Field{
7373
zap.String("event", "rate_limited"),
7474
zap.String("method", method),

logger/zaplogger_logger.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func (d *defaultLogger) SetLevel(level LogLevel) {
5151
d.logLevel = level
5252
}
5353

54+
// With adds contextual key-value pairs to the logger, returning a new logger instance with the context.
55+
// This is useful for creating a logger with common fields that should be included in all subsequent log entries.
56+
func (d *defaultLogger) With(fields ...zapcore.Field) Logger {
57+
return &defaultLogger{
58+
logger: d.logger.With(fields...),
59+
logLevel: d.logLevel,
60+
}
61+
}
62+
5463
// Debug logs a message at the Debug level. This level is typically used for detailed troubleshooting
5564
// information that is only relevant during active development or debugging.
5665
func (d *defaultLogger) Debug(msg string, fields ...zapcore.Field) {
@@ -109,14 +118,14 @@ func GetLoggerBasedOnEnv() *zap.Logger {
109118
if os.Getenv("APP_ENV") == "development" {
110119
logger, err := zap.NewDevelopment()
111120
if err != nil {
112-
panic(err) // Handle error according to your application's error policy
121+
panic(err)
113122
}
114123
return logger
115124
}
116125

117126
logger, err := zap.NewProduction()
118127
if err != nil {
119-
panic(err) // Handle error according to your application's error policy
128+
panic(err)
120129
}
121130
return logger
122131
}

0 commit comments

Comments
 (0)