nodejs console输出到文件

时间:2021-06-16 18:20:32   收藏:0   阅读:0
var fs      = require(‘fs‘)
var util    = require(‘util‘)

var logPath = ‘upgrade.log‘
var logFile = fs.createWriteStream(logPath, { flags: ‘a‘ })

console.log = function() {
  logFile.write(util.format.apply(null, arguments) + ‘\n‘)
  process.stdout.write(util.format.apply(null, arguments) + ‘\n‘)
}

console.error = function() {
  logFile.write(util.format.apply(null, arguments) + ‘\n‘)
  process.stderr.write(util.format.apply(null, arguments) + ‘\n‘)
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!