moleculerjs/moleculer - v0.14.0-beta4

moleculerjs/moleculer - v0.14.0-beta4

Moleculer

Logger settings changed

The whole logging function has been rewritten in this version. It means, it has a lot of new features, but the configuration of loggers has contains breaking changes. You can't use some old custom logger configuration form. The new configuration same as the other Moleculer module configurations. This new version supports all famous loggers like Pino, Winston, Bunyan, Debug & Log4js.

If you are using the built-in default console logger, this breaking change doesn't affect you.

The logFormatter and logObjectPrinter broker options has been removed and moved into the Console and File logger options.

Not changed usable configurations

// moleculer.config.js
module.exports = {
// Enable console logger
logger: true,

// Disable all loggers
logger: false
};

You CANNOT use these legacy configurations

// moleculer.config.js
module.exports = {
// DON'T use a custom create function, like
logger: bindings => pino.child(bindings),

// DON'T use a custom logger, like
logger: {
error: () => { ... },
warn: () => { ... },
info: () => { ... },
debug: () => { ... }
}
};

Console logger

This logger prints all log messags to the console. It supports several built-in formatters or you can use your custom formatter, as well.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Console",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Console",
options: {
// Logging level
level: "info",
// Using colors on the output
colors: true,
// Print module names with different colors (like docker-compose for containers)
moduleColors: false,
// Line formatter. It can be "json", "short", "simple", "full", a `Function` or a template string like "{timestamp} {level} {nodeID}/{mod}: {msg}"
formatter: "full",
// Custom object printer. If not defined, it uses the `util.inspect` method.
objectPrinter: null,
// Auto-padding the module name in order to messages begin at the same column.
autoPadding: false
}
}
};

File logger

This logger saves all log messages to file(s). It supports JSON & formatted text files or you can use your custom formatter, as well.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "File",
};

It will save the log messages to the logs folder in the current directory with moleculer-{date}.log filename.

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "File",
options: {
// Logging level
level: "info",
// Folder path to save files. You can use {nodeID} & {namespace} variables.
folder: "./logs",
// Filename template. You can use {date}, {nodeID} & {namespace} variables.
filename: "moleculer-{date}.log",
// Line formatter. It can be "json", "short", "simple", "full", a `Function` or a template string like "{timestamp} {level} {nodeID}/{mod}: {msg}"
formatter: "json",
// Custom object printer. If not defined, it uses the `util.inspect` method.
objectPrinter: null,
// End of line. Default values comes from the OS settings.
eol: "\n",
// File appending interval in milliseconds.
interval: 1 * 1000
}
}
};

Pino logger

This logger uses the Pino logger.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Pino",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Pino",
options: {
// Logging level
level: "info",

pino: {
// More info: http://getpino.io/#/docs/api?id=options-object
options: null,

// More info: http://getpino.io/#/docs/api?id=destination-sonicboom-writablestream-string
destination: "/logs/moleculer.log",
}
}
}
};

To use this logger please install the pino module with npm install pino --save command.

Bunyan logger

This logger uses the Bunyan logger.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Bunyan",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Bunyan",
options: {
// Logging level
level: "info",

bunyan: {
// More settings: https://github.com/trentm/node-bunyan#constructor-api
name: "moleculer"
}
}
}
};

To use this logger please install the bunyan module with npm install bunyan --save command.

Winston logger

This logger uses the Winston logger.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Winston",
};

Full configuration

// moleculer.config.js
const winston = require("winston");

module.exports = {
logger: {
type: "Winston",
options: {
// Logging level
level: "info",

winston: {
// More settings: https://github.com/winstonjs/winston#creating-your-own-logger
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "/logs/moleculer.log" })
]
}
}
}
};

To use this logger please install the winston module with npm install winston --save command.

debug logger

This logger uses the debug logger.
To see messages you have to set the DEBUG environment variable to export DEBUG=moleculer:*.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Debug",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Debug",
options: {
// Logging level
level: "info",
}
}
};

To use this logger please install the debug module with npm install debug --save command.

Log4js logger

This logger uses the Log4js logger.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Log4js",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Log4js",
options: {
// Logging level
level: "info",

log4js: {
// More info: https://github.com/log4js-node/log4js-node#usage
appenders: {
app: { type: "file", filename: "/logs/moleculer.log" }
},
categories: {
default: { appenders: [ "app" ], level: "debug" }
}
}
}
}
};

To use this logger please install the log4js module with npm install log4js --save command.

Datadog logger

This logger uploads log messages to the Datadog server.

Please note, this logger doesn't print any messages to the console, just collects & uploads. Use it beside another logger which also prints the messages.

Shorthand configuration with default options

// moleculer.config.js
module.exports = {
logger: "Datadog",
};

Full configuration

// moleculer.config.js
module.exports = {
logger: {
type: "Datadog",
options: {
// Logging level
level: "info",

// Datadog server endpoint. https://docs.datadoghq.com/api/?lang=bash#send-logs-over-http
url: "https://http-intake.logs.datadoghq.com/v1/input/",
// Datadog API key
apiKey: process.env.DATADOG_API_KEY,
// Datadog source variable
ddSource: "moleculer",
// Datadog env variable
env: undefined,
// Datadog hostname variable
hostname: os.hostname(),
// Custom object printer function for `Object` & `Ąrray`
objectPrinter: null,
// Data uploading interval
interval: 10 * 1000
}
}
};

Multiple loggers

This new logger configuration admits to use multiple loggers even from the same logger type and different logging levels.

Define multiple loggers with different logging levels

This configuration demonstrates how you can define a Console logger, a File logger to save all log messages in formatted text file and another File logger to save only error messages in JSON format.

// moleculer.config.js
module.exports = {
logger: [
{
type: "Console",
options: {
level: "info",
}
},
{
type: "File",
options: {
level: "info",
folder: "/logs/moleculer",
filename: "all-{date}.log",
formatter: "{timestamp} {level} {nodeID}/{mod}: {msg}"
}
},
{
type: "File",
options: {
level: "error",
folder: "/logs/moleculer",
filename: "errors-{date}.json",
formatter: "json"
}
}
]
};

Using different loggers for different modules

This configuration demonstrates how you can define loggers for certain modules.

// moleculer.config.js
module.exports = {
logger: [
// Shorthand `Console` logger configuration
"Console",
{
// This logger saves messages from all modules except "greeter" service.
type: "File",
options: {
level: {
"GREETER": false,
"**": "info"
},
filename: "moleculer-{date}.log"
}
},
{
// This logger saves messages from only "greeter" service.
type: "File",
options: {
level: {
"GREETER": "debug",
"**": false
},
filename: "greeter-{date}.log"
}
}
],

logLevel: "info" // global log level. All loggers inherits it.
};

Logging level setting.

To configure logging levels, you can use the well-known logLevel broker option which can be a String or an Object. But it is possible to overwrite it in all logger options with the level property.

Complex logging level configuration

// moleculer.config.js
module.exports = {
logger: [
// The console logger will use the `logLevel` global setting.
"Console",
{
type: "File",
options: {
// Overwrite the global setting.
level: {
"GREETER": false,
"**": "warn"
}
}
}
],

logLevel: {
"TRACING": "trace",
"TRANS*": "warn",
"GREETER": "debug",
"**": "info",
}
};

Other Changes

  • change chalk to kleur

  • Fixed #580 (hot-reload with Runner)

  • Added timeout option to action schema.

  • Protobuf syntax with proto3

  • Using configurable serializer in Redis cacher.

  • Bulkhead for event handlers.

Report Page