Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is log file in nodejs app with winston

I can not find in my app directory where is 'test.log' file?

below code is in server.js

var winston = require('winston'), mylogger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console) (),
    new (winston.transports.File) ({filename: 'test.log'})
  ]
});

mylogger.log('Hello world');

my app directory:

/
  app/
  config/
  public/
  server.js
like image 865
user3044147 Avatar asked Nov 11 '22 16:11

user3044147


1 Answers

Interesting question. Looking at the source code for the file transport, it appears that the directory, if not specified, is derived from the filename parameter itself. It seems to suggest that you either use an absolute path or relative path with a directory explicitly specified.

This line is where it figures out the absolute path.

var fullname = path.join(self.dirname, target);

self.dirname is setup here:

this.dirname = options.dirname || path.dirname(options.filename);

so the question is that if options.filename does not include a directory, what does path.dirname return?

I do not actually know, but there are two possibilities I would suspect:

  • Current working directory of the process
  • The file system root, because if path.dirname takes what is left of the last /, then it is undefined and undefined + '/test.log' is '/test.log'

There are two steps you can take:

  • Check the current directory and file system root to see which it is. (in other words, test the theories)
  • Specify the directory explicitly (probably a good idea anyway)

https://github.com/flatiron/winston/blob/master/lib/winston/transports/file.js

like image 84
Brandon Avatar answered Nov 14 '22 22:11

Brandon