React 远程控制台日志记录
React Remote Console Logging
我使用 Mongo 设置了一个 Express Server,以便在使用 React 对 Electron 应用程序进行调试测试期间记录控制台日志。
我只是使用 ajax 来发送我通常用 console.log 打印的内容。这适用于我想要记录的单个事件,但是我如何将整个 chrome 样式的控制台导出为一个对象,以便到达控制台的任何内容(例如:webpack 消息、来自其他组件的消息等)都将是作为一个对象可访问,我可以对其进行 POST。
基本上是一种记录您在控制台中看到的所有内容的方法,无论是来自第三方包还是我自己明确记录的内容。是否有我在 chromium/electron/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);
将单个事件记录到服务器很容易。如何转储整个控制台?
更新
下面提到的是把进程绑定到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);
});
然而,当与反应一起使用时,两者都给我这个错误:
TypeError: Cannot read property 'write' of undefined
hook_stream
当我在电子 main.js 中使用该特定方法时,它似乎可以很好地记录电子节点端。但是我无法让它在我的反应组件中工作。
您绑定到 stdout, stderr streams in the process 模块。
看看 npm capture-console。您将需要从任何渲染器进程以及主进程捕获控制台输出。
更新
Electron 似乎已经完成了一些 strange things with renderer process stdout stream. You are better off using a custom logging solution like electron-log 并从写入的日志文件同步日志。
这样做的一种方法是用您的自定义实现覆盖 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);
我使用 Mongo 设置了一个 Express Server,以便在使用 React 对 Electron 应用程序进行调试测试期间记录控制台日志。
我只是使用 ajax 来发送我通常用 console.log 打印的内容。这适用于我想要记录的单个事件,但是我如何将整个 chrome 样式的控制台导出为一个对象,以便到达控制台的任何内容(例如:webpack 消息、来自其他组件的消息等)都将是作为一个对象可访问,我可以对其进行 POST。
基本上是一种记录您在控制台中看到的所有内容的方法,无论是来自第三方包还是我自己明确记录的内容。是否有我在 chromium/electron/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);
将单个事件记录到服务器很容易。如何转储整个控制台?
更新 下面提到的是把进程绑定到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);
});
然而,当与反应一起使用时,两者都给我这个错误:
TypeError: Cannot read property 'write' of undefined
hook_stream
当我在电子 main.js 中使用该特定方法时,它似乎可以很好地记录电子节点端。但是我无法让它在我的反应组件中工作。
您绑定到 stdout, stderr streams in the process 模块。
看看 npm capture-console。您将需要从任何渲染器进程以及主进程捕获控制台输出。
更新
Electron 似乎已经完成了一些 strange things with renderer process stdout stream. You are better off using a custom logging solution like electron-log 并从写入的日志文件同步日志。
这样做的一种方法是用您的自定义实现覆盖 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);