我的代码:
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,无法打开文件。错误:ENOENT,stat './path/to/file'
问题是什么?