Skip to content

Commit e6ccc16

Browse files
committed
Refactor MockLogger implementation
1 parent 960a8fd commit e6ccc16

File tree

1 file changed

+13
-50
lines changed

1 file changed

+13
-50
lines changed

mocklogger/mocklogger.go

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
package mocklogger
33

44
import (
5-
"errors"
6-
"fmt"
7-
"os"
85
"time"
96

107
"github.com/deploymenttheory/go-api-http-client/logger" // Assuming this is the package where Logger interface is defined
118
"github.com/stretchr/testify/mock"
129
"go.uber.org/zap"
13-
"go.uber.org/zap/zapcore"
1410
)
1511

1612
// MockLogger is a mock type for the Logger interface, embedding a *zap.Logger to satisfy the type requirement.
@@ -30,12 +26,10 @@ func NewMockLogger() *MockLogger {
3026
// Ensure MockLogger implements the logger.Logger interface from the logger package
3127
var _ logger.Logger = (*MockLogger)(nil)
3228

29+
// GetLogLevel mocks the GetLogLevel method of the Logger interface.
3330
func (m *MockLogger) GetLogLevel() logger.LogLevel {
3431
args := m.Called()
35-
if args.Get(0) != nil { // Check if the Called method has a return value
36-
return args.Get(0).(logger.LogLevel)
37-
}
38-
return logger.LogLevelNone // Return LogLevelNone if no specific log level is set
32+
return args.Get(0).(logger.LogLevel) // Assuming that the first argument is the log level
3933
}
4034

4135
// SetLevel sets the logging level of the MockLogger.
@@ -47,111 +41,80 @@ func (m *MockLogger) SetLevel(level logger.LogLevel) {
4741

4842
// With adds contextual key-value pairs to the MockLogger and returns a new logger instance with this context.
4943
// This is useful for adding common fields to all subsequent logs produced by the logger.
50-
func (m *MockLogger) With(fields ...zapcore.Field) logger.Logger {
44+
func (m *MockLogger) With(fields ...zap.Field) logger.Logger {
5145
m.Called(fields)
52-
53-
return m
46+
newMock := NewMockLogger()
47+
newMock.logLevel = m.logLevel
48+
return newMock
5449
}
5550

5651
// Debug logs a message at the Debug level.
57-
func (m *MockLogger) Debug(msg string, fields ...zapcore.Field) {
52+
func (m *MockLogger) Debug(msg string, fields ...zap.Field) {
5853
m.Called(msg, fields)
59-
if m.logLevel <= logger.LogLevelDebug {
60-
fmt.Printf("[DEBUG] %s\n", msg)
61-
}
6254
}
6355

6456
// Info logs a message at the Info level.
65-
func (m *MockLogger) Info(msg string, fields ...zapcore.Field) {
57+
func (m *MockLogger) Info(msg string, fields ...zap.Field) {
6658
m.Called(msg, fields)
67-
if m.logLevel <= logger.LogLevelInfo {
68-
fmt.Printf("[INFO] %s\n", msg)
69-
}
7059
}
7160

7261
// Error logs a message at the Error level and returns an error.
73-
func (m *MockLogger) Error(msg string, fields ...zapcore.Field) error {
62+
func (m *MockLogger) Error(msg string, fields ...zap.Field) error {
7463
m.Called(msg, fields)
75-
if m.logLevel <= logger.LogLevelError {
76-
fmt.Printf("[ERROR] %s\n", msg)
77-
}
78-
return errors.New(msg)
64+
return m.Called(msg).Error(0)
7965
}
8066

8167
// Warn logs a message at the Warn level.
82-
func (m *MockLogger) Warn(msg string, fields ...zapcore.Field) {
68+
func (m *MockLogger) Warn(msg string, fields ...zap.Field) {
8369
m.Called(msg, fields)
84-
if m.logLevel <= logger.LogLevelWarn {
85-
fmt.Printf("[WARN] %s\n", msg)
86-
}
8770
}
8871

8972
// Panic logs a message at the Panic level and then panics.
90-
func (m *MockLogger) Panic(msg string, fields ...zapcore.Field) {
73+
func (m *MockLogger) Panic(msg string, fields ...zap.Field) {
9174
m.Called(msg, fields)
92-
if m.logLevel <= logger.LogLevelPanic {
93-
fmt.Printf("[PANIC] %s\n", msg)
94-
panic(msg)
95-
}
9675
}
9776

9877
// Fatal logs a message at the Fatal level and then calls os.Exit(1).
99-
func (m *MockLogger) Fatal(msg string, fields ...zapcore.Field) {
78+
func (m *MockLogger) Fatal(msg string, fields ...zap.Field) {
10079
m.Called(msg, fields)
101-
if m.logLevel <= logger.LogLevelFatal {
102-
fmt.Printf("[FATAL] %s\n", msg)
103-
os.Exit(1)
104-
}
10580
}
10681

10782
// LogRequestStart logs the start of an HTTP request.
10883
func (m *MockLogger) LogRequestStart(event string, requestID string, userID string, method string, url string, headers map[string][]string) {
10984
m.Called(event, requestID, userID, method, url, headers)
110-
// Mock logging implementation...
11185
}
11286

11387
// LogRequestEnd logs the end of an HTTP request.
11488
func (m *MockLogger) LogRequestEnd(event string, method string, url string, statusCode int, duration time.Duration) {
11589
m.Called(event, method, url, statusCode, duration)
116-
// Mock logging implementation...
11790
}
11891

11992
// LogError logs an error event.
12093
func (m *MockLogger) LogError(event string, method string, url string, statusCode int, serverStatusMessage string, err error, rawResponse string) {
12194
m.Called(event, method, url, statusCode, serverStatusMessage, err, rawResponse)
122-
// Mock logging implementation...
12395
}
12496

12597
// Example for LogAuthTokenError:
12698
func (m *MockLogger) LogAuthTokenError(event string, method string, url string, statusCode int, err error) {
12799
m.Called(event, method, url, statusCode, err)
128-
// Mock logging implementation...
129100
}
130101

131102
// LogCookies logs information about cookies.
132103
func (m *MockLogger) LogCookies(direction string, obj interface{}, method, url string) {
133-
// Use the mock framework to record that LogCookies was called with the specified arguments
134104
m.Called(direction, obj, method, url)
135-
fmt.Printf("[COOKIES] Direction: %s, Object: %v, Method: %s, URL: %s\n", direction, obj, method, url)
136105
}
137106

138107
// LogRetryAttempt logs a retry attempt.
139108
func (m *MockLogger) LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error) {
140109
m.Called(event, method, url, attempt, reason, waitDuration, err)
141-
// Mock logging implementation...
142-
fmt.Printf("[RETRY ATTEMPT] Event: %s, Method: %s, URL: %s, Attempt: %d, Reason: %s, Wait Duration: %s, Error: %v\n", event, method, url, attempt, reason, waitDuration, err)
143110
}
144111

145112
// LogRateLimiting logs rate limiting events.
146113
func (m *MockLogger) LogRateLimiting(event string, method string, url string, retryAfter string, waitDuration time.Duration) {
147114
m.Called(event, method, url, retryAfter, waitDuration)
148-
// Mock logging implementation...
149-
fmt.Printf("[RATE LIMITING] Event: %s, Method: %s, URL: %s, Retry After: %s, Wait Duration: %s\n", event, method, url, retryAfter, waitDuration)
150115
}
151116

152117
// LogResponse logs HTTP responses.
153118
func (m *MockLogger) LogResponse(event string, method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration) {
154119
m.Called(event, method, url, statusCode, responseBody, responseHeaders, duration)
155-
// Mock logging implementation...
156-
fmt.Printf("[RESPONSE] Event: %s, Method: %s, URL: %s, Status Code: %d, Response Body: %s, Response Headers: %v, Duration: %s\n", event, method, url, statusCode, responseBody, responseHeaders, duration)
157120
}

0 commit comments

Comments
 (0)