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';
withrequire('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
3 Answers
Reset to default 8import * 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
版权声明:本文标题:javascript - How to import NodeJS winston-daily-rotate-file dependency? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741760630a2396383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论