![](/img/trans.png)
[英]How can I get the full object in Node.js's console.log(), rather than '[Object]'?
[英]Node's console.log() outputs an object's info. How do I output it to a file?
我喜歡console.log(object)
如何以json格式輸出object
的結構。 如何使我的應用將相同的內容輸出到文件?
正如Golo所說,Node沒有內置的功能,但是您可以輕松編寫自己的(或使用Winston):)
fs = require('fs');
logToFile = function(fileName, objectToLog) {
jsonText = JSON.stringify(objectToLog, null, '\t');
fs.writeFileSync(fileName, jsonText, 'utf8');
}
sampleData = { name: 'Batman', city: 'Gotham' };
logToFile('log.txt', sampleData);
Node.js中沒有開箱即用的文件記錄支持。
基本上,您有兩種選擇:
您可以使用操作系統的機制來重定向流,從而將Node.js進程的任何輸出重定向到文件。
使用專用的日志記錄庫,例如Winston 。
我會選擇第二個選項,因為它是更靈活的選項,您遲早需要它,至少在您的項目變得更大的情況下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.