Skip to content

Commit f7dc776

Browse files
authored
Merge pull request #238 from contentstack/fix/DX-3531-logHandler
Fix/dx 3531 log handler
2 parents 026d7bc + f873653 commit f7dc776

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version: 4.10.1
2+
#### Date: Oct-01-2025
3+
Enhancement: Added logHandler interceptors for request and response logging
4+
15
### Version: 4.10.0
26
#### Date: Sep-22-2025
37
Fix: Enhance retry logic to use configured retryDelay

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.10.0",
3+
"version": "4.10.1",
44
"type": "module",
55
"license": "MIT",
66
"main": "./dist/legacy/index.cjs",

src/lib/contentstack.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,67 @@ export function stack(config: StackConfig): StackClass {
104104
});
105105
};
106106
}
107+
// LogHandler interceptors
108+
if (config.debug) {
109+
// Request interceptor for logging
110+
client.interceptors.request.use((requestConfig: any) => {
111+
config.logHandler!('info', {
112+
type: 'request',
113+
method: requestConfig.method?.toUpperCase(),
114+
url: requestConfig.url,
115+
headers: requestConfig.headers,
116+
params: requestConfig.params,
117+
timestamp: new Date().toISOString()
118+
});
119+
return requestConfig;
120+
});
121+
122+
// Response interceptor for logging
123+
client.interceptors.response.use(
124+
(response: any) => {
125+
const level = getLogLevelFromStatus(response.status);
126+
config.logHandler!(level, {
127+
type: 'response',
128+
status: response.status,
129+
statusText: response.statusText,
130+
url: response.config?.url,
131+
method: response.config?.method?.toUpperCase(),
132+
headers: response.headers,
133+
data: response.data,
134+
timestamp: new Date().toISOString()
135+
});
136+
return response;
137+
},
138+
(error: any) => {
139+
const status = error.response?.status || 0;
140+
const level = getLogLevelFromStatus(status);
141+
config.logHandler!(level, {
142+
type: 'response_error',
143+
status: status,
144+
statusText: error.response?.statusText || error.message,
145+
url: error.config?.url,
146+
method: error.config?.method?.toUpperCase(),
147+
error: error.message,
148+
timestamp: new Date().toISOString()
149+
});
150+
throw error;
151+
}
152+
);
153+
}
154+
155+
// Helper function to determine log level based on HTTP status code
156+
function getLogLevelFromStatus(status: number): string {
157+
if (status >= 200 && status < 300) {
158+
return 'info';
159+
} else if (status >= 300 && status < 400) {
160+
return 'warn';
161+
} else if (status >= 400) {
162+
return 'error';
163+
} else {
164+
return 'debug';
165+
}
166+
}
167+
107168
// Retry policy handlers
108169
const errorHandler = (error: any) => {
109170
return retryResponseErrorHandler(error, config, client);

0 commit comments

Comments
 (0)