Skip to content

Commit

Permalink
add bunyan & debug
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Aug 31, 2019
1 parent 7ffc282 commit 131c8db
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {
],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "2017"
"ecmaVersion": "2018"
},
"plugins": [
"node",
Expand Down
45 changes: 33 additions & 12 deletions dev/loglevel.js
Expand Up @@ -2,43 +2,64 @@

const ServiceBroker = require("../src/service-broker");
const { extend } = require("../src/logger");
const winston = require("winston");

const broker = new ServiceBroker({
logger: [
/*{
{
type: "Console",
options: {
//level: "error",
//formatter: (type, args, bindings) => [].concat(args, bindings)
//formatter: "simple",
moduleColors: true
moduleColors: true,
autoPadding: true
}
},*/
},
/*{
type: "Pino",
options: {
options: {
base: null
pino: {
options: {
base: null
},
//destination: "d:/pino.log"
}
//destination: "d:/pino.log"
}
},*/
{
/*{
type: "Bunyan",
options: {
bunyan: {
name: "my-app"
}
}
}
},*/
/*{
type: "Winston",
options: {
winston: {
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "d:/winston.log" })
]
}
}
},*/
/*{
type: "Debug",
options: {
}
}*/
],
/*logLevel: {
"MY.**": false,
logLevel: {
//"MY.**": false,
"TRANS*": "warn",
"*.GREETER": "debug",
"**": "debug",
},*/
logLevel: "info",
},
//logLevel: "info",
//logFormatter: "short",
transporter: "NATS",
cacher: "Memory"
Expand Down
27 changes: 23 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
"bunyan": "1.8.12",
"coveralls": "3.0.6",
"dd-trace": "0.14.0",
"debug": "4.1.1",
"dotenv": "8.1.0",
"eslint": "6.2.2",
"eslint-plugin-node": "9.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/logger.js
Expand Up @@ -103,6 +103,8 @@ class LogFactory {
logHandlers.forEach(fn => fn(type, args));
};*/

logger.appenders = appenders;


this.cache.set(this.getBindingsKey(bindings), logger);

Expand Down
3 changes: 2 additions & 1 deletion src/loggers/bunyan.js
Expand Up @@ -34,6 +34,7 @@ class BunyanLogger extends BaseLogger {
createLogger: null
});
}

/**
* Initialize logger.
*
Expand All @@ -57,7 +58,7 @@ class BunyanLogger extends BaseLogger {
getLogHandler(bindings) {
let level = this.getLogLevel(bindings ? bindings.mod : null);
if (!level)
level = "silent";
return null;

const logger = _.isFunction(this.opts.createLogger) ? this.opts.createLogger(level, bindings) : this.bunyan.child({ level, ...bindings });

Expand Down
38 changes: 26 additions & 12 deletions src/loggers/console.js
Expand Up @@ -46,7 +46,8 @@ class ConsoleLogger extends BaseLogger {
colors: true,
moduleColors: false,
formatter: null,
objectPrinter: null
objectPrinter: null,
autoPadding: false
});
}

Expand All @@ -65,18 +66,30 @@ class ConsoleLogger extends BaseLogger {
}, {});

if (this.opts.colors && this.opts.moduleColors === true) {
this.opts.moduleColors = ["cyan", "yellow", "green", "magenta", "red", "blue", "white", "grey",
"bold.cyan", "bold.yellow", "bold.green", "bold.magenta", "bold.red", "bold.blue", "bold.white", "bold.grey"];
this.opts.moduleColors = ["cyan", "yellow", "green", "magenta", "red", "blue", "grey", /*"white,"*/
"bold.cyan", "bold.yellow", "bold.green", "bold.magenta", "bold.red", "bold.blue", "bold.grey"];
}
this.colorCnt = 0;
}

