Code Monkey home page Code Monkey logo

Comments (7)

IjzerenHein avatar IjzerenHein commented on June 19, 2024 6

Hi! So for Android I currently only capture console.log, console.info and console.error' Any log-messages that are logged in the native-code are currently nog logged.

I'm using Javascript code to intercept console.log:

function init() {
    if (Platform.OS === 'android') {
        console.log = interceptLog(console.log);
        console.info = interceptLog(console.info);
        console.error = interceptLog(console.error);
        //console.debug = interceptLog(console.debug);
    }
    if (!__DEV__) {
        global.ErrorUtils.setGlobalHandler(errorHandler);
    }
}

function interceptLog(originalFn) {
    return function() {
        const args = Array.prototype.slice.apply(arguments);
        let result = '';
        for (let i = 0; i < args.length; i++) {
            const arg = args[i];
            if (!arg || (typeof arg === 'string') || (typeof arg === 'number')) {
                result += arg;
            }
            else {
                result += JSON.stringify(arg);
            }
        }
        //originalFn.call(console, 'INTERCEPTED LOG: ' + result);
        FirebaseCrash.log(result);
        return originalFn.apply(console, arguments);
    };
}

from react-native-firebase-crash-report.

IjzerenHein avatar IjzerenHein commented on June 19, 2024 1

Hi @ianlin. Yes you're true, you could integrate it in AppDelegate (that's exactly what I've done so far 🙃). Yet, I'd still love to see this in the form of an installable plugin rather than custom AppDelegate code.

So, here is my case for using automatically hooking into console.log:

  • Console.log support n+1 arguments so you can do things like this console.log("hello", "world: ", {a: 1, b: 2})
  • Standard way to support log-levels using console.info, console.warn, console.error
  • Warnings throws by react-native are automatically logged (they use console.warn)
  • Code portability, when you use console.log, the code can be used on the web as well, and could easily switch to another crash reporting tool if you want
  • Log messages generated by other plugins are also captured

Also, I think it would be really nice if this is a feature of this plugin. Perhaps one that you could enable conditionally, but in my perception it should be enabled always when you choose to use this plugin. Also, most people don't know that you can intercept react-native's log messages. Having this code in a central plugin that everyone uses makes it much easier to maintain for all of us.

Cheers :)

from react-native-firebase-crash-report.

ianlin avatar ianlin commented on June 19, 2024 1

That sounds great, will try to make it soon. Btw do you have any idea of overriding react-native's logging functions in Android?

from react-native-firebase-crash-report.

IjzerenHein avatar IjzerenHein commented on June 19, 2024 1

@temitope No, there's no reason why that code shouldn't work on iOS too. When I pasted the example, I happened to have a clause for Android only in there at that moment, no idea why anymore. I've been using it successfully for iOS as well.

from react-native-firebase-crash-report.

ianlin avatar ianlin commented on June 19, 2024

Hi @IjzerenHein, thanks for noticing this. We are using a logging abstraction layer in Javascript with this module, e.g.

import FirebaseCrash from 'react-native-firebase-crash-report';

export default class Log {
    constructor(props) {
        this.logToConsole = props.logToConsole;
        this.logToFirebase = props.logToFirebase;
    }

    log(msg) {
        if (this.logToConsole) {
            console.log(msg);
        }
        if (this.logToFirebase) {
            FirebaseCrash.log(msg);
        }
    }

However I think you are right, making it in native side is much better for performance. But it might not be a feature to this module IMO since it can be all done through editing AppDelegate.m without React Native's bridge. Let me know if it make sense to you or not, cheers!

from react-native-firebase-crash-report.

temitope avatar temitope commented on June 19, 2024

@ianlin @IjzerenHein thanks for the code and the discussion, super helpful.

Do any of you see any reason why the interceptLog code @IjzerenHein shared shouldn't (or couldn't) be used for iOS? I noticed that it was only done for android in the code. If possible I would love to avoid modifying the AppDelegate.m file entirely and simply do this in pure JS on the react-native side for both platforms.

Cheers

from react-native-firebase-crash-report.

temitope avatar temitope commented on June 19, 2024

@IjzerenHein great news. thanks!

from react-native-firebase-crash-report.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.