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");
3条答案
按热度按时间du7egjpx1#
将下面的代码放入服务器文件中。
2ul0zpep2#
Winston提供了在创建日志文件时设置dirname的选项。
默认情况下,Winston创建项目根目录:
例如,如果您希望日志位于src目录中:
完整代码示例:
nfs0ujit3#
应使用相对路径**(点和反斜杠)**: