Skip to content

Commit

Permalink
changes to log better
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysmithTTD committed Nov 8, 2024
1 parent 5601012 commit 05461d5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 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
11 changes: 4 additions & 7 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -39,6 +35,7 @@ const headersToRedact = ['authorization', 'authentication'];
export const getLoggingMiddleware = () => expressWinston.logger({
winstonInstance: logger,
headerBlacklist: headersToRedact,
meta: false,
});

export default logger;
export default logger;

0 comments on commit 05461d5

Please sign in to comment.