joh*_*p34 5 console chromium console.log reactjs electron
我使用 Mongo 设置了一个 Express Server,以在使用 React 调试 Electron 应用程序期间记录控制台日志。
我只是使用 ajax 发送我通常使用 console.log 打印的内容。这适用于我想要记录的单个事件,但是我如何将整个 chrome 样式控制台导出为一个对象,以便任何可以到达控制台的内容(例如:webpack 消息、来自其他组件的消息等)都可以作为一个对象访问我可以做一个POST。
基本上是一种记录您在控制台中看到的所有内容的方法,无论是来自第 3 方软件包还是我自己明确记录的内容。是否有我在铬/电子/反应文档中没有看到的某种控制台转储所有方法?
例子:
//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);
Run Code Online (Sandbox Code Playgroud)
将单个事件记录到服务器很容易。如何转储整个控制台?
UPDATE 下面提到的是绑定到stdout 和stderr 进程。我尝试了推荐的包 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);
});
Run Code Online (Sandbox Code Playgroud)
但是,当与 react 一起使用时,两者都给我写了这个错误:
TypeError: Cannot read property 'write' of undefined
hook_stream
Run Code Online (Sandbox Code Playgroud)
当我在电子 main.js 中使用它时,该特定方法似乎可以很好地记录电子节点侧。但是我无法让它在我的反应组件中工作。
这样做的一种方法是console.log用您的自定义实现覆盖,因此每当代码的任何部分调用console.log该调用时,您的自定义函数都会拦截该调用,您可以在其中使用某些 API 调用将消息记录到远程服务器。
一旦您记录了您的消息,您就可以调用原始console.log方法。
以下示例显示了console.log方法的自定义实现。
var orgLog = console.log;
console.log = function(message) {
alert("Intercepted -> " + message); //Call Remote API to log the object.
//Invoke the original console.log
return orgLog(message);
}
let a = {
foo: "bar"
};
console.log(a);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4055 次 |
| 最近记录: |