Guidelines
Logging

Logging

Use a standardized logging library

A standardized logging library is a library or tool that is widely used and accepted within the software development community as a way to log events and messages within an application.

Using a standardized logging library can help to improve the quality and consistency of the logs generated by your code, and can make it easier to understand and troubleshoot issues that may arise. A standardized logging library can also provide additional features and functionality, such as the ability to customize the format of the logs or to integrate with external logging systems. By using a standardized logging library, you can ensure that your logs are consistent, reliable, and easy to work with.

Here are a few examples of standardized logging libraries that are commonly used on Node.js projects:

  • Winston (opens in a new tab): Winston is a popular logging library for Node.js that is designed to be simple and easy to use. It supports multiple transports (e.g., console, file, HTTP) and allows you to customize the format of log messages.
  • Bunyan (opens in a new tab): Bunyan is another popular logging library for Node.js that is designed to be fast and easy to use. It supports multiple transports and allows you to customize the format of log messages.
  • Log4js (opens in a new tab): Log4js is a logging library that is based on the popular log4j library for Java. It supports multiple transports and allows you to customize the format of log messages.

Log at appropriate levels

Logging levels are a way to classify the severity of log messages. Different log levels are typically used to indicate the importance or severity of the message being logged. The most common log levels are:

  • Debug: Debug level messages are used for detailed debugging information. These messages are typically only useful when diagnosing problems and are not typically needed in production environments.
  • Info: Info level messages are used to provide general information about the operation of the application. These messages might include details about the startup and shutdown of the application, as well as other important events.
  • Warning: Warning level messages are used to indicate potential problems or issues that may arise. These messages might include issues that are not necessarily causing problems at the moment, but could become issues in the future.
  • Error: Error level messages are used to indicate a serious problem or failure. These messages might include details about exceptions or other errors that have occurred.

By using different log levels, you can filter log messages based on their importance or severity. This can make it easier to identify and prioritize issues that need to be addressed.

Include sufficient context in logs

Sufficient context refers to the amount of information included in a log message that is needed to understand the context of the event being logged. This might include details about the function or method that generated the log message, the input values, the call stack, and any relevant state information.

By including enough context in the logs, you can provide the necessary information to help debug and diagnose problems, and can make it easier to identify and fix issues quickly.

Use structured logging

Structured logging is a way of logging events and messages in a structured format, rather than as free-form strings. With structured logging, each log message is a structured object (e.g., a dictionary or JSON object) with well-defined fields. This can make it easier to parse and analyze log messages, as the structure of the log message is consistent and well-defined.

To implement structured logging, you can use a standardized logging library that supports structured logging, or you can convert your log messages to a structured format manually.

Rotate logs regularly

Log rotation is the process of regularly archiving or deleting older log files to prevent them from consuming too much disk space. Rotating logs regularly can help to keep the size of the logs manageable and can improve the performance and efficiency of your code, as well as making it easier to work with and analyze the logs.

Many standardized logging library include support for log rotation as a built-in feature. For example, the Winston logging library includes a Rotate-Stream transport that can be used to rotate log files based on size or time.

Monitor logs

Log monitoring is the process of regularly checking log files for unusual or unexpected events. Monitoring logs can help to identify issues and problems with your code in real-time, and can allow you to take proactive measures to address those issues before they become serious problems.

There are many tools and services available for log monitoring, ranging from simple scripts that check logs for specific keywords or patterns to more sophisticated platforms that provide advanced analysis and visualization capabilities.

Use centralized logging

Centralized logging is the practice of collecting and storing log messages from multiple sources in a central location. This can help improve security by providing a single, centralized location for storing and protecting log data. It can also make it easier to analyze and understand what is happening across an entire system.

Use requestId

A requestId (or request ID) is a unique identifier that is generated for each incoming request to an application or service.

RequestIds are useful because they provide a way to uniquely identify each incoming request to an application or service, which can be helpful for tracking and debugging issues, as well as for auditing and monitoring the performance and behavior of the system.

Avoid client-side console logs in production

Avoid client-side console logs in production, as they can impact the performance, security, and user experience of the application. Instead, it's usually better to use a dedicated error tracking platform or service to log and monitor client-side errors.

Track errors that occur on the client-side

Client-side errors can be caused by a variety of factors, such as bugs in the client-side code, issues with the user's web browser or operating system, or problems with the client-side dependencies of the application (e.g., libraries or frameworks).

It's important to track client-side errors because if they are not tracked, there is no way for the development team to know that the error occurred and take action. Ignoring client-side errors can lead to a poor user experience and can affect the reputation of the application.

Last updated on