我想在nodejs
名为 logs 的目录中为我的项目设置日志
根据此处的文档
我在做 :
winston.add(winston.transports.File, { filename: 'logs/mylogs.log' });
但它什么也没做。
我如何达到同样的效果?
将以下代码放入您的服务器文件中。
var winston = require('winston');
var fs = require( 'fs' );
var path = require('path');
var logDir = 'log'; // directory path you want to set
if ( !fs.existsSync( logDir ) ) {
// Create the directory if it does not exist
fs.mkdirSync( logDir );
}
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
colorize: 'all'
}),
new (winston.transports.File)({filename: path.join(logDir, '/log.txt')})
]
});
logger.info("Anything you want to write in logfile");
您应该使用相对路径(点和反斜杠):
winston.add(winston.transports.File, { filename: './logs/mylogs.log' });