Skip to content

Commit

Permalink
feat: added query logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wwills2 committed Sep 24, 2024
1 parent 57b2b0f commit a89fcdd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ const mirrorQueryLogger = (query) => {
};

const appLogLevel = getConfig().APP.LOG_LEVEL;
const localLogging =
appLogLevel === 'silly' || appLogLevel === 'debug' ? localQueryLogger : false;
const mirrorLogging =
appLogLevel === 'silly' || appLogLevel === 'debug'
? mirrorQueryLogger
: false;
const localLogging = appLogLevel === 'silly' ? localQueryLogger : false;
const mirrorLogging = appLogLevel === 'silly' ? mirrorQueryLogger : false;

export default {
local: {
Expand Down
29 changes: 29 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import packageJson from '../package.json' assert { type: 'json' };
import datalayer from './datalayer';
import { Organization } from './models';
import { logger } from './config/logger.js';

const { CADT_API_KEY, READ_ONLY, IS_GOVERNANCE_BODY, USE_SIMULATOR } =
getConfig().APP;
Expand All @@ -40,6 +41,34 @@ app.use(
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Request logger middleware
app.use((req, res, next) => {
logger.verbose(`Received request: ${req.method} ${req.originalUrl}`, {
method: req.method,
url: req.originalUrl,
headers: req.headers,
timestamp: new Date().toISOString(),
});

const startTime = Date.now();

res.on('finish', () => {
const duration = Date.now() - startTime;
logger.verbose(
`Processed request: ${req.method} ${req.originalUrl}, status: ${res.statusCode}, duration: ${duration}ms`,
{
method: req.method,
url: req.originalUrl,
status: res.statusCode,
responseTime: duration,
timestamp: new Date().toISOString(),
},
);
});

next();
});

// Common assertions on every endpoint
app.use(async function (req, res, next) {
try {
Expand Down

0 comments on commit a89fcdd

Please sign in to comment.