admin管理员组

文章数量:1307610

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: formatbine(
        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: formatbine(
                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

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.bine(
        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.bine(
                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

Share Improve this question edited Aug 8, 2019 at 5:57 Alexpandiyan Chokkan asked Jun 6, 2019 at 7:14 Alexpandiyan ChokkanAlexpandiyan Chokkan 1,0751 gold badge12 silver badges30 bronze badges 7
  • What is the exact error that you receive? – Dzhuneyt Commented Jun 6, 2019 at 7:18
  • @Dzhuneyt i've the content with error along with custom-logger file. – Alexpandiyan Chokkan Commented Jun 6, 2019 at 7:30
  • Try replacing import * as DailyRotateFile from 'winston-daily-rotate-file'; with require('winston-daily-rotate-file') – Avanthika Commented Jun 6, 2019 at 7:32
  • github./winstonjs/winston-daily-rotate-file/issues/90 - Take a look at this. – Avanthika Commented Jun 6, 2019 at 7:32
  • @Avanthika I have done it earlier. It is working fine when I use require. I would like to use the import. – Alexpandiyan Chokkan Commented Jun 6, 2019 at 7:43
 |  Show 2 more ments

3 Answers 3

Reset to default 8
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.

Try this:

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

const daily_rotate_transport = new WinstonDailyRotate({
  filename: "./logs/app",
  datePattern: "YYYY-MM/DD[.log]",
});

try this

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

本文标签: javascriptHow to import NodeJS winstondailyrotatefile dependencyStack Overflow