From 56010129fbaa986842f9f397cada33577ae76a68 Mon Sep 17 00:00:00 2001 From: Ashley Smith Date: Fri, 8 Nov 2024 13:01:52 -0700 Subject: [PATCH 1/2] format info --- src/utils/logging.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utils/logging.ts b/src/utils/logging.ts index 65ce853..997e6d7 100644 --- a/src/utils/logging.ts +++ b/src/utils/logging.ts @@ -1,15 +1,34 @@ 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; } + info.message = truncateMessage(info.message, 250); + if (info.meta) { + const {meta, ...rest} = info; + return rest; + } + return info; +}); + 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', })); @@ -22,4 +41,4 @@ export const getLoggingMiddleware = () => expressWinston.logger({ headerBlacklist: headersToRedact, }); -export default logger; +export default logger; \ No newline at end of file From 05461d50f131cba498f6f4f85752f06535615686 Mon Sep 17 00:00:00 2001 From: Ashley Smith Date: Fri, 8 Nov 2024 13:23:05 -0700 Subject: [PATCH 2/2] changes to log better --- src/app.ts | 17 ----------------- src/utils/logging.ts | 11 ++++------- 2 files changed, 4 insertions(+), 24 deletions(-) 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 997e6d7..f4cd17a 100644 --- a/src/utils/logging.ts +++ b/src/utils/logging.ts @@ -9,12 +9,8 @@ function truncateMessage(message: string, maxChars: number): string { const formatInfo = format((info) => { if (info.private) { return false; } - info.message = truncateMessage(info.message, 250); - if (info.meta) { - const {meta, ...rest} = info; - return rest; - } - return info; + const shortenedMessage = truncateMessage(info.message, 250); + return { ...info, message: shortenedMessage }; }); const logger = createLogger({ @@ -39,6 +35,7 @@ const headersToRedact = ['authorization', 'authentication']; export const getLoggingMiddleware = () => expressWinston.logger({ winstonInstance: logger, headerBlacklist: headersToRedact, + meta: false, }); -export default logger; \ No newline at end of file +export default logger;