反应远程控制台日志 [英] React Remote Console Logging

查看:148
本文介绍了反应远程控制台日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Mongo设置了Express Server,以便在使用React进行Electron应用程序的调试测试期间记录控制台日志。

I setup an Express Server with Mongo to record console logs during debug testing of an Electron app using React.

我只是使用ajax发送通常会打印的内容与console.log。这对于我要记录的单个事件很好用,但是我如何将整个chrome样式控制台导出为一个对象,以便可以到达控制台的所有内容(例如:webpack消息,来自其他组件的消息等)都可以作为一个对象进行访问

I simply use ajax to send what I would normally print with console.log. This works fine with individual events I want logged, but how do I export the entire chrome style console as an object so that anything that would reach the console (example: webpack messages, messages from other components etc) would all be accessible as one object that I can do a POST on.

基本上是一种记录您在控制台中看到的所有内容的方法,无论它是来自第3方程序包还是我明确记录了自己。是否有控制台转储所有我在铬/电子/反应文档中没有看到的某种方法?

Basically a way to record everything that you would see in the console whether it was from a 3rd party package or that I expressly logged myself. Is there a console dump all method of some sort I'm not seeing in the chromium/electron/react docs?

示例:

//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);

将单个事件记录到服务器很容易。我该如何转储整个控制台?

Logging individual events to the server is easy. How do I dump the entire console?

更新
下面提到的是与stdout和stderr进程相关的。我尝试了推荐的软件包捕获控制台以及以下代码段:

UPDATE Mentioned below was to tie into the process stdout and stderr. I tried the recommended package capture-console and also this code snippet:

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一起使用时,两者都给我这个写错误信息:

However both give me this error with write when using with react:

TypeError: Cannot read property 'write' of undefined
hook_stream

$的属性 write b
$ b

当我在电子main.js中使用它时,该特定方法似乎记录了电子节点一侧的情况。但是,我无法在我的react组件中使用它。

That particular method seems to log the electron node side fine when I use it in the electron main.js. However I can't get it to work within my react components.

推荐答案

一种方法是覆盖 console.log 与您的自定义实现,因此,只要代码的任何部分调用 console.log ,该调用都会被拦截您的自定义函数,您可以在其中使用一些API调用将消息记录到远程服务器。

One way of doing this is to overwrite the console.log with your custom implementation, so whenever any part of the code calls the console.log the call will be intercepted by your custom function where you can log the message to your remote server using some API calls.

一旦您记录了消息,就可以调用原始的 console.log 方法。

Once you have logged your message you can call the original console.log method.

下面的示例显示了 console.log 的自定义实现方法。

Following example shows a custom implementation of console.log method.

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);

这篇关于反应远程控制台日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