Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write to console.log from Java in React Native

I'm building a React Native native module (aka 'bridge') for iOS and Android and need to log to the JS console in native code (Objective C and Java). This is done easily in iOS using RCTLog but I can't figure out how to do it in Java.

I tried Log.i but those messages aren't forwarded to Javascript.

The only thing I can think of at this point is to emit a JS event from the Java module and have a handler on the JS side just call console.log with the message but that's a pretty roundabout way of doing it.

I would expect to be able to do something like this.getReactApplicationContext().log("see me in the JS console") but no dice. Anyone know how to do this?

like image 236
Steve Potter Avatar asked Nov 13 '17 17:11

Steve Potter


Video Answer


1 Answers

It looks like as of upcoming react native 0.63 we may be getting the equivalent of iOS's RCTLog in Android: https://github.com/facebook/react-native/commit/52b3105f652eca72892f200923e1687f1d995486

The files are in master: https://github.com/facebook/react-native/tree/master/ReactAndroid/src/main/java/com/facebook/react/util

like image 66
sbearben Avatar answered Sep 23 '22 17:09

sbearben