diff --git a/src/app.ts b/src/app.ts index 9d98e6b..a1f42a7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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'; @@ -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()); diff --git a/src/utils/logging.ts b/src/utils/logging.ts index 65ce853..f4cd17a 100644 --- a/src/utils/logging.ts +++ b/src/utils/logging.ts @@ -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', })); @@ -20,6 +35,7 @@ const headersToRedact = ['authorization', 'authentication']; export const getLoggingMiddleware = () => expressWinston.logger({ winstonInstance: logger, headerBlacklist: headersToRedact, + meta: false, }); export default logger;