Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format info to reduce length of long errors #91

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading