我使用Express Server和Mongo来记录在使用React开发Electron应用程序时进行调试测试期间的控制台日志。我简单地使用ajax发送我通常使用console.log打印的内容。这对我想要记录的单个事件有效,但是如何将整个Chrome样式控制台导出为一个对象,以便可以访问到从控制台获取的任何内容(例如:webpack消息、来自其他组件的消息等),并将它们作为一个对象进行POST呢?基本上,需要一种方法来记录您在控制台中看到的所有内容,无论是来自第三方软件包还是我自己明确记录的内容。是否有某种控制台全部转储的方法,我没有在Chromium / Electron / React文档中看到?例如:
但是,在使用React时,无论哪种方式都会给我返回写入时的错误:
//import some debugger method to POST to server collecting logs
export function debugpost(logobject) {
$.ajax({
type: "POST",
url: "http://" + "192.168.0.94" + ":3000/tasks",
headers: {
},
data: {
log: logobject
},
success: function(data) {
}.bind(this),
error: function(errMsg) {
console.log(errMsg);
}.bind(this)
});
}
//simple way of recording logs in other component.
var testlogmessage = "This isn't right"
debugpost(testlogmessage);
将单个事件记录到服务器很容易。如何转储整个控制台?
更新 下面提到的是将其连接到进程的标准输出和标准错误输出。我尝试了推荐的 capture-console 包以及以下代码片段:
var logs = [],
hook_stream = function(_stream, fn) {
// Reference default write method
var old_write = _stream.write;
// _stream now write with our shiny function
_stream.write = fn;
return function() {
// reset to the default write method
_stream.write = old_write;
};
},
// hook up standard output
unhook_stdout = hook_stream(process.stdout, function(string, encoding, fd) {
logs.push(string);
});
但是,在使用React时,无论哪种方式都会给我返回写入时的错误:
TypeError: Cannot read property 'write' of undefined
hook_stream
当我在 electron 的 main.js 中使用该特定方法时,它似乎能够很好地记录 electron 节点端的情况。然而,我无法在我的 React 组件中使其工作。
console.info
、console.warn
和console.error
,参考:https://github.com/webpack/webpack/blob/e4ae6463c1afd30183fb4b3cadfc4bd129229b1f/hot/log.js#L23-L29 - James Brierleywindow.onerror
来完成这个任务。https://developer.mozilla.org/zh-CN/docs/Web/API/GlobalEventHandlers/onerror - James Brierley