Use a mature logger to increase error visibility
One Paragraph Explainer
We love console.log but a reputable and persistent logger like Pino (a newer option focused on performance) is mandatory for serious projects. High-performance logging tools help identify errors and possible issues. Logging recommendations include:
- Log frequently using different levels (debug, info, error).
- When logging, provide contextual information as JSON objects.
- Monitor and filter logs with a log querying API (built-in to many loggers) or log viewer software.
- Expose and curate log statements with operational intelligence tools such as Splunk.
Code Example
const pino = require('pino');
// your centralized logger object
const logger = pino();
// custom code somewhere using the logger
logger.info({ anything: 'This is metadata' }, 'Test Log Message with some parameter %s', 'some parameter');
Blog Quote: “Logger Requirements”
From the StrongLoop blog (“Comparing Winston and Bunyan Node.js Logging” by Alex Corbatchev, Jun 24, 2014):
Let’s identify a few requirements (for a logger):
- Timestamp each log line. This one is pretty self-explanatory – you should be able to tell when each log entry occurred.
- Logging format should be easily digestible by humans as well as machines.
- Allows for multiple configurable destination streams. For example, you might be writing trace logs to one file but when an error is encountered, write to the same file, then into error file and send an email at the same time.
Where’s Winston?
For more information on why traditional favorites (e.g., Winston) may not be included in the current list of recommended best practices, please see #684.
当前内容版权归 goldbergyoni 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 goldbergyoni .