I couldn't find a logging library that worked for my library, so I made one
15 points by hongminhee
15 points by hongminhee
I started using LogTape a few months ago and really like it.
The only serious complaint I have is there’s no way to check whether a particular log call would have any effect, so I can’t skip a potentially-expensive debug-level log call when there’s no debug logging enabled. By potentially-expensive I mean one where it’s expensive to compute the args that will be logged, like calling Array.map to extract a list of keys from a bunch of database records.
I’m also sad that implicit contexts aren’t implemented in web browsers, so I can’t take advantage of that. (It's a TC39 stage 2 proposal that node/deno/bun have already adopted but browsers haven’t, and Firefox has taken a negative position on it for some reason, which doesn’t bode well.)
Thanks! For expensive log calls, LogTape supports lazy evaluation—pass a callback and it only runs if the log level is enabled:
logger.debug("Keys: {keys}", () => ({ keys: records.map(r => r.key) }));
Agreed on the browser implicit context situation—hoping TC39 Async Context gets wider adoption eventually.
Isn't this how every Java and Python logging library works? I'm surprised something like this didn't exist in JS.
Of possible interest:
https://dave.autonoma.ca/blog/2022/01/08/logging-code-smell/
Swapping logging libraries is a great example of why duplicating logging statements throughout the code base can be improved upon by using an event-based architecture, which allows isolating the log statement to a single line of code.