bithavoc / express-winston

express.js middleware for winstonjs
https://www.npmjs.com/package/express-winston
MIT License
795 stars 187 forks source link

express-winston disappears in GCP Logs #278

Open andreisaikouski opened 1 year ago

andreisaikouski commented 1 year ago

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);
logger.info('server starting');

Importantly, all of this works great and like a charm at first!.

e.g. enter image description here

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

yinzara commented 1 year ago

What version of express-winston and what version of winston are you using? This defect was present in an older version.

andreisaikouski commented 1 year ago

Hi @yinzara!

express-winston 4.2.0 winston 3.8.1 @google-cloud/logging-winston 5.1.0 Node 16.17.1 NPM 8.15.0 running os: Debian GNU/Linux 10 (buster)