查看日志

了解如何在使用 Expo CLI 时查看日志、Android Studio 和 Xcode 中的原生日志以及系统日志。


在 React Native 应用中记录信息的方式与在网页浏览器中类似。你可以使用 console.logconsole.warnconsole.error。然而,有时你可能希望深入了解,以获取有关应用中发生情况的更多有用信息。为此,你可以使用 本地日志系统日志

🌐 Logging information in a React Native app works similarly to in a web browser. You can use console.log, console.warn and console.error. However, at times, you might want to dive deep to get more useful information about what's happening in your app. For that, you can use native logs and system logs.

控制台日志

🌐 Console logs

当你运行 npx expo start 并连接一个设备时,控制台日志将显示在终端进程中。这些日志是通过 WebSocket 从运行时发送到 Expo CLI 的,这意味着结果的精确度低于直接将开发工具连接到引擎时的情况。

🌐 When you run npx expo start and connect a device, console logs will show up in the terminal process. These logs are sent from the runtime to Expo CLI over web sockets, meaning the results are lower fidelity than connecting dev tools directly to the engine.

你可以通过使用 Hermes 创建开发版本,并 连接检查器,查看高保真日志,并使用像 console.table 这样的高级日志功能。

🌐 You can view high fidelity logs and use advanced logging functions like console.table by creating a development build with Hermes, and connecting the inspector.

原生日志

🌐 Native logs

通过在本地编译本地应用,你可以在 Android Studio 和 Xcode 中查看本地运行时日志。更多信息,请参阅 本地调试

🌐 You can view native runtime logs in Android Studio and Xcode by compiling the native app locally. For more information, see native debugging.

系统日志

🌐 System logs

虽然通常没有必要,但如果你想查看设备上发生的所有情况的日志,例如,甚至来自其他应用和操作系统的日志,你可以使用以下命令:

🌐 While it's usually not necessary, if you want to see logs for everything happening on your device, for example, even the logs from other apps and the OS, you can use the following commands:

Terminal
# Show system logs for an Android device with adb logcat
npx react-native log-android
# Show system logs for an iOS device
npx react-native log-ios