Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instructions for custom logging in native modules #4447

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/debugging-native-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,47 @@ npx react-native log-ios

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.

<details>
<summary>**💡 Custom Native Logs**</summary>

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:

#### Android (Java/Kotlin)

In your native module, use the `Log` class to add logs that can be viewed in Logcat:

```java
import android.util.Log;

private void log(String message) {
Log.d("YourModuleName", message);
}
```

To view these logs in Logcat, use this command, replacing `YourModuleName` with your custom tag:

```shell
adb logcat "*:S" ReactNative:V ReactNativeJS:V YourModuleName:D
```

#### iOS (Objective-C/Swift)

In your native module, use `NSLog` for custom logs:

```objective-c
NSLog(@"YourModuleName: %@", message);
```

Or, in Swift:

```swift
print("YourModuleName: \(message)")
```

These logs will appear in the Xcode console when running the app.

</details>

## Debugging in a Native IDE

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.
Expand Down
Loading