@goldix.org/loggers
v0.0.9
Published
Pool of loggers
Downloads
21
Readme
Install modules:
npm i --save @goldix.org/loggers @goldix.org/logger-winston-console @goldix.org/logger-winston-elastic
Example with multiple loggers:
const { Loggers } = require('@goldix.org/loggers');
const { LoggerWinstonConsole } = require('@goldix.org/logger-winston-console');
const { LoggerWinstonElastic } = require('@goldix.org/logger-winston-elastic');
Loggers.define('LoggerWinstonConsole', LoggerWinstonConsole);
Loggers.define('LoggerWinstonElastic', LoggerWinstonElastic);
const pools = {
default: {
LoggerWinstonElastic: {
enabled: false,
//@see https://github.com/vanthome/winston-elasticsearch
level: 'info',
indexPrefix: 'example',
clientOpts: {
//@see https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
host: 'http://localhost:9200'
}
},
LoggerWinstonConsole: {
enabled: true,
//@see https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport
level: 'info',
colorize: true,
timestamp: true,
prettyPrint: true
}
}
};
function transformMessage(level, message, payload, options) {
message = `${options && options.prefix} ${level} ${message}`;
if(options.someParameter === 1 && options.loggerClass === 'LoggerWinstonElastic') {
//Disable write this message to elastic
options.skip = true;
}
if(options.options.someParameter2 === true) {
payload.memory = process.memoryUsage();
}
return {
level,
message,
payload,
options
}
}
const loggers = new Loggers({ pools, transformMessage });
const dbLogger = loggers.get('default', { prefix: 'DB', someParameter: 1 })
dbLogger.debug('Users count 12', null, { someParameter2: true });
const httpLogger = loggers.get('default', { transformMessage: (level, message, payload, options) => {
let req = payload.req;
if(req) {
payload.ip = req.ip;
delete payload.req;
if(req.originalUrl === '/favicon.ico') {
options.skip = true;
}
}
return {
level,
message: `HTTP ${req.method} ${req.originalUrl}`,
payload,
options
}
})
app.use((req, res, next) => {
next();
})