Skip to content

Commit

Permalink
Merge pull request #91 from IABTechLab/ans-UID2-4409-limit-logging-of…
Browse files Browse the repository at this point in the history
…-errors-tc-portal

format info to reduce length of long errors
  • Loading branch information
ashleysmithTTD authored Nov 8, 2024
2 parents 0dcb6b7 + 05461d5 commit 33d6fb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
17 changes: 0 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import cookieParser from 'cookie-parser';
import express, { NextFunction, Request, Response } from 'express';
import winstonExpress from 'express-winston';
import Handlebars from 'hbs';
import helmet from 'helmet';
import createError from 'http-errors';
import i18n from 'i18n';
import path from 'path';
import winston from 'winston';

import makeMetricsApiMiddleware from './middleware/metrics';
import indexRouter from './routes/index';
Expand Down Expand Up @@ -39,21 +37,6 @@ app.use(
}),
);

app.use(winstonExpress.logger({
transports: [
new winston.transports.Console(),
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json(),
),
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: 'HTTP {{req.method}} {{req.url}}', // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: false, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute: (_req, _res) => false, // optional: allows to skip some log messages based on request and/or response
}));

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
Expand Down
18 changes: 17 additions & 1 deletion src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import expressWinston from 'express-winston';
import winston, { createLogger } from 'winston';
import winston, { createLogger, format } from 'winston';

import { isProduction } from './process';

function truncateMessage(message: string, maxChars: number): string {
return message.length > maxChars ? message.substring(0, maxChars) : message;
}

const formatInfo = format((info) => {
if (info.private) { return false; }
const shortenedMessage = truncateMessage(info.message, 250);
return { ...info, message: shortenedMessage };
});

const logger = createLogger({
transports: [
new winston.transports.Console({
level: isProduction ? 'info' : 'debug',
}),
],
format: format.combine(
formatInfo(),
format.json(),
),
});

logger.exceptions.handle(new winston.transports.Console({
level: isProduction ? 'info' : 'debug',
}));
Expand All @@ -20,6 +35,7 @@ const headersToRedact = ['authorization', 'authentication'];
export const getLoggingMiddleware = () => expressWinston.logger({
winstonInstance: logger,
headerBlacklist: headersToRedact,
meta: false,
});

export default logger;

0 comments on commit 33d6fb0

Please sign in to comment.