Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed with Winston
Asked Answered
I

3

7

I am getting an error with winston logging if I am using winston-daily-rotate-file. When I am building my application with webpack there are no errors but when I execute my build below error is coming(stack Trace):

/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)

My winston config is:

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',
        prettyPrint: true,
        handleExceptions: true,
        json: false,
        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;
Insolvent answered 22/8, 2020 at 14:55 Comment(0)
M
6

I had the exact same error, line number and everything, happening on aws elasticbeanstalk.

It worked locally, but when it got to aws, the error happened because node is running as user 'webapp'. It looks like that user doesn't have permission to write files to /var/log, which seems odd. So I changed it to log to /tmp and the error went away. Note: be sure the log files are owned by webapp (or whatever user runs the node app) or the error will persist.

Medarda answered 24/8, 2020 at 20:4 Comment(0)
H
0

In my case the directory the logs were supposed to be saved to didn't exist and that was causing the error.

Hasbeen answered 16/5, 2022 at 12:41 Comment(0)
P
0

In our case, the directory where PM2 logs were stored got exhausted. Once we cleared up space, the error disappeared.

Pandean answered 10/10, 2024 at 7:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.