javascriptnode.jsloggingwinstonloggly

Winston transport for Loggly not working consistently


I have Winston set up with 3 transports: Console, File, and Loggly (using https://github.com/loggly/winston-loggly-bulk).

While the Console and File transports log properly, my Loggly transport only logs my initial "Server listening on port 3001" log and nothing else, and I don't understand why.

logger-utils.ts:

import * as winston from 'winston';
import { Loggly } from 'winston-loggly-bulk';

export const createLogger = () => {
  return winston.createLogger({
    format: winston.format.combine(
      winston.format.errors({ stack: true }),
      winston.format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss ZZ',
      }),
      winston.format.json(),
    ),
    transports: [
      new winston.transports.Console({
        level: process.env.LOGGING_LEVEL,
      }),
      new winston.transports.File({ filename: `logs/my-app.log` }),
      new Loggly({
        token: '<token>',
        subdomain: '<subdomain>',
        tags: ['Winston-NodeJS'],
        json: true,
      }),
    ],
  });
};

logger.ts:

import { createLogger } from './logger-utils';

const logger = createLogger();

export default logger;

server.ts:

import app from './modules/app/app';
import logger from './core/logger/logger';

app.listen(PORT, () => {
  logger.info(`Server listening on port ${3001}`);
});

app.middleware.ts:

import logger from '../../core/logger/logger';

export const initializeApp = async (ctx: AppContext) => {
  logger.info('initializeApp', { ctx });
  ....

So, I start the server and run through a flow, and when I check the Console and File I see that it logs both the messages in server.ts and app.middleware.ts but when I check my Loggly dashboard I only see the log from server.ts.

I feel like I'm missing something incredibly obvious but I don't understand why it won't log my info in app.middleware.ts despite logging my stuff in server.ts, and it makes even less sense when I consider that both Console and File transports are logging everything correctly.

Any ideas on how to fix this issue so that my Loggly transport works like my other transports?


Solution

  • ctx object has circular reference inside and probably serialization failed at the Loggly end. Logging without the ctx object should fix the issue.