winstonjs / winston-daily-rotate-file

A transport for winston which logs to a rotating file each day.
MIT License
895 stars 155 forks source link

Filename Parameter Not Accepting String Assignment for TypeScript #304

Closed rrandall91 closed 3 years ago

rrandall91 commented 3 years ago

We're receiving the following error message when attempting to instantiate the DailyRotateFile class in a TypeScript file. The configuration works fine when using a standard JavaScript file.

TSError: ⨯ Unable to compile TypeScript:
src/config/winston.config.ts(29,5): error TS2322: Type 'string' is not assignable to type 'undefined'.

Configuration code

  winston.add(new DailyRotateFile({
    level: "error",
    filename: "./logs/errors-%DATE%.log",
    datePattern: "YYYY-MM-DD",
    format: format.combine(
      format.timestamp(),
      format.align(),
      format.printf(info => `[${info.timestamp}] ${info.level}: ${info.message}`)
    ),
    zippedArchive: true,
    handleExceptions: true,
    handleRejections: true,
  }));
mattberther commented 3 years ago

Please help me understand where the "filename" parameter fits into this problem? Asked another way, please provide the smallest working code sample to reliably reproduce this issue.

rrandall91 commented 3 years ago

That was the line number for the filename parameter. I've already reverted the problem code but will attempt again and update this issue.

rrandall91 commented 3 years ago

winston.config.ts

import { Application } from "express";
import winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
import expressWinston from "express-winston";

const { format } = winston;

export function configureRequestLogger(app: Application) {
  if (process.env.NODE_ENV !== "production") {
    console.info("Configuring Winston Request Logger...");

    // HTTP Logger for development
    app.use(expressWinston.logger({
      transports: [ new winston.transports.Console({ format: format.simple() }) ],
      format: format.combine(
        format.colorize(),
        format.json()
      ),
      headerBlacklist: [ "authorization" ]
    }));
  }
};

export function configureErrorLogger(app: Application) {
  console.info("Configuring Winston Error Logger...");

  winston.add(new DailyRotateFile({
    level: "error",
    filename: "./logs/errors-%DATE%.log",
    datePattern: "YYYY-MM-DD",
    format: format.combine(
      format.timestamp(),
      format.align(),
      format.printf(info => `[${info.timestamp}] ${info.level}: ${info.message}`)
    ),
    zippedArchive: true,
    handleExceptions: true,
    handleRejections: true,
  }));

  if (process.env.NODE_ENV !== "production") {
    winston.add(new winston.transports.Console({
      level: "info",
      format: format.combine(
        format.timestamp(),
        format.colorize(),
        format.printf(info => `[${info.timestamp}] ${info.level}: ${info.message}`),
      ),
      handleExceptions: true,
    }));
  }

  app.use(expressWinston.errorLogger({
    winstonInstance: winston,
    headerBlacklist: [ "authorization" ]
  }));
};

Error message:

calendar_1                   | [nodemon] restarting due to changes...
calendar_1                   | [nodemon] starting `ts-node ./src/index.ts index.js`
calendar_1                   | 
calendar_1                   | /app/node_modules/ts-node/src/index.ts:513
calendar_1                   |     return new TSError(diagnosticText, diagnosticCodes)
calendar_1                   |            ^
calendar_1                   | TSError: ⨯ Unable to compile TypeScript:
calendar_1                   | src/config/winston.config.ts(29,5): error TS2322: Type 'string' is not assignable to type 'undefined'.
calendar_1                   | src/config/winston.config.ts(54,5): error TS2740: Type 'typeof winston' is missing the following properties from type 'Logger': silent, levels, profilers, close, and 69 more.
calendar_1                   | 
calendar_1                   |     at createTSError (/app/node_modules/ts-node/src/index.ts:513:12)
calendar_1                   |     at reportTSError (/app/node_modules/ts-node/src/index.ts:517:19)
calendar_1                   |     at getOutput (/app/node_modules/ts-node/src/index.ts:752:36)
calendar_1                   |     at Object.compile (/app/node_modules/ts-node/src/index.ts:968:32)
calendar_1                   |     at Module.m._compile (/app/node_modules/ts-node/src/index.ts:1056:42)
calendar_1                   |     at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
calendar_1                   |     at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:1059:12)
calendar_1                   |     at Module.load (internal/modules/cjs/loader.js:928:32)
calendar_1                   |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
calendar_1                   |     at Module.require (internal/modules/cjs/loader.js:952:19)
calendar_1                   | [nodemon] app crashed - waiting for file changes before starting...
mattberther commented 3 years ago

@rrandall91 Thank you. This appears to be related to #297. I can reproduce your issue with this minimal code example:

import DailyRotateFile from "winston-daily-rotate-file";

new DailyRotateFile({
    filename: "./logs/errors-%DATE%.log",
    handleExceptions: true,
    handleRejections: true,
  });

Removing handleRejections fixes the issue. If you can confirm on your side, I'll add the handleRejections option (and any other missing ones) on #307.

mattberther commented 3 years ago

@rrandall91 I've added an update to #307 so you can test if that version solves the issue. Looks like handleRejections was added to the type at https://github.com/winstonjs/winston-transport/commit/868d6577956f82ee0b021b119a4de938c61645f7, and #307 now includes the most recent version of winston-transport.

mattberther commented 3 years ago

Resolved with #307 and pushed to npm as winston-daily-rotate-file@4.5.1.

rrandall91 commented 3 years ago

@mattberther thanks for checking into this. I've been swamped at work but I'll check out the fix soon.

plaftsis commented 3 years ago

The issue still persists in winston-daily-rotate-file@4.5.5.

rrandall91 commented 3 years ago

@plaftsis I have working code using version 4.5.3

    winston.add(new DailyRotateFile({
      level: "error",
      filename: "./logs/errors-%DATE%.log",
      datePattern: "YYYY-MM-DD",
      format: format.combine(
        format.timestamp(),
        format.align(),
        format.printf(info => `[${info.timestamp}] ${info.level}: ${info.message}`)
      ),
      zippedArchive: true,
      handleExceptions: true,
    }));