Skip to content

Commit

Permalink
standardise log for tracking server
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc committed Nov 18, 2024
1 parent 239349e commit b40b2ff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const createLogEntry = (ctx, status = 200, error = null) => {
const { writeKey, dataPlaneUrl } = ctx.request.query;

return {
timestamp: new Date().toISOString(),
user_agent: ctx.headers['user-agent'],
ip: ctx.ip,
endpoint: ctx.originalUrl,
status,
writeKey: writeKey || 'N/A',
dataPlaneUrl: dataPlaneUrl || 'N/A',
...(error && { error }),
};
};

module.exports = { createLogEntry };
10 changes: 7 additions & 3 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const { join } = require('path');
const Router = require('koa-router');
const axios = require('axios');
const { createLogEntry } = require('./logger');
require('dotenv').config();

const router = new Router();
Expand All @@ -24,6 +25,7 @@ router.get('/load', async (ctx) => {
} catch (err) {
ctx.response.body = 'failed to fetch rudder-js-sdk';
ctx.status = 400;
console.log(JSON.stringify(createLogEntry(ctx, 400, 'Failed to fetch rudder-js-sdk')));
return ctx;
}

Expand All @@ -35,14 +37,14 @@ router.get('/load', async (ctx) => {
});

const { writeKey, dataPlaneUrl } = ctx.request.query;
console.log('writeKey', writeKey);
console.log('dataplaneUrl', dataPlaneUrl);
if (!isValidDataPlaneURL(dataPlaneUrl) || !isValidWriteKey(writeKey)) {
console.log(`writeKey:${writeKey} or dataPlaneUrl:${dataPlaneUrl} is invalid or missing`);
ctx.response.body = {
error: 'writeKey or dataPlaneUrl is invalid or missing',
};
ctx.status = 400;
console.log(
JSON.stringify(createLogEntry(ctx, 400, 'Invalid or missing writeKey/dataPlaneUrl')),
);
return ctx;
}

Expand All @@ -63,12 +65,14 @@ router.get('/load', async (ctx) => {
// console.log("d", d);
ctx.response.body = d + rudderJsCode + deviceModeInit;
ctx.set('Content-Type', 'application/javascript');
console.log(JSON.stringify(createLogEntry(ctx)));
return ctx;
});

router.get('/health', (ctx) => {
ctx.response.body = 'Server is Up';
ctx.status = 200;
console.log(JSON.stringify(createLogEntry(ctx)));
return ctx;
});

Expand Down

0 comments on commit b40b2ff

Please sign in to comment.