generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocklogger.go
156 lines (133 loc) · 5.7 KB
/
mocklogger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package mocklogger
import (
"errors"
"fmt"
"os"
"time"
"github.com/deploymenttheory/go-api-http-client/logger" // Assuming this is the package where Logger interface is defined
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// MockLogger is a mock type for the Logger interface, embedding a *zap.Logger to satisfy the type requirement.
type MockLogger struct {
mock.Mock
*zap.Logger
logLevel logger.LogLevel
}
// NewMockLogger creates a new instance of MockLogger with an embedded no-op *zap.Logger.
func NewMockLogger() *MockLogger {
return &MockLogger{
Logger: zap.NewNop(),
}
}
// Ensure MockLogger implements the logger.Logger interface from the logger package
var _ logger.Logger = (*MockLogger)(nil)
func (m *MockLogger) GetLogLevel() logger.LogLevel {
args := m.Called()
if args.Get(0) != nil { // Check if the Called method has a return value
return args.Get(0).(logger.LogLevel)
}
return logger.LogLevelNone // Return LogLevelNone if no specific log level is set
}
// SetLevel sets the logging level of the MockLogger.
// This controls the verbosity of the logger, allowing it to filter out logs below the set level.
func (m *MockLogger) SetLevel(level logger.LogLevel) {
m.logLevel = level
m.Called(level)
}
// With adds contextual key-value pairs to the MockLogger and returns a new logger instance with this context.
// This is useful for adding common fields to all subsequent logs produced by the logger.
func (m *MockLogger) With(fields ...zapcore.Field) logger.Logger {
m.Called(fields)
return m
}
// Debug logs a message at the Debug level.
func (m *MockLogger) Debug(msg string, fields ...zapcore.Field) {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelDebug {
fmt.Printf("[DEBUG] %s\n", msg)
}
}
// Info logs a message at the Info level.
func (m *MockLogger) Info(msg string, fields ...zapcore.Field) {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelInfo {
fmt.Printf("[INFO] %s\n", msg)
}
}
// Error logs a message at the Error level and returns an error.
func (m *MockLogger) Error(msg string, fields ...zapcore.Field) error {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelError {
fmt.Printf("[ERROR] %s\n", msg)
}
return errors.New(msg)
}
// Warn logs a message at the Warn level.
func (m *MockLogger) Warn(msg string, fields ...zapcore.Field) {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelWarn {
fmt.Printf("[WARN] %s\n", msg)
}
}
// Panic logs a message at the Panic level and then panics.
func (m *MockLogger) Panic(msg string, fields ...zapcore.Field) {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelPanic {
fmt.Printf("[PANIC] %s\n", msg)
panic(msg)
}
}
// Fatal logs a message at the Fatal level and then calls os.Exit(1).
func (m *MockLogger) Fatal(msg string, fields ...zapcore.Field) {
m.Called(msg, fields)
if m.logLevel <= logger.LogLevelFatal {
fmt.Printf("[FATAL] %s\n", msg)
os.Exit(1)
}
}
// LogRequestStart logs the start of an HTTP request.
func (m *MockLogger) LogRequestStart(event string, requestID string, userID string, method string, url string, headers map[string][]string) {
m.Called(event, requestID, userID, method, url, headers)
// Mock logging implementation...
}
// LogRequestEnd logs the end of an HTTP request.
func (m *MockLogger) LogRequestEnd(event string, method string, url string, statusCode int, duration time.Duration) {
m.Called(event, method, url, statusCode, duration)
// Mock logging implementation...
}
// LogError logs an error event.
func (m *MockLogger) LogError(event string, method string, url string, statusCode int, serverStatusMessage string, err error, rawResponse string) {
m.Called(event, method, url, statusCode, serverStatusMessage, err, rawResponse)
// Mock logging implementation...
}
// Example for LogAuthTokenError:
func (m *MockLogger) LogAuthTokenError(event string, method string, url string, statusCode int, err error) {
m.Called(event, method, url, statusCode, err)
// Mock logging implementation...
}
// LogCookies logs information about cookies.
func (m *MockLogger) LogCookies(direction string, obj interface{}, method, url string) {
// Use the mock framework to record that LogCookies was called with the specified arguments
m.Called(direction, obj, method, url)
fmt.Printf("[COOKIES] Direction: %s, Object: %v, Method: %s, URL: %s\n", direction, obj, method, url)
}
// LogRetryAttempt logs a retry attempt.
func (m *MockLogger) LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error) {
m.Called(event, method, url, attempt, reason, waitDuration, err)
// Mock logging implementation...
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)
}
// LogRateLimiting logs rate limiting events.
func (m *MockLogger) LogRateLimiting(event string, method string, url string, retryAfter string, waitDuration time.Duration) {
m.Called(event, method, url, retryAfter, waitDuration)
// Mock logging implementation...
fmt.Printf("[RATE LIMITING] Event: %s, Method: %s, URL: %s, Retry After: %s, Wait Duration: %s\n", event, method, url, retryAfter, waitDuration)
}
// LogResponse logs HTTP responses.
func (m *MockLogger) LogResponse(event string, method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration) {
m.Called(event, method, url, statusCode, responseBody, responseHeaders, duration)
// Mock logging implementation...
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)
}