question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

winston.transports.File ignores file creation errors

See original GitHub issue

When adding File transport to a logger which points to a non-existing file and the directory does not have WRITE permissions, Winston does not catch ‘error’ event from the stream when it fails to create the log file. This prevents to catch the error in app code thus no one knows the file logger does not work at all because the error message from WriteStream is just stored in buffer and never gets written into the file or console.

Example code:

var logger = require('winston').Logger({
    transports: [
        new winston.transports.File({
            filename: '/var/log/server.log',
            level: 'debug'
        })
    ],
    emitErrs: true
});
logger.on('error', function (err) {
    console.error('Logging error info: ', JSON.stringify(err));
});

The file creation error in WriteStream is not captured and propagated to the logger. This could be easily fixed by capturing the ‘error’ event in file.js in File.prototype._createStream.createAndFlush method by adding:

self._stream.on('error', function(err) {
    self.emit('error', err);
});

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rjhilgefortcommented, Feb 26, 2018

I’m seeing this on 3.0.0-rc1

0reactions
eadsjrcommented, Mar 25, 2020

I’ve added the following to my bash execution script to prevent a repeat performance and provide a clue to someone examining the script as to what can go wrong.

# verify logging permission - user, group and read/write permission
if 
   [ "$(ls -ld log | awk '{print $3}')" != "$USER" ] || \
   [ "$(ls -ld log | awk '{print $4}')" != "$GROUP" ] ||  \ 
   [ "$(ls -ld log | awk '{print $1}' | head -c 3 | tail -c 2)" != "rw" ]
then
  echo "User '$USER' does not have appropriate permissions to access the 'log/' folder. Run 'chown -R $USER:$GROUP log/ ; chmod -R 750 log/' with admin privileges to prevent loss of logging functionality."
  echo "chown -R $USER:$GROUP log/ ; chmod -R 750 log/"
  exit 1
fi
Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston not displaying error details - node.js - Stack Overflow
It is a module that I am using to log some information to a file and some to a mysql database. require('dotenv').config(); const...
Read more >
winstonjs/winston - Gitter
i installed winston to use logger (for log rotation) but the logs contains ANSI ... It seems like Winston isn't logging error objects...
Read more >
How to use the winston.transports.Console function in ... - Snyk
To help you get started, we've selected a few winston.transports. ... File({ filename: 'error.log', level: 'error' }), new transports.File({ filename: ...
Read more >
winston - npm
It is possible to use multiple transports of the same type e.g. winston.transports.File when you construct the transport. const logger = winston ...
Read more >
Node.js Logging with Winston - Reflectoring
File ({ level: "error", filename: "logs/error.log", }), new transports.Console(), ], }); module.exports = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found