首页 > 解决方案 > is it possible to make an on(consolelog event? it would do something every time text is logged to console

问题描述

This feature would do something every time text is logged to the node.js console. Here's a somewhat of example of how I think it should look like:

on('console', msg => {console.log("text was logged! " + msg)})

Regular node.js

标签: javascriptnode.js

解决方案


Console methods don't emit events, but the console object and its methods can be overwritten, so you could redefine it yourself: console.log = (...args) => { process.stdout.write('logged: ' + args.join(' ')) }, or wrap it in a Proxy.


推荐阅读