0
我正在嘗試使用slf4j api爲我的nodejs應用程序的外部文件中寫入日誌。但我無法找到相同的幫助/文檔。請幫忙。如何在nodejs中使用slf4j api保存外部文件中的日誌
我正在嘗試使用slf4j api爲我的nodejs應用程序的外部文件中寫入日誌。但我無法找到相同的幫助/文檔。請幫忙。如何在nodejs中使用slf4j api保存外部文件中的日誌
您可能會喜歡使用winston而不是其他日誌記錄,因爲winston仍處於開發和維護中的許多貢獻。您可以創建一個單獨的文件,如log.js
並使用日誌記錄配置。這裏是最低配置
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
的例子,然後在任何地方使用像其他模塊logger = require('./log');
或在您的出發點打電話,讓全球GLOBAL.logger = require('./log');
。現在,你可以在任何地方使用,而不需要像
logger.info('It is working')
我希望這會幫助你