Pino 9.9.0 Bug: Logging Booleans In TypeScript

by Luna Greco 47 views

Hey everyone! It looks like we've hit a snag with the new Pino 9.9.0 release when it comes to logging boolean values in TypeScript. Let's dive into the issue, understand what's happening, and explore potential solutions.

Understanding the Issue

In the latest Pino version, specifically 9.9.0, there's a regression that's causing problems when you try to log boolean values using placeholders. If you're like me, you've probably used this pattern for ages:

const biteMe = true;
logger.debug("can I bite? %s", biteMe);

However, with Pino 9.9.0, TypeScript is throwing a compiler error:

error TS2345: Argument of type '[boolean]' is not assignable to parameter of type '[] | [string]'.

This is a big deal because it breaks existing code that relies on this common logging pattern. The error message essentially means that TypeScript is expecting either an empty array or an array containing a string, but it's receiving an array with a boolean instead. This type mismatch is what's triggering the error.

So, why is this happening? It seems the TypeScript definitions in Pino 9.9.0 have become stricter, and they're no longer implicitly allowing boolean values to be used as string placeholders. This is a regression, meaning it's a change in behavior from previous versions where this worked just fine. For many developers, this issue will stop your build process, if you have enabled noImplicitAny or strictNullChecks in your tsconfig.json file. These are common flags in modern TypeScript projects that help catch type-related errors early on.

Before version 9.9.0, Pino's TypeScript definitions were more lenient, automatically converting boolean values to their string representations when used with placeholders. This made logging straightforward and intuitive. You could simply pass a boolean value, and Pino would handle the conversion behind the scenes. However, the stricter type checking in 9.9.0 means that this implicit conversion is no longer happening, leading to the TypeScript error we're seeing.

It's crucial to address this issue because logging is a fundamental part of any application. It's how we track what's happening, debug problems, and monitor performance. When logging breaks, it can significantly impact our ability to maintain and troubleshoot our code. For example, imagine you're trying to debug a complex flow in your application. You rely on log messages to trace the execution path and identify potential issues. If you can't log boolean values, you're missing valuable information that could help you pinpoint the root cause of a bug.

The impact extends beyond simple debugging. Logging is also essential for auditing and security. You might need to log certain events or actions to comply with regulations or to track suspicious activity. If you can't reliably log boolean values, you might be missing critical security information. So, we need to find a way to work around this issue or, ideally, get it fixed in a future Pino release.

Possible Solutions and Workarounds

Okay, so we know what the problem is. What can we do about it? Here are a few potential solutions and workarounds:

1. Explicitly Convert Booleans to Strings

The most straightforward workaround is to explicitly convert your boolean values to strings before logging them. This ensures that you're passing a string value to the logger, which satisfies the TypeScript type checking. Here's how you can do it:

const biteMe = true;
logger.debug("can I bite? %s", String(biteMe));

In this example, we're using the String() constructor to convert the biteMe boolean value to its string representation (`