Sometimes there is a situation when you need to execute certain code when calling the console.log()
.
For example, send a request to the server. One way to do this is to override the original method.
But in this case, we lose the filename and the line number where the console.log
call occurs.
Something like this is needed:
console.on('log', () =>{})
All it does is replace console method calls
console.log(/*...args*/)
with
console.log(/*...args*/);
if (console.events) {
console.events.emit({
type: "log",
args: [/*...args*/],
loc: ["code.ts", "2"],
});
}
and provides a utility subscribe
function to listen for these events
With npm:
npm install babel-plugin-console-events
With yarn:
yarn add babel-plugin-console-events
{
"plugins": ["babel-plugin-console-events"]
}
module.exports = {
babel: {
plugins: [
"babel-plugin-console-events",
],
loaderOptions: { /* Any babel-loader configuration options: https://github.com/babel/babel-loader. */ },
loaderOptions: (babelLoaderOptions, { env, paths }) => {
return babelLoaderOptions;
}
}
};
Pass the listener to the subscribe
function and it will be called when calling the log, warn, info, debug, error
methods of the console object.
import { subscribe } from 'babel-plugin-console-events2'
subscribe((event) => {
// code here
})
MIT