nestjs - NestJs 将 Morgan 和 Nest-Winston 的输出合并到一个文件中
问题描述
我正在尝试使用nest - winston将所有快速日志和未处理的异常记录在一个文件中。关注以下文章::
- https://www.npmjs.com/package/nest-winston
- https://www.section.io/engineering-education/logging-with-winston/
- Node.js - 记录 / 使用 morgan 和 winston
我对此进行了研究,发现应该使用 morgan 来记录快速日志。
const winston = require("winston");
const morgan = require("morgan");
async function bootstrap() {
const appOptions = {
cors: true,
logger: WinstonModule.createLogger({
transports: [
new winston.transports.Console({}), // ==> 1
new winston.transports.File({
filename:
"logs/Combined-" + new Date(Date.now()).toDateString() + ".log",
level: "info",
handleExceptions: true,
}),
new winston.transports.File({
filename:
"logs/Errors-" + new Date(Date.now()).toDateString() + ".log",
level: "error",
}),
],
format: winston.format.combine(
winston.format.timestamp({
format: "MMM-DD-YYYY HH:mm:ss",
}),
winston.format.printf(
(error) => `${error.level}: ${[error.timestamp]}: ${error.message}`
)
),
}),
};
const app = await NestFactory.create(ApplicationModule, appOptions);
app.setGlobalPrefix("api");
app.use(morgan("combined", { stream: winston.stream.write })); // ==> 2
const options = new DocumentBuilder()
.setTitle("xx")
.setDescription("xx")
.setVersion("1.0")
.setBasePath("api")
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup("/docs", app, document);
await app.listen(3000);
}
bootstrap();
Winston 正确地将数据记录到文件中。
error: Jul-08-2021 18:12:34: Input data validation failed
error: Jul-08-2021 18:26:28: Input data validation failed
error: Jul-08-2021 18:27:09: Input data validation failed
error: Jul-08-2021 20:57:52: Input data validation failed
info: Jul-08-2021 21:47:40: Mapped {/api/pricing/:id, GET} route
info: Jul-08-2021 21:47:40: Mapped {/api/route/:slug, DELETE} route
info: Jul-08-2021 21:47:40: Nest application successfully started
现在我想记录我插入摩根的所有快速日志,如代码所示(第 2 点)。它记录到控制台,但不记录到文件。但是,如果我注释掉第 1 点,即登录到控制台。项目没有启动。它在低于 2 行后被卡住。我等了 15 分钟,但没有任何进展。
[nodemon] restarting due to changes...
[nodemon] starting `node ./index index.js`
解决方案
为将来的使用和挑战回答这个问题为时已晚您可以将nestjs应用程序配置为使用winston logger并使用以下配置登录文件
在 main.ts 添加这些代码
// Import these two line
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.json(),
winston.format.splat(),
winston.format.prettyPrint(),
winston.format.label({ label: 'Smartedu' }),
),
transports: [
new winston.transports.File({
filename: 'questions_server_error.log',
level: 'error',
}),
new winston.transports.Console(),
],
exceptionHandlers: [
new winston.transports.File({ filename: 'questions_exceptions.log' }),
],
exitOnError: false,
}),
});
}
确保你执行 npm install 这两个包
npm i nest-winston
npm i winston
您可以在传输部分添加任何记录器级别,请参阅 winston 文档 然后一切正常
推荐阅读
- django - 鹡鸰表单文件上传
- html - Angular 材质“mat-slide-toggle”覆盖
- javascript - 透明选项在具有 nodeIntegration 的电子中不起作用
- mysql - Mysql 复制 - 一主两从
- excel - 有没有办法根据单元格值中的多个条目在 excel 中通用重复行
- php - 数据库错误:无法建立连接,因为目标机器使用 EC2 + Linux + XAMPP 主动拒绝它
- javascript - 无效的挂钩调用。钩子只能在函数组件的主体内部调用。有任何想法吗?
- c++ - 三种方式读取和存储对象数组的性能比较
- python - Python Selenium在for循环中获取页面
- php - 通过不那么清晰的标记分割内容 - PHP preg_split 或爆炸