dahlia / logtape

Simple logging library with zero dependencies for Deno, Node.js, Bun, browsers, and edge functions
http://logtape.org/
MIT License
546 stars 11 forks source link

Whitespace trimming in template literals #16

Closed kachkaev closed 2 months ago

kachkaev commented 2 months ago

👋 @dahlia! Just started playing with logtape – awesome architecture!

I faced a small issue with template literals and decided to share. Technically, there is no bug, it's just a small DX trap that could be avoided.

It turned out that {foo} and { foo } were not the same thing, which caught me:

logger.info('{ hello }', { hello: 'HELLO' });
// Expected: HH:MM:SS.xxx INF 'HELLO'
// Received: HH:MM:SS.xxx INF undefined
logger.info('{ hello }', { ' hello ': 'HELLO' });
// HH:MM:SS.xxx INF 'HELLO'

Because I use Prettier and it puts whitespace around { } in objects, I uncontentiously did the same in template literals. It took a bit of time to figure out what was going on.

Here is an idea: if we detect dev mode and there is a key mismatch like ' hello ' / 'hello', logtape could log a warning and explaining the situation. Or maybe it could even ‘forgive’ spaces around braces. This could help folks with onboarding, because every new team member who uses logtape for the first time can accidentally fall into the same trap as I did.

WDYT?

dahlia commented 2 months ago

How about a behavior that strips out the whitespace to find the variable if there is leading and trailing whitespace in a template variable and it doesn't match exactly?

logger.info("value = { foo }", { " foo ": 1, foo: 2 });
// INF value = 1
logger.info("value = { foo }", { foo: 2 });
// INFO value = 2
dahlia commented 2 months ago

It will be shipped with v0.6.0, which is the next release. You can give it a try in advance at v0.6.0-dev.80+33bdf827 (JSR & npm).

kachkaev commented 2 months ago

Perfect, thanks! 🚀

dahlia commented 2 months ago

This is shipped with LogTape v0.6.0 (JSR & npm).