【发布时间】:2021-10-21 21:32:08
【问题描述】:
我尝试使用节点 js.winston 2.4.0 版本创建 winston(记录器文件),它成功地将信息和错误存储在日志文件中。但最新版本它创建的日志文件但无法存储错误和信息消息。如何修复它
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()
}));
}
app.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"}
【问题讨论】:
-
你给日志目录写权限了吗?
-
如何给日志目录写权限? @narayansharma91
-
chmod - R 777 /your/log/directory(如果您使用的是 ubuntu)
-
不要破坏您的帖子。通过在本网站上发布,您已不可撤销地授予 Stack Exchange 网络以CC BY-SA 4.0 license 分发该内容的权利,只要它认为合适即可。有关删除的替代方法,请参阅:I've thought better of my question; can I delete it?