Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transports.File not writing to file #1001

Closed
snwflake opened this issue Apr 1, 2017 · 18 comments
Closed

transports.File not writing to file #1001

snwflake opened this issue Apr 1, 2017 · 18 comments
Labels
winston-file Issues to move to `winston-file` when we create it

Comments

@snwflake
Copy link

snwflake commented Apr 1, 2017

I know this was already asked, several times, but either there was no fix, or it just got closed.

$ npm -v
4.4.4

winston: 2.3.1

My logger:

const winston = require('winston');

var logger = new (winston.Logger)({
  /*level: 'error',*/                    // doesn't make a difference
  exitOnError: true,
  transports: [
    new (winston.transports.File)({
      filename: __dirname + '/error.log',
      level: 'error',
      handleExceptions: true,
      humanReadableUnhandledException: true,
      json: true
    }),
    new (winston.transports.Console)()
  ]
});

The console transport works fine when calling e.g.

logger.log('error', 'some message', trace);

But the file transport refuses to work. It doesn't matter if the file exists or not.
It worked fine when I used the default logger winston.log('error', 'bla', someThing);.
I also invoke the onLogging event:

logger.on('logging', (transport, level, msg, meta) => {
  if(level === 'error' || level === 'warn'){
    // tell the user that something went wrong.
  }
});
@mhuggins
Copy link

I'm having the same problem.

@mhawila
Copy link

mhawila commented Aug 21, 2017

I have the same issue and I have to say I have encountered a number of questions asking the same thing. Some from 2 years back!! Looks like winston is not going to fix this one!

@mhuggins and @snwflake did you guys find a solution to this?

@mhuggins
Copy link

I don't recall, but I remember running into an issue where the file and/or directory didn't exist already. Winston won't create the directory for you, it needs to already exist.

@sleekmountaincat
Copy link

having same issue. any news on this?

@snwflake
Copy link
Author

@mhawila nope, wrote my own logger.

@Vindexus
Copy link

I'm encountering the same issue.

@tsulatsitamim
Copy link

tsulatsitamim commented Jul 30, 2018

  winston.exceptions.handle(
    new winston.transportsConsole(),
    new winston.transports.File({ filename: 'logs/test.log' })
  );

throw new Error('Exception Handler');

At first this log to both console and file, but the next hour it doesn't write to file, but still log to console.

Any Idea?

Set

  exitOnError: false;

Resolve my problem, I think because winston exit with no status code.

@gnanasuriyan
Copy link

gnanasuriyan commented Nov 21, 2018

I had encountered with same issue and looks like winston failed to handle relative path. For an example, winston failed to create file when i use logs/errors.log but its started working fine when i set complete path like ${__dirname}/logs/error.log.

Hope it will help!

@superbogy
Copy link

whats wrong with this repo, i don't want use it any more~!

@occultskyrong
Copy link

I think it has something to do with the path you set for filename .
If the filename just like /a/b/error.log , the directory b corresponding to the path /a/b must exist before the error.log file can be generated.
So , u just need to make sure that the directory where error.log is located exists.

Otherwise , use winston-daily-rotate-file module , it will auto generated directory .

duplicate #875

@adoyle-h
Copy link
Contributor

duplicate to #1465

@indexzero indexzero added the winston-file Issues to move to `winston-file` when we create it label Jan 29, 2019
@AdamCraven
Copy link

I'm not sure what's specifically causing it, but what caused this for me was I was running a synchronous script that has heavy synchronous read and writes, which also encountered file system errors:

"EISDIR: illegal operation on a directory, read"

When the error was there (the script carried on for a little while afterword), the script exited too fast for the logs to be flushed and the files were never written. Without the error, even though it was the exact same script, the logs are created.

Here are some documented ways to get around this: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md#winstonlogger

@Darkproduct
Copy link

Darkproduct commented Oct 21, 2021

I'm having the same issue with "winston": "^3.3.3":

const winston = require(`winston`);

const logger = winston.createLogger({
  level: winston.config.syslog.levels,
  format: winston.format.json(),
  transports: [
    //
    // - Write all logs with level `error` and below to `error.log`
    // - Write all logs with level `info` and below to `combined.log`
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log', level: 'info' }),
    new winston.transports.Console({ format: winston.format.simple(), colorize: true, level: 'info' })
  ]
})

logger.silly("silly!");
logger.debug("debug!");
logger.verbose("verbose!");
logger.http("http!");
logger.info("info!");
logger.warn("warn!");
logger.error("error!");

I get the expected console output from info, warn, and error but not colorized. But there are no log files in the root dir.
I tried so far:

  • Using ./error.log instead of error.log
  • Using a unique name and searching on my whole system if there is this file anywhere.

Edit:

Just fixed the console color output with:

// Console logging
new winston.transports.Console({
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.simple()
  ),
  colorize: true,
  level: 'info'
})

@Karl-maker
Copy link

I know this is a long time ago, but had this issue. However, I realized the file location wasn't being resolved. This might work.

filename: path.resolve(__dirname, "add_your_relative_path/error.log")

@mp3846
Copy link

mp3846 commented Jan 12, 2022

I have winston 3.3.3 installed and working properly in local both for logging in console and a file, but when I deploy on heroku it does not log anything neither in console nor file. I dont know if this issue is related to winston or heroku

@Darkproduct
Copy link

Darkproduct commented Jan 12, 2022 via email

@mp3846
Copy link

mp3846 commented Jan 12, 2022

This is probably a privilege thing, where the user/agent who executes the code, doesn't have file permissions to write the logs. I had this issue once where the log file whos created with root. So this log file was then also owned by root and coudn't be overwritten with normal user privileges.

I have winston 3.3.3 installed and working properly in local both for logging in console and a file, but when I deploy on heroku it does not log anything neither in console nor file. I dont know if this issue is related to winston or heroku

I'm not sure if this is a privilege thing. There is a paragraph in this page about this problem I think. some add-on for heroku called coralogix may work with winston:

Node.js Logging Libraries
Many developers will naturally gravitate toward an async logging library like Winston, Morgan, or Bunyan. The quintessential feature of Winston is its support for multiple transports which can be configured at various logging levels. However, problems with Winston include a lack of important details in its default log formatting. An example of missing details is log entry timestamps. Timestamps must be added via config. The lack of machine names and process IDs make it difficult to apply the next layer of third party smart log analytics apps. However, Heroku Add-Ons like Coralogix can easily work with Winston and Bunyan.

@mp3846
Copy link

mp3846 commented Jan 12, 2022

Actually it does log in heroku log console. Problem is still logging to a file (in local no problem)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
winston-file Issues to move to `winston-file` when we create it
Projects
None yet
Development

No branches or pull requests