これは完璧な解決策ではないかもしれませんが、同様の状況にありました。私はアクティブであるかもしれないし、そうでないかもしれない異なったファイルを持っていて、アプリの異なった部分を管理しています。
私の解決策は、winstonに基づいて自分自身のロガーを作ることでした。
log.js:
各ソースコードファイルが1つの貴様ファイルにログを記録するロガー、および「共通」のファイルを必要としていたので、私は私が直接ウィンストンを必要とするのではなく、呼び出し「ジェネレータ」を作っ
'use strict';
const util = require('util'),
winston = require('winston'),
config = require('./config.json');
//If I don't want to log in files and only in console
let testMode = (config.log.mode === 'test');
//"Common" log file
const fileTransport = new (winston.transports.File)({
timestamp: true,
name: config.log.all.file,
filename: config.log.all.file,
level: config.log.all.level
}),
//"Common" error log file
errorTransport = new (winston.transports.File)({
timestamp: true,
name: config.log.error.file,
filename: config.log.error.file,
level: 'error'
});
//Logs are also sent in mongoDB, with the same schema as in the files
let mongoTransport = {},
mongoErrorTransport = {};
if(!testMode) {
//Add winston.transport.MongoDB
require('winston-mongodb');
mongoTransport = new (winston.transports.MongoDB)({
name: 'all',
host: config.log.db.host,
safe: config.log.db.safe,
collection: 'all',
level: config.log.all.level,
db: config.log.db.db
});
mongoErrorTransport = new (winston.transports.MongoDB)({
name: 'error',
host: config.log.db.host,
safe: config.log.db.safe,
collection: 'error',
level: 'error',
db: config.log.db.db
});
}
function getTransports(file) {
let transports = [];
//Log in the console
transports.push(new (winston.transports.Console)({
timestamp: true,
level: config.log.all.level,
formatter: (args) => {
let d = new Date();
return d.getFullYear() +
'/' + d.getMonth(), +
'/' + d.getDate(), +
' ' + d.getHours(), +
':' + d.getMinutes(), +
':' + d.getSeconds(), +
':' + d.getMilliseconds(), +
' - ' + file +
' - ' + args.level +
' -\t' + args.message +
'\t' + util.inspect(args.meta);
}
}));
if(testMode) {
return transports;
}
let name,
level,
filename;
transports.push(fileTransport);
transports.push(errorTransport);
transports.push(mongoTransport);
transports.push(mongoErrorTransport);
//Module specific logs
if(config.log[file] && config.log[file].file) {
name = config.log[file].file;
} else {
name = file;
}
if(config.log[file] && config.log[file].level) {
level = config.log[file].level;
} else if(config.log.default && config.log.default.level) {
level = config.log.default.level;
} else {
level = 'info';
}
if(config.log[file] && config.log[file].file) {
filename = config.log[file].file;
} else if(config.log.default && config.log.default.file) {
filename = config.log.default.path + file + '.log';
} else if(config.log.default && config.log.default.path) {
filename = config.log.default.file;
} else {
filename = './log/' + file + '.log';
}
//Module specific log file
transports.push(new (winston.transports.File)(
{
timestamp: true,
name: name,
level: level,
filename: filename
}
));
//Module specific Mongo collection for logs
transports.push(new (winston.transports.MongoDB)({
name: 'mongo' + file,
host: config.log.db.host,
safe: config.log.db.safe,
collection: file,
level: level,
db: config.log.db.db
}));
return transports;
}
//Generator
module.exports = (file) => {
let transports = getTransports(file);
return new (winston.Logger)({
rewriters: [
(level, msg, meta) => {
meta.app = file + '.js';
return meta;
}
],
transports: transports
});
};
は次のように呼ばれるように:それは完璧なソリューションにはほど遠いですが
'use strict';
const Logger = require('./log.js'),
logger = Logger('myModule');
logger.debug('Hi');
logger.error('Oops');
、および特定の問題には適用できない場合があり、同様のものが手動ですべてのロガーを作成するよりもきれいかもしれません。
私が知る限り、はい、あなたは別のロガーが必要です。あるいは、ヒッキーな解決策は、winston上で独自のロガーを構築し、可能なすべてのロガーを宣言し、さまざまなトランスポートに印刷するための便利な方法を作ることです。 – DrakaSAN