Skip to content

Commit 70e7b2d

Browse files
efernandes-techhuntie
andauthoredJan 23, 2025
Add instructions for custom logging in native modules (facebook#4447)
Co-authored-by: Alex Hunt <[email protected]>
1 parent 1387af7 commit 70e7b2d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
 

‎docs/debugging-native-code.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,47 @@ npx react-native log-ios
2323

2424
You may also access these through Debug > Open System Log… in the iOS Simulator or by running `adb logcat "*:S" ReactNative:V ReactNativeJS:V` in a terminal while an Android app is running on a device or emulator.
2525

26+
<details>
27+
<summary>**💡 Custom Native Logs**</summary>
28+
29+
If you are writing a Native Module and want to add custom logs to your module for debugging purposes, you can use the following method:
30+
31+
#### Android (Java/Kotlin)
32+
33+
In your native module, use the `Log` class to add logs that can be viewed in Logcat:
34+
35+
```java
36+
import android.util.Log;
37+
38+
private void log(String message) {
39+
Log.d("YourModuleName", message);
40+
}
41+
```
42+
43+
To view these logs in Logcat, use this command, replacing `YourModuleName` with your custom tag:
44+
45+
```shell
46+
adb logcat "*:S" ReactNative:V ReactNativeJS:V YourModuleName:D
47+
```
48+
49+
#### iOS (Objective-C/Swift)
50+
51+
In your native module, use `NSLog` for custom logs:
52+
53+
```objective-c
54+
NSLog(@"YourModuleName: %@", message);
55+
```
56+
57+
Or, in Swift:
58+
59+
```swift
60+
print("YourModuleName: \(message)")
61+
```
62+
63+
These logs will appear in the Xcode console when running the app.
64+
65+
</details>
66+
2667
## Debugging in a Native IDE
2768

2869
When working with native code, such as when writing native modules, you can launch the app from Android Studio or Xcode and take advantage of the native debugging features (setting up breakpoints, etc.) as you would in case of building a standard native app.

0 commit comments

Comments
 (0)
Please sign in to comment.