6

I am able to use winston-daily-rotate-file dependency by using require.

var DailyRotateFile = require('winston-daily-rotate-file');

But when I try to import like below, it is not working. How to resolve it?

import * as DailyRotateFile from 'winston-daily-rotate-file';

custom-logger.js

import { createLogger, format, transports } from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import fs from 'fs';
import path from 'path';

const env = process.env.NODE_ENV || 'development';
const logDir = 'log';
if(!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const dailyRotateFileTransport = new DailyRotateFile({
    filename: `${logDir}/%DATE%-results.log`,
    datePattern: 'YYYY-MM-DD',
    maxSize: '1k'
})

const logger = createLogger({
    level: env === 'development' ? 'debug' : 'info',
    format: format.combine(
        format.label({ label: path.basename(process.mainModule.filename)}),
        //format.colorize(),
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
        format.json()
    ),
    transports: [
        new transports.Console({
            level: 'info',
            format: format.combine(
                format.printf(
                    info => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}`
                )
            )
        }),
        dailyRotateFileTransport
    ]
});

module.exports = logger;

While running the application, getting the below error

var dailyRotateFileTransport = new DailyRotateFile({
                               ^

TypeError: DailyRotateFile is not a constructor

Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30

3 Answers3

7
import * as winston from 'winston';
import 'winston-daily-rotate-file';
import appRoot from 'app-root-path';


const logger = winston.createLogger({
    transports: [
        new winston.transports.DailyRotateFile ({
            filename: 'application-%DATE%.log',
            dirname: `${appRoot}/logs/`,
            level: 'info',
            handleExceptions: true,
            colorize: true,
            json: false,
            zippedArchive: true,
            maxSize: '20m',
            maxFiles: '14d'        
        })
    ],
    exitOnError: false
});

logger.stream = {
    write: function(message, encoding) {
      logger.info(message);
    },
  };

export default logger;

This configures the logger for Daily rotation.

user12575927
  • 311
  • 3
  • 8
0

Try this:

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

const daily_rotate_transport = new WinstonDailyRotate({
  filename: "./logs/app",
  datePattern: "YYYY-MM/DD[.log]",
});
Elletlar
  • 3,136
  • 7
  • 32
  • 38
-2

try this

import DailyRotateFile = require("winston-daily-rotate-file");

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106