txt文件中的Node.js console.log() [英] Node.js console.log() in txt file
问题描述
我还有另一个问题(最后一个问题).目前,我正在开发一个Node.js项目,并且其中有许多 console.log()函数.到目前为止,这还行得通,但我也希望将写入控制台的所有内容 也写入日志文件中.有人可以帮帮我吗?
I have another question (last question). At the moment i am working on a Node.js project and in this I have many console.log() functions. This has worked okay so far but I also want everything that's written to the console to also be written in a log-file. Can someone please help me?
例如:
Console.log('The value of array position [5] is '+ array[5]);
在我的真实代码中,它的作用还多一些,但这应该可以使您有所了解.
In my real code its a bit more but this should give you an idea.
希望您能感谢您.
推荐答案
我会使用一个库而不是重新发明轮子.我在npm上寻找了log4j
类型的库,它带有 https://github.com/nomiddlename/log4js-node
I would use a library instead of re-inventing the wheel. I looked for a log4j
-type library on npm, and it came up with https://github.com/nomiddlename/log4js-node
如果要登录到控制台和文件:
if you want to log to the console and to a file:
var log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' },
{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
]
});
现在您的代码可以使用创建新的记录器了
now your code can create a new logger with
var logger = log4js.getLogger('cheese');
并在代码中使用记录器
logger.warn('Cheese is quite smelly.');
logger.info('Cheese is Gouda.');
logger.debug('Cheese is not a food.');
这篇关于txt文件中的Node.js console.log()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!