Let’s assume I have several single-spa react microfrontend apps in my project, e.g. Cart
and Products
I want to redefine console.log
, console.error
, etc. so that it had prefix with appropriate microfrontend app name:
-
console.log
invoked fromCart
app:[Cart]: some log info
-
console.log
invoked fromProducts
app:[Products]: some log info
Is it possible to achieve this behavior?
I thought of zone.js, but the execution context is not being passed to component’s children:
import type React from 'react';
import 'zone.js';
export const withLogger = (
Component: React.ReactElement,
regionName: string
): React.ReactElement =>
Zone.current
.fork({
name: regionName
})
.run(() => {
const consoleLogOG = console.log;
window.console.log = (...args): void => {
const newArgs = [`[${Zone.current.name}]`, ...args];
consoleLogOG.apply(console, newArgs);
};
return Component;
});
UPD: I want to create out of the box solution for about 20 microfronts, so I don’t want to move from standard console.log
to any custom logger. Ideally, I want to create some kind of a HOC which will redefine console.log
for all of the calls within wrapper’s subtree (execution context)
2
Answers
it is not possible to make zone.js work in a situation where multiple microfrontends are being used with single-spa. This is because all of the microfrontends share the same environment and zone.js cannot handle this dynamic change.
one possible way would be to create a new function for logging that takes in a specific name for each microfrontend and includes it in the log message. Then, instead of using console.log, each microfrontend can use this new function for logging. This will help differentiate the logs from each microfrontend.
Basically, instead of having all the log messages mixed together in a single file, we can organize them into separate files for each microfront. This will help us easily identify which microfront the log messages belong to by adding the region name before the message. It’s like each microfront has their own little diary, but with their name on the cover so we know whose diary it is.
note: you have to consider how to handle asynchronous functions and error logging in a similar way. thoroughly test