Tip: Use explicit log statements Jan 16

Logging is a critical practice in software development, providing insights into the application’s behavior, helping diagnose issues, and facilitating debugging. However, the effectiveness of logging is significantly influenced by how log statements are written. It’s essential to make log statements explicit and contextual, which not only enhances the readability and maintainability of the code but also aids in troubleshooting.

Why Explicit Log Statements Matter

When you include context in your log statements, it becomes much easier to trace issues back to their source. This is because explicit log statements act as breadcrumbs, guiding developers through the execution flow of the application. By embedding meaningful context, such as function names, variable states, or specific operations being performed, developers can quickly identify where a problem might have occurred.

Benefits of Contextual Logging:

  1. Enhanced Searchability: With explicit log statements, you can search your codebase more effectively. For instance, if a log entry contains specific identifiers like function names or user IDs, you can quickly locate the relevant code sections by searching for these terms.

  2. Improved Debugging: When an issue arises, clear and detailed log messages provide valuable insights into what the application was doing at any given time. This can significantly reduce the time spent diagnosing problems.

  3. Better Collaboration: Explicit logs help team members understand the code quickly, making it easier for new developers to get up to speed or for external auditors to assess the code.

  4. Efficient Monitoring: In production environments, explicit logs allow for better monitoring and alerting. They can be used to trigger alerts when specific conditions are met, thereby improving the system’s reliability and responsiveness.

How to Implement Explicit Log Statements

To achieve effective logging, consider configuring your logger to include context automatically or ensure each log statement is self-explanatory. Here’s how you can improve your logging practices:

Avoid Generic Log Statements

Generic log statements lack context and clarity, making them difficult to interpret. For example:

function saveUser(user) {
  console.log(user); // Avoid at all cost
  console.log('got', user); // Using 'got' is too generic
}

The above examples are problematic because they provide minimal information about what the log entry refers to or where it was generated.

Use Explicit and Contextual Log Statements

Instead, aim for log statements that clearly describe the action being taken and the context in which it occurs:

function saveUser(user) {
  logger.debug(`UsersRepository.saveUser(user: ${user?.id})`); // Easy to search for
  logger.debug(`Preparing to save user ${user?.id} in the database`); // Clear and descriptive
}

In these examples, the log messages are not only descriptive but also include specific details such as the function name and user ID. This makes them much more valuable for anyone trying to understand the code’s behavior or diagnose an issue.


By adopting a practice of writing explicit and context-rich log statements, you significantly enhance the maintainability and operability of your software. This approach not only aids in debugging and monitoring but also fosters a more collaborative and efficient development environment. So, make it a habit to use explicit log statements and empower your team with the clarity and insights they provide.