2017-07-17 43 views
0

我的代码:温斯顿记录抛出ENOENT

import winston from 'winston'; 

Meteor.startup(() => { 
    const env = process.env.NODE_ENV || 'development'; 
    const tsFormat =() => (new Date()).toLocaleTimeString(); 
    const logDir = 'log'; 
    const logger1 = new (winston.Logger)({ 
    transports: [ 
     // colorize the output to the console 
     new (winston.transports.Console)({ 
     timestamp: tsFormat, 
     colorize: true, 
     level: 'info', 
     }), 
     new (winston.transports.File)({ 
     filename: `${logDir}/results.log`, 
     timestamp: tsFormat, 
     level: env === 'development' ? 'debug' : 'info', 
     }), 
    ], 
    }); 
    logger1.info('Hello world'); 
    //logger1.warn('Warning message'); 
    //logger1.debug('Debugging info'); 
}); 

输出:

I20170717-11:39:11.027(2)? 11:39:10 - info: Hello world 
W20170717-11:39:11.150(2)? (STDERR) 
W20170717-11:39:11.151(2)? (STDERR) events.js:72 
W20170717-11:39:11.152(2)? (STDERR)   throw er; // Unhandled 'error' event 
W20170717-11:39:11.152(2)? (STDERR)    ^
W20170717-11:39:11.153(2)? (STDERR) Error: ENOENT, open 'log/results.log' 

的results.log甚至没有创建

更新:当我使用了一个文件名没有路径然后它工作)。

相关,但没有帮助解决:
Node.js, can't open files. Error: ENOENT, stat './path/to/file'

问题是什么?

+1

尝试检查目录chmod,或手动创建文件等。有什么改变吗? –

+0

'log'目录是否存在? – robertklep

+0

@robertklep nope – Gobliins

回答

0

Winston实际上并没有创建你想要日志文件去的目录,这就是为什么你得到ENOENT

(我不是很熟悉,流星,但下面的说明,“普通” Node.js的工作)

您可以实例温斯顿以确保它的存在,使用前fs.mkdirSync手动创建目录,虽然这只会深入一层(如果路径中有任何内容,它不会创建中间目录)。

还有mkdirp.sync(),其中确实创建中间目录。

使用同步版本更容易一些,而且由于这是一种在应用程序启动期间只发生一次的操作,因此它不是瓶颈。