1

I am developing using React Native CLI as I need some Native Libraries in Android and iOS. Because of this I needed to use react-native link packagename

I then find out that the exposed methods for the Native Module in that package is not enough and I need to add some methods in the Native Module in Java (Android). Because of this I need to be able to debug in Java while I'm running React Native. I have tried System.out.println and it does not work. How can I log some result or message in Java when it is being called in a React Native project????

olfek
  • 3,210
  • 4
  • 33
  • 49
preston
  • 3,721
  • 6
  • 46
  • 78

2 Answers2

4

On the native Android side, you need to use the Log class from android.util.Log for logging.

Then, use adb logcat and filter the output for the tag provided to the function used from the Log class.

For example, on native Android side:

Log.d("THIS IS MY TAG", "HELLO WORLD")

Then in a terminal (assuming you have adb installed):

adb logcat -s "THIS IS MY TAG"

EDIT

Remember to add import android.util.Log at the top of the file that uses Log.d or anything else from the Log class.

EDIT #2

Also, you will need to run a full rebuild of your React Native project so that the modified native files are updated.

You can do this by running the following command: react-native run-android

olfek
  • 3,210
  • 4
  • 33
  • 49
  • Hi Log.d("THIS IS MY TAG", "HELLO WORLD") works but it comes along with alot of noises printed out....too much to handle... I tried adb logcat -s "THIS IS MY TAG" in the terminal but for some reason the HELLO WORLD does not show up....only...--------- beginning of main --------- beginning of system – preston Mar 19 '20 at 04:00
  • 1
    @preston See edit 2, you need to rebuild the React Native project. – olfek Mar 19 '20 at 09:57
1

open separate terminal and execute this command adb logcat -s [TAG]:[LEVEL]

examples:

adb logcat -s ReactNative:D
adb logcat -s ReactNative
adb logcat -s ReactNative, ReactNativeJS
adb logcat -s MyLog:D, ResizeImage:I

In RN Modules (android) JAVA/KOTLIN you have to use this.

String TAG = "ReactNative"; // set any word you want
Log.d(TAG,message); // d is level. 
// Other levels: d(debug), v(verbose), i(info), e(error), w(warning)
// then run 'adb logcat -s ReactNative' in separate terminal

You can also use npx react-native log-android but it only logs ReactNative and ReactNativeJS tags ReactNative (android.util.Log.d('ReactNative ) in the java/kotlin) ReactNativeJS (console.log in js or jsx of your app) Note: To use 'npx react-native log-android', you must use TAG="ReactNative" in you java/kotlin code other Tags will be ignored by log-android.

CrackerKSR
  • 1,380
  • 1
  • 11
  • 29