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

express-winston disappears in GCP Logs #278

Open
andreisaikouski opened this issue Oct 7, 2022 · 2 comments
Open

express-winston disappears in GCP Logs #278

andreisaikouski opened this issue Oct 7, 2022 · 2 comments

Comments

@andreisaikouski
Copy link

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
Copy link
Collaborator

yinzara commented Oct 7, 2022

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

@andreisaikouski
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants