node.js - winston 3.2.1 版本不创建日志文件
问题描述
我尝试使用节点 js.winston 2.4.0 版本创建 winston(记录器文件),它成功地在日志文件中存储信息和错误。但最新版本它创建的日志文件但无法存储错误和信息消息。如何修复它
温斯顿.js
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
应用程序.js
/**
* @fileoverview Root file of the application.
*/
// Import ExpressJS
const winston = require('winston');
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => winston.info(`Server Started and Listening on port ${port}`));
我得到了输出
winston] Attempt to write logs with no transports {"message":"Server Started and Listening on port 4202","level":"info"}
解决方案
您需要将记录器对象从Winston.js
文件导入到app.js
. 在您的app.js
代码中,Winston 导入适用于 npm 包,而不适用于在Winston.js
.
在 Winston.js 中,导出记录器对象:
var winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
module.exports = logger;
在 app.js 中,导入后使用该记录器对象。
const logger = require('./Winston'); //assuming Winston.js is in the same folder level as app.js
const express = require('express');
const app = express();
// StartUp Processes
require('./startup/logging_error_startup');
require('./startup/routes_startup')(app);
require('./startup/check_security_key_startup')();
require('./startup/validation_startup')();
require('./startup/swagger_startup')(app);
require('./startup/production_startup')(app);
// Start the server
const port = process.env.PORT || 4202;
app.listen(port, () => logger.info(`Server Started and Listening on port ${port}`));
推荐阅读
- optimization - 如何区分负变量和正变量?
- c# - 如何从 linq 中的数据表中动态选择?
- c++ - 构建makefile的问题
- python - Python Split on \t 'fooled' by text with )
- django - 我在 CBV django 中收到有关 TemplateView 的错误
- javascript - 如何更新棒棒糖图表的填充和排序值?
- python - 如果按钮是由for循环制作的,如何在单击按钮时删除按钮?
- vue.js - 如何在 Vue 中显示自定义输入的文件名?
- google-sheets - Google 表格 - 查询、选择 x 或 y 并排除 0 个结果
- json - 在 Flutter 中从 JSON 创建一个列表