gpt4 book ai didi

javascript - 如何导入 NodeJS winston-daily-rotate-file 依赖项?

转载 作者:行者123 更新时间:2023-12-04 12:25:28 25 4
gpt4 key购买 nike

我可以使用 winston-daily-rotate-file使用 require 的依赖关系.

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

但是当我尝试像下面这样导入时,它不起作用。如何解决?
import * as DailyRotateFile from 'winston-daily-rotate-file';

自定义记录器.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;

运行应用程序时,出现以下错误
var dailyRotateFileTransport = new DailyRotateFile({
^

TypeError: DailyRotateFile is not a constructor

最佳答案

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;

这将记录器配置为每日轮换。

关于javascript - 如何导入 NodeJS winston-daily-rotate-file 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56472563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com