I have a logger class, I want to initialize it in the worker , and inject in the activities from the worker.
But the issue is, I have multiple activity files, each file has multiple methods.
How do i inject it to all these multiple activity files
There’s different ways this can be done, but I think the following two are the ones really worth mentioning:
-
Register a custom logger on
Runtime.logger
. This is the easiest approach, and allow collecting log messages from workflows, activities and from Core SDK. In this approach, your workflow code will use thelogger
object exported by the@temporalio/workflow
package (sample), and your activity code will use thelogger
object accessible on the Activity context (sample). -
Wrap your activities functions in a “createActivities” function that accepts the logger dependency (sample). That approach allows for more general dependency injection use cases. In your case, since you have multiple files, you may aggregate activity functions from each of these files by using the object spread operator, like this:
const worker = await Worker.create({
// ...
activities: {
...createAccountActivities(logger),
...createShopingCartActivities(logger),
...createFavoritesActivities(logger),
},
// ...
});
Tnx for the quick response jwatkins…
If you don’t mind could you please explain the process of “Registering a custom logger on Runtime.logger
” that you have mentioned in point 1
Sorry, I forgot to include the link to that part.
I just added a new sample that specifically demonstrate registration of a custom logger. It should be easier to follow than the previous ‘instrumentation’ sample I gave you previously.
Initialization of Temporal’s Runtime
, including registration of the custom logger and requesting that Core SDK forward its own logs to your customer logger, is done here.