5

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
user3044147
  • 1,374
  • 5
  • 14
  • 23

2 Answers2

1

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

Brandon
  • 9,822
  • 3
  • 27
  • 37
  • I use search command in window, there is no result. I change to use {filename: '/test.log'}, I still doesn't appear in directory with server.js file – user3044147 Dec 13 '13 at 01:49
  • filename: __dirname + '/debug.log' } this way is still not work – user3044147 Dec 13 '13 at 02:02
  • Have you checked for any permissions issues? – Brandon Dec 13 '13 at 03:11
  • I do not know why it works with this way [http://stackoverflow.com/questions/18918896/in-winston-for-node-js-is-there-a-way-to-suppress-log-level-from-message?rq=1]. But the debug.log file just contain: **info: 127.0.0.1 - - [Fri, 13 Dec 2013 03:28:22 GMT] "GET /articles HTTP/1.1" 200 - "http://localhost/" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"** I want to log everything (include socketIO receiving and writing) likely STDIN and STDOUT, what should I do? – user3044147 Dec 13 '13 at 03:52
  • Use shell redirection when starting the node.js process like `node server.js >>stdout.log 2>&1`: http://www.tldp.org/LDP/abs/html/io-redirection.html – Brandon Dec 13 '13 at 11:43
0

I wrote a test file like this:

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

logger.log('info', 'Hello distributed log files!');
logger.info('Hello again distributed logs');

process.exit();

and I found that if I deleted the last line, the log file would be created and logged correctly.

I used process.exit() to break my program as I just wanted to test the logger, and it looked like it somehow broke the process on log file as well.

Check this issue: https://github.com/winstonjs/winston/issues/228

starshine wang
  • 616
  • 1
  • 8
  • 13