-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
57 lines (48 loc) · 1.69 KB
/
Copy pathrun.js
File metadata and controls
57 lines (48 loc) · 1.69 KB
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
/**
* Standalone axios example – logs outbound HTTP requests to MongoDB
* Run from repo root: npm run build && cd example/standalone && npm install && npm start
* Or from example/standalone: npm install && node run.js (requires parent dist/)
*/
const axios = require('axios');
const { StandaloneApiLogger, createAxiosLogger } = require('../../dist');
const options = {
mongoUri: process.env.MONGO_URI || 'mongodb://localhost:27017',
databaseName: 'api_logger_example',
collectionName: 'standalone_logs',
maskFields: ['password', 'token'],
logResponseBody: true,
logRequestBody: true,
};
async function main() {
const logger = new StandaloneApiLogger(options);
await logger.init();
const axiosLogger = createAxiosLogger(logger, () => ({
id: 'standalone-user',
email: 'standalone@example.com',
}));
axios.interceptors.request.use(axiosLogger.request);
axios.interceptors.response.use(axiosLogger.response, axiosLogger.error);
console.log('Making logged requests...');
try {
const getRes = await axios.get('https://httpbin.org/get', { params: { foo: 'bar' } });
console.log('GET response status:', getRes.status);
} catch (err) {
console.log('GET error (may be network):', err.message);
}
try {
const postRes = await axios.post('https://httpbin.org/post', {
name: 'John',
email: 'john@example.com',
password: 'secret',
});
console.log('POST response status:', postRes.status);
} catch (err) {
console.log('POST error (may be network):', err.message);
}
await logger.close();
console.log('Done. Check api_logger_example.standalone_logs in MongoDB.');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});