如何解决错误[ERR_STREAM_DESTROYED]:使用Winston销毁流后无法调用写入
如果我使用 winston-daily-rotate-file ,则Winston日志记录会出现错误。 当我使用 webpack 构建我的应用程序时,没有错误,但是当我执行我的构建时,下面的错误来了(堆栈跟踪):
/app/web-app/server/node_modules/winston/lib/winston/logger.js:307
throw ex;
^
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
at doWrite (_stream_writable.js:399:19)
at writeOrBuffer (_stream_writable.js:387:5)
at WriteStream.Writable.write (_stream_writable.js:318:11)
at Object.<anonymous> (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/file-stream-rotator/FileStreamrotator.js:616:26)
at DailyRotateFile.log (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston-daily-rotate-file/daily-rotate-file.js:157:20)
at DailyRotateFile._write (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston-transport/index.js:82:19)
at doWrite (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:428:64)
at writeOrBuffer (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:417:5)
at DailyRotateFile.Writable.write (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:334:11)
at DerivedLogger.ondata (/mnt/e/workspace/codebase/hermes_dashboard/app/web-app/server/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:681:20)
at DerivedLogger.emit (events.js:327:22)
我的winston配置是:
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
var options = {
file: {
zippedArchive: true,prettyPrint: true,json: false,filename: `/app/web-app/server/logs/server_logs/server.log`,datePattern: '.yyyy-MM-dd',handleExceptions: true,maxsize: '10m',// 10MB
maxFiles: 5,colorize: false,},console: {
level: 'debug',colorize: true,};
var logger = new winston.createLogger({
transports: [
// new winston.transports.DailyRotateFile(options.file),new DailyRotateFile(options.file),new winston.transports.Console(options.console),],exitOnError: false,// do not exit on handled exceptions
});
logger.stream = {
write: message => {
logger.info(message);
},};
module.exports = logger;
解决方法
我在aws elasticbeanstalk上发生了完全相同的错误,行号和所有其他事情。
它在本地工作,但是到aws时,发生了错误,因为节点以用户“ webapp”的身份运行。似乎该用户无权将文件写入/ var / log,这似乎很奇怪。所以我将其更改为登录到/ tmp,错误消失了。注意:请确保日志文件归webapp拥有(或任何运行节点应用程序的用户),否则错误将持续存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。