nibbiolo
nibbiolo9mo ago

Attaching metadata to console.log

hello, is there a way to include automatically in logs ( preferably via console.log() ) a specific "common" value that is used afteward to debug a "session"? FOr session here i mean a set of function which are needed to complete a bigger task. Example, I have a task that requires 15 mutations and 3 action calls. In datadog/axiom and i want to "isolate" all the functions related to that specific task. Ideally i want to add the ID of this task to all the related console.log wherever they are called (mutations or actions related to that task), but wondering if there is a way to do it which is not manual, something like in a wrapper/middleware, or a solution where i dont really need to pass manually this ID around to all console.log, or around function calling as parameter
3 Replies
sshader
sshader9mo ago
Echoing what other folks have said, we have "request ID" which will be added to axiom / datadog events shortly, and might be what you're looking for here -- an action calling a mutation will both have the same request ID Another strategy is to use custom functions to expose something like ctx.logger that's more customized to the logging you want to do, which gets attached using customFunctions(https://github.com/get-convex/convex-helpers?tab=readme-ov-file#custom-functions). I have an example of something like this for one of my apps (https://github.com/sshader/proset/pull/6). In my case, I wanted to log events for particular games and be able to filter to any logs for a particular game, so I have stuff like ctx.logger.log("GAME", { gameId: game._id }, "Started") but with some tricks for type-safety. If you have some existing value that's common across all functions for your bigger task (and especially if you already have customFunctions code that pulls that out), you could also customize your logger to do something like ctx.logger.logForTask("foo") that automatically injects in that value (e.g. if I were scraping a website with a series of scheduled functions, I might prefix all of my log lines with the URL I'm scraping)
GitHub
Build software better, together
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.
From An unknown user
From An unknown user
From An unknown user
GitHub
GitHub - get-convex/convex-helpers: A collection of useful code to ...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
sshader
sshader9mo ago
(also stay tuned for a stack post on different ways to use console.log + axiom / datadog to monitor and debug your app, but this is a bit of a preview)
nibbiolo
nibbioloOP9mo ago
you guys are amazing, thank you so much, your help i so invaluable

Did you find this page helpful?