Utility tool used for logging in Falcon packages, but can also be used for logging in your custom Node.js apps.
pino library under the hood to work with log messages.
The package exports a singleton
Logger object that exposes logging and configuration methods. By default log level is set to
But if you do want to change it - you need to set a required log level
for the logger instance and the sooner you do this after the initial import statement the better:
Since you might be working with multiple apps - obviously you would like to be able to distinguish them apart in your logging system. In order to do that - there's a dedicated method for that:
This will simply add
"app": "my-app" data to every log message you send (not visible when using
so you'll be able to filter your log messages by this key:
Setting LogLevel and App for Falcon-based apps is done automatically using the provided config values
Falcon-Logger provides a handy
getFor method to initialize an extra
module key for log message. This way, you could easily
define sub-loggers for your nested modules, for example:
This call will add
"module": "my-module" data to every log entry you send via
subLogger. In conjunction with
logger-pretty - it will render an additional
[my-module] section in the log message output:
Another handy method is called
traceTime. This method can be used to calculate the time that your callback needs to complete the execution.
This method accepts 2 argument -
traceTime method will return the result of the execution of your callback. If your log level is set to
Logger will produce the following log message:
(10ms) may vary depending on your code. If log level is set higher than
trace - the calculations won't be performed, and the result will be returned right away.
The rest of the methods are available from the original Pino module.
@deity/falcon-logger package exposes a binary script called
logger-pretty that provides a basic formatter for log entries.
Best to use with Falcon-based apps in
logger-prettyscript reuses code of
Due to a nature of Pino logger - you can use this package as a part of pipelining (
| logger-pretty) in your
As a result, your console output should look similar to this:
This way, the formatting code offloads your application (which gives an extra performance boost) and handles it in a dedicated sub-process. It also gives you the ability to apply your own formatting without changing any internal code, you simply change the last part of the pipeline.
productionmode - you simply remove the last part of the pipeline and you will start seeing a raw JSON output:
Logger-Pretty on production
Even though your application is running in
production mode - it is still possible to use
logger-pretty to format those log messages
without a need to restart your application. All you need to do is to ensure
@deity/falcon-logger is installed
and then simply pass log entries to this script:
When using PM2:
There's an extra mode included into
logger-pretty - it's called
minimal. To enable it - you need to pass an extra flag
logger-pretty -m. Best to use with Falcon-based apps when running
as it does not show a log level nor date time information in the output:
To get more information about Logger transports - please refer to this page.