In Moleculer framework all core modules have a custom logger instance. They are inherited from the broker logger instance which can be configured in the broker options.
Built-in logger
The Moleculer has a built-in console logger. It is the default logger.
Built-in console logger:
const { ServiceBroker } = require("moleculer"); |
Console messages:
[2018-06-26T11:38:06.728Z] INFO node-100/POSTS: Log message via Service logger |
Custom log levels
Log level can be changed with logLevel
option in broker options. Use it with built-in console logger only.
const broker = new ServiceBroker({ |
Available log levels:
fatal
,error
,warn
,info
,debug
,trace
Fine-tuned log levels
The log level can be set for every Moleculer module. Wildcard usage is allowed.
const broker = new ServiceBroker({ |
This settings are evaluated from top to bottom, so the
**
level must be the last item.
Internal modules:
BROKER
,TRANS
,TX
as transporter,CACHER
,REGISTRY
.
For services, the name comes from the service name. E.g. POSTS. If version is defined it is used as prefix. E.g. V2.POSTS
Log formats
There are some built-in log formatter.
Name | Output |
---|---|
default |
[2018-06-26T13:36:05.761Z] INFO node-100/BROKER: Message |
simple |
INFO - Message |
short |
[13:36:30.968Z] INFO BROKER: Message |
Custom log formatter
Custom log formatter function can be set for the built-in console logger.
const broker = new ServiceBroker({ |
Output:
WARN dev-pc: Warn message |
Custom object & array printing formatter
Set a custom formatter function to print object & arrays. The default function prints the objects & arrays to a single line in order to be easy to process with an external log tool. But when you are developing, it would be useful to print objects to a human-readable multi-line format. For this purpose, overwrite the logObjectPrinter
function in the broker options.
Output with default function
[2017-08-18T12:37:25.720Z] INFO dev-pc/BROKER: { name: 'node', lts: 'Carbon', sourceUrl: 'https://nodejs.org/download/release/v8.10.0/node-v8.10.0.tar.gz', headersUrl: 'https://nodejs.org/download/release/v8.10.0/node-v8.10.0-headers.tar.gz' } |
Switch to multi-line printing & increment depth
const util = require("util"); |
Output:
[2017-08-18T12:37:25.720Z] INFO dev-pc/BROKER: { name: 'node', |
External loggers
External loggers can be used with Moleculer. In this case, set a creator function to logger
. The ServiceBroker will call it when a new module inherits a new logger instance.
Pino
const pino = require("pino")({ level: "info" }); |
Bunyan
const bunyan = require("bunyan"); |
Winston
const winston = require("winston"); |
Some external loggers have not
trace
&fatal
log methods (e.g.: winston). In this case you have to extend your logger.
Bindings
The bindings
object contains the following properties:
ns
- namespacenodeID
- nodeIDmod
- type of core module:broker
,cacher
,transit
,transporter
svc
- service namever
- service version
Please noteAvoid using these bindings property names when you log an
Object
.
For example: thebroker.logger.error({ mod: "peanut" })
overrides the originalmod
value!