How we use pino in our NodeJS apps for a better logging experience
What is Structured Logging
Treating logs as data gives us greater insight into the operational activity of the systems we build. Structured logging, which is using a consistent, predetermined message format containing semantic information, builds on this technique and enables tools such as Graylog2 and Splunk to yield deeper insights — From ThoughtWorks Technology Radar 👉 link
There are many npm packages offering similar functionality. We chose to use pino in most of our NodeJS apps.
The Why
If you are using Structured Logging and CloudWatch Insights, you will get the following…
Easy to Filter
Logs can be filtered by level, node id, keywords, etc…
Easy to Aggregated
Logs can be aggregated by the number of errors per 5 minutes
Easy to Post Processing
Logs can be processed to create alarms, reports, trends, etc
Easy to Transform
You can transform the standard JSON logline to a human-friendly format by using pino-pretty
Some Tips
1. Inject log level
Inject the log level via an environment variable to ensure it can be controlled when the app starts.
2. Avoid too granular log levels
A handful of levels should to be enough for common cases: error
warn
info
debug
3. Consistent log levels
Set up a convention for your team on what does each log level means, and what log you should put in there, etc
4. Inject logger as dependencies
In a GraphQL project, you should probably consider injecting a logger
to ApolloContext, so that you can easily access it in code.
5. Prettify logs on your dev environment
Pipe to pino-pretty
in your npm script, start.js | pino-pretty
. It changes a standard JSON logline to something easy for a human. 👉 pino-pretty
6. Pipe logs to a local file
This is a general tip for piping logs into a file: npm run dev | tee dev.log
, print logs to the terminal and write to a local file. 👉 tee command
Gotchas
pino-pretty
is a peer dependency, it should and will be shaken off while bundling.- Ensure redact sensitive information. Never log PII, token, keys, etc