2017-05-16 33 views

回答

0

一個簡單的方法是重寫你的函數,它記錄的數據也寫入文件系統控制檯上的log功能。

這裏的總體思路:

function attachFSLogger(filePath) { 
    // remember the old log method 
    const oldLog = console.log; // remove this line if you only want to log into the file 

    // override console.log 
    console.log = (...messages) => { 

    // log the console message immediately as usual 
    oldLog.apply(console, messages); // remove this line if you only want to log into the file 

    // asynchronously append to the file log 
    fs.appendFile(filePath, messages.join('\n'), (err) => { 
     if (err) throw err; 
    }); 
    } 
} 

attachFSLogger('./logs.txt'); 

console.log('test'); // logs 'test' and also appends 'test' to logs.txt 

如果您希望在經常登錄的事情,使用流會更efficient

function attachFSLogger(filePath) { 
    // remember the old log method 
    const oldLog = console.log; // remove this line if you only want to log into the file 

    // create a write stream for the given file path 
    const fsLog = fs.createWriteStream(filePath, { 
    flags: 'a' // 'a' means appending 
    }) 

    // override console.log 
    console.log = (...messages) => { 

    // log the console message immediately as usual 
    oldLog.apply(console, messages); // remove this line if you only want to log into the file 

    // stream message to the file log 
    fsLog.write(messages.join('\n')); 
    } 
} 

attachFSLogger('./logs.txt'); 

console.log('test'); // logs 'test' and also appends 'test' to logs.txt 

有了這樣說,有許多圖書館在那裏爲您做這樣的事情,並且已經過徹底的測試。

這裏有一對夫婦:

+0

'錯誤:EPERM:操作不允許,打開'C:\ Program Files \ nodejs \ logs.txt' at Error(native)'這就是我試過時返回的信息 – WebCodingFun

+0

這是一個權限問題。以管理員身份或在非限制文件夾中運行腳本。 – nem035

+0

謝謝!但它不會寫入新行......如何在每次記錄日誌時創建換行符? – WebCodingFun

0

對於nodejs庫,npm repo是你的朋友 - 你可以找到很多它們並嘗試最適合你的需求的東西。例如https://www.npmjs.com/package/log搜索的第一個結果log

你當然也可以實現自己的鉤到控制檯做得一樣好,用自己的函數是寫信給你選擇的文件,如覆蓋控制檯功能:

var fs = require('fs'); 

function myLog(file) { 
    return function() { 
    fs.appendFileSync(file || 'log.txt', arguments.join('\n')); 
    } 
} 

console.log = myLog(); 
console.warn = myLog(); 
console.error = myLog('errorLog.txt'); 
+0

所以不是'的console.log()'我會用'myLog()'?另外,是'console.log'的日誌,還是我只是覆蓋函數? – WebCodingFun

+0

我剛剛嘗試使用該代碼,並得到一個錯誤:'fs.appendFileSync(file |'log.txt',arguments.join('\ n'); ^ SyntaxError:missing)在參數列表**之後箭頭指向'arguments.join'函數的結尾括號** – WebCodingFun

+0

錯過了一個右括號 - 這只是一個大方向,儘管...現在更新 –

0

記錄儀.js文件

var fs = require('fs'); 
var log = []; 

//override default console.log behaviour 
console.log = function(d) { 
    process.stdout.write(d + '\n'); 
    log.push(d); //intercept every log and keep the value 
}; 

//log to file 
function log(filePath, clb){ 
    clb = clb || function(){}; //optional callback 
    fs.truncate(filePath, 0, function() { 
    fs.writeFile(filePath, string, function (err) { 
     clb(err) 
    }); 
    }); 
} 

module.exports = log; 

那麼你的代碼導入裏面的文件,並在需要時調用該函數。

var logger = require('./logger'); 
logger.log('log.txt', function(err){ 
if(err) 
    console.log(err); 
//else ok 
}); 
+0

返回:'錯誤:無法找到模塊'。/ logger'' – WebCodingFun

+0

您必須將第一塊代碼放入單獨的JavaScript文件中。我使用logger.js作爲名稱(需求正在查找該文件)。這就是你得到錯誤的原因。 – Karim

相關問題