/**
*
* Get a color for the module name.
*/
getNextColor() {
if (this.opts.colors && Array.isArray(this.opts.moduleColors))
return this.opts.moduleColors[this.colorCnt++ % this.opts.moduleColors.length];
getNextColor(mod) {
if (this.opts.colors && Array.isArray(this.opts.moduleColors)) {
// Credits: "visionmedia/debug" https://github.com/visionmedia/debug/blob/master/src/common.js#L45
let hash = 0;

for (let i = 0; i < mod.length; i++) {
hash = ((hash << 5) - hash) + mod.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}

return this.opts.moduleColors[Math.abs(hash) % this.opts.moduleColors.length];


//return this.opts.moduleColors[this.colorCnt++ % this.opts.moduleColors.length];
}

return "grey";
}
Expand All @@ -90,9 +103,8 @@ class ConsoleLogger extends BaseLogger {
if (_.isFunction(formatter))
return (type, args) => formatter(type, args, bindings);

const c = this.getNextColor();

const mod = (bindings && bindings.mod) ? bindings.mod.toUpperCase() : "";
const c = this.getNextColor(mod);
const modColorName = c.split(".").reduce((a,b) => a[b] || a()[b], kleur)(mod);
const moduleColorName = bindings ? kleur.grey(bindings.nodeID + "/") + modColorName : "";

Expand Down Expand Up @@ -121,10 +133,12 @@ class ConsoleLogger extends BaseLogger {
* @param {object} bindings
*/
getLogHandler(bindings) {
const formatter = this.getFormatter(bindings);

const level = this.getLogLevel(bindings ? bindings.mod : null);
const levelIdx = level ? BaseLogger.LEVELS.indexOf(level) : -1;
if (!level)
return null;

const levelIdx = BaseLogger.LEVELS.indexOf(level);
const formatter = this.getFormatter(bindings);

return (type, args) => {
const typeIdx = BaseLogger.LEVELS.indexOf(type);
Expand Down
41 changes: 40 additions & 1 deletion src/loggers/debug.js
Expand Up @@ -27,9 +27,48 @@ class DebugLogger extends BaseLogger {
constructor(opts) {
super(opts);

this.opts = _.defaultsDeep(this.opts, {});
this.opts = _.defaultsDeep(this.opts, {
createLogger: null
});
}

/**
* Initialize logger.
*
* @param {LogFactory} logFactory
*/
init(logFactory) {
super.init(logFactory);

try {
this.debug = require("debug")("moleculer");
} catch(err) {
/* istanbul ignore next */
this.broker.fatal("The 'debug' package is missing! Please install it with 'npm install debug --save' command!", err, true);
}
}

/**
*
* @param {object} bindings
*/
getLogHandler(bindings) {
const mod = bindings ? bindings.mod : null;
const level = this.getLogLevel(mod);
if (!level)
return null;

const levelIdx = BaseLogger.LEVELS.indexOf(level);

const logger = _.isFunction(this.opts.createLogger) ? this.opts.createLogger(level, bindings) : this.debug.extend(mod);

return (type, args) => {
const typeIdx = BaseLogger.LEVELS.indexOf(type);
if (typeIdx > levelIdx) return;

return logger(...args);
};
}
}

module.exports = DebugLogger;
1 change: 1 addition & 0 deletions src/loggers/index.js
Expand Up @@ -16,6 +16,7 @@ const Loggers = {
Console: require("./console"),
Debug: require("./debug"),
File: require("./file"),
Log4js: require("./log4js"),
Pino: require("./pino"),
Winston: require("./winston"),

Expand Down
16 changes: 10 additions & 6 deletions src/loggers/pino.js
Expand Up @@ -28,10 +28,12 @@ class PinoLogger extends BaseLogger {
super(opts);

this.opts = _.defaultsDeep(this.opts, {
// http://getpino.io/#/docs/api?id=options-object
options: null,
// http://getpino.io/#/docs/api?id=destination-sonicboom-writablestream-string
destination: null,
pino: {
// http://getpino.io/#/docs/api?id=options-object
options: null,
// http://getpino.io/#/docs/api?id=destination-sonicboom-writablestream-string
destination: null,
},

createLogger: null
});
Expand All @@ -47,7 +49,9 @@ class PinoLogger extends BaseLogger {

try {
const Pino = require("pino");
this.pino = Pino(this.opts.options ? this.opts.options : undefined, this.opts.destination ? this.opts.destination : null);
this.pino = Pino(
this.opts.pino && this.opts.pino.options ? this.opts.pino.options : undefined,
this.opts.pino && this.opts.pino.destination ? this.opts.pino.destination : null);
} catch(err) {
/* istanbul ignore next */
this.broker.fatal("The 'pino' package is missing! Please install it with 'npm install pino --save' command!", err, true);
Expand All @@ -61,7 +65,7 @@ class PinoLogger extends BaseLogger {
getLogHandler(bindings) {
let level = this.getLogLevel(bindings ? bindings.mod : null);
if (!level)
level = "silent";
return null;

const logger = _.isFunction(this.opts.createLogger) ? this.opts.createLogger(level, bindings) : this.pino.child({ level, ...bindings });

Expand Down
56 changes: 55 additions & 1 deletion src/loggers/winston.js
Expand Up @@ -27,9 +27,63 @@ class WinstonLogger extends BaseLogger {
constructor(opts) {
super(opts);

this.opts = _.defaultsDeep(this.opts, {});
this.opts = _.defaultsDeep(this.opts, {
winston: {
level: "silly",
},
createLogger: null
});
}

/**
* Initialize logger.
*
* @param {LogFactory} logFactory
*/
init(logFactory) {
super.init(logFactory);

try {
this.winston = require("winston").createLogger(this.opts.winston);
} catch(err) {
/* istanbul ignore next */
this.broker.fatal("The 'winston' package is missing! Please install it with 'npm install winston --save' command!", err, true);
}
}

/**
*
* @param {object} bindings
*/
getLogHandler(bindings) {
let level = this.getLogLevel(bindings ? bindings.mod : null);
if (!level)
return null;

const levelIdx = BaseLogger.LEVELS.indexOf(level);

const logger = _.isFunction(this.opts.createLogger) ? this.opts.createLogger(level, bindings) : this.winston.child({ level, ...bindings });

return (type, args) => {
const typeIdx = BaseLogger.LEVELS.indexOf(type);
if (typeIdx > levelIdx) return;

switch(type) {
case "info": return logger.info(...args);
case "fatal":
case "error": return logger.error(...args);
case "warn": return logger.warn(...args);
case "debug": return logger.debug(...args);
case "trace": return logger.log("silly", ...args);
default: {
if (logger[type])
return logger[type](...args);

return logger.info(...args);
}
}
};
}
}

module.exports = WinstonLogger;

0 comments on commit 131c8db

Please sign in to comment.