Set up the use of winston
and express-winston
logging and stream data into GCP Logging.
Set up looks like:
import winston from 'winston';
import ExpressWinston from 'express-winston';
import { LoggingWinston } from '@google-cloud/logging-winston';
import config from '../config';
const logFormat = winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`,
);
const transports: winston.transport[] = [
// This transport dumps into console
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), logFormat),
}),
new LoggingWinston({
projectId: config.projectId, // its a config
});
];
const logger = winston.createLogger({
level: config.logs.level, // defaults to 'info'
transports,
format: winston.format.combine(
winston.format.colorize(),
winston.format.json(),
winston.format.prettyPrint(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.metadata({
fillExcept: ['message', 'level', 'timestamp'],
}),
),
});
// Whitelisting request and response bodies to be logged
ExpressWinston.requestWhitelist.push('body');
ExpressWinston.responseWhitelist.push('body');
const expressLogger = ExpressWinston.logger({
level: config.logs.level, // defaults to 'info'
format: winston.format.combine(
winston.format.colorize(),
winston.format.json(),
winston.format.prettyPrint(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
),
transports,
// remove the following headers from getting logged
headerBlacklist: ['authorization', 'cookie', 'set-cookie'],
colorize: true,
msg: '{{res.statusCode}} {{req.method}} {{req.url}} {{res.responseTime}}ms',
ignoreRoute: (req) =>
ignoredRoutes.indexOf(req.originalUrl || req.url) >= 0,
});
export { logger, expressLogger };
This logger is then used as follows:
import express from 'express';
import { logger, expressLogger } from './logger';
// . . .
const app = express();
app.use(expressLogger);
Importantly, all of this works great and like a charm.
But then after a few days, the express logger stops logging -- without any error in the logs. The winston logger continues to work fine.
This forces me to restart the server to make it work again. Any suggestions on how to resolve this?
Thanks