All Moleculer’s core modules have a custom logger instance. They are inherited from the broker logger instance which can be configured in the broker options.
The
v0.14version contains breaking changes. This means that you can’t use the old way of configuring the logger. If you are using the built-in default console logger, this breaking change doesn’t affect you. For more info check the Migration Guide.
Built-in Loggers
Console
This logger prints all log messages 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",
};
// moleculer.config.js
module.exports = {
// Enable console logger
logger: true,
};
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,
// Set formatter. It can be "json", "short", "simple", "full" or a custom `Function`
formatter: null,
// 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
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",
// Formatter. It can be "json" or a format 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
}
}
};
External Loggers
Pino
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
pinomodule withnpm install pino --savecommand.
Bunyan
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
bunyanmodule withnpm install bunyan --savecommand.
Winston
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
winstonmodule withnpm install winston --savecommand.
debug
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
debugmodule withnpm install debug --savecommand.
Log4js
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
log4jsmodule withnpm install log4js --savecommand.
Multiple Loggers
This new logger configuration admits usage of multiple loggers even from the same logger type and different logging levels.
Define multiple loggers with different logging levels// 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"
}
}
]
};
This example shows a configuration of Console logger, a File logger that saves all log messages in formatted text file and another File logger that only saves error messages in JSON format.
Filtering
You can configure your loggers to only log data of certain services or modules.
Example// 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.
};
Log Level Setting
To configure logging levels, you can use the well-known logLevel broker option which can be a String or an Object. However, it is also 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",
}
};