ios-android-logs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseiOS and Android Device Logs
iOS和Android设备日志
Complete guide to viewing and filtering device logs on iOS and Android.
Capacitor应用在iOS和Android平台查看与过滤设备日志的完整指南。
When to Use This Skill
使用场景
- User needs to see device logs
- User is debugging crashes
- User wants to filter logs by app
- User needs real-time log streaming
- User asks "how to see logs"
- 用户需要查看设备日志
- 用户正在调试崩溃问题
- 用户希望按应用过滤日志
- 用户需要实时日志流
- 用户询问“如何查看日志”
Quick Commands
快速命令
bash
undefinedbash
undefinediOS - Stream logs from connected device
iOS - 从已连接设备流式传输日志
xcrun devicectl device log stream --device <UUID>
xcrun devicectl device log stream --device <UUID>
iOS - Stream from simulator
iOS - 从模拟器流式传输日志
xcrun simctl spawn booted log stream
xcrun simctl spawn booted log stream
Android - Stream all logs
Android - 流式传输所有日志
adb logcat
adb logcat
Android - Filter by package
Android - 按应用包名过滤
adb logcat --pid=$(adb shell pidof com.yourapp.id)
undefinedadb logcat --pid=$(adb shell pidof com.yourapp.id)
undefinediOS Logs
iOS日志
Method 1: Console.app (GUI)
方法1:Console.app(图形界面)
- Open Console.app (Applications > Utilities)
- Select your device in sidebar
- Click "Start Streaming"
- Use search to filter:
- By process:
process:YourApp - By subsystem:
subsystem:com.yourapp - By message:
"error"
- By process:
- 打开Console.app(应用程序 > 实用工具)
- 在侧边栏选择你的设备
- 点击“开始流式传输”
- 使用搜索功能过滤日志:
- 按进程:
process:YourApp - 按子系统:
subsystem:com.yourapp - 按消息内容:
"error"
- 按进程:
Method 2: devicectl (CLI - Recommended)
方法2:devicectl(命令行工具 - 推荐)
bash
undefinedbash
undefinedList connected devices
列出已连接设备
xcrun devicectl list devices
xcrun devicectl list devices
Stream logs from specific device
从指定设备流式传输日志
xcrun devicectl device log stream --device <DEVICE_UUID>
xcrun devicectl device log stream --device <DEVICE_UUID>
Stream with predicate filter
使用谓词过滤日志
xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"'
--predicate 'process == "YourApp"'
xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"'
--predicate 'process == "YourApp"'
Stream specific log levels
流式传输指定级别的日志
xcrun devicectl device log stream --device <DEVICE_UUID>
--level error
--level error
xcrun devicectl device log stream --device <DEVICE_UUID>
--level error
--level error
Save to file
将日志保存到文件
xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"' > app_logs.txt
--predicate 'process == "YourApp"' > app_logs.txt
undefinedxcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"' > app_logs.txt
--predicate 'process == "YourApp"' > app_logs.txt
undefinedMethod 3: simctl for Simulators
方法3:使用simctl查看模拟器日志
bash
undefinedbash
undefinedStream logs from booted simulator
从已启动的模拟器流式传输日志
xcrun simctl spawn booted log stream
xcrun simctl spawn booted log stream
Filter by process
按进程过滤
xcrun simctl spawn booted log stream --predicate 'process == "YourApp"'
xcrun simctl spawn booted log stream --predicate 'process == "YourApp"'
Filter by subsystem
按子系统过滤
xcrun simctl spawn booted log stream --predicate 'subsystem == "com.yourapp"'
xcrun simctl spawn booted log stream --predicate 'subsystem == "com.yourapp"'
Show only errors
仅显示错误日志
xcrun simctl spawn booted log stream --level error
xcrun simctl spawn booted log stream --level error
Combine filters
组合过滤条件
xcrun simctl spawn booted log stream
--predicate 'process == "YourApp" AND messageType == error'
--predicate 'process == "YourApp" AND messageType == error'
undefinedxcrun simctl spawn booted log stream
--predicate 'process == "YourApp" AND messageType == error'
--predicate 'process == "YourApp" AND messageType == error'
undefinedMethod 4: Xcode Device Logs
方法4:Xcode设备日志
- Window > Devices and Simulators
- Select device
- Click "Open Console"
- Or: View device logs for crash reports
- 窗口 > 设备与模拟器
- 选择设备
- 点击“打开控制台”
- 或者:查看设备日志中的崩溃报告
iOS Log Predicate Examples
iOS日志谓词示例
bash
undefinedbash
undefinedProcess name
按进程名称
--predicate 'process == "YourApp"'
--predicate 'process == "YourApp"'
Contains text
包含指定文本
--predicate 'eventMessage contains "error"'
--predicate 'eventMessage contains "error"'
Subsystem
按子系统
--predicate 'subsystem == "com.yourapp.plugin"'
--predicate 'subsystem == "com.yourapp.plugin"'
Category
按分类
--predicate 'category == "network"'
--predicate 'category == "network"'
Log level
按日志级别
--predicate 'messageType == error'
--predicate 'messageType == error'
Combined
组合条件
--predicate 'process == "YourApp" AND messageType >= error'
--predicate 'process == "YourApp" AND messageType >= error'
Time-based (last 5 minutes)
时间范围(最近5分钟)
--predicate 'timestamp > now - 5m'
undefined--predicate 'timestamp > now - 5m'
undefinediOS Log Levels
iOS日志级别
| Level | Description |
|---|---|
| Default messages |
| Informational |
| Debug (hidden by default) |
| Error conditions |
| Fault/critical |
| 级别 | 描述 |
|---|---|
| 默认消息 |
| 信息性消息 |
| 调试信息(默认隐藏) |
| 错误情况 |
| 严重错误/故障 |
Android Logs
Android日志
Method 1: adb logcat (CLI)
方法1:adb logcat(命令行工具)
bash
undefinedbash
undefinedBasic log stream
基础日志流
adb logcat
adb logcat
Clear logs first, then stream
先清空日志,再开始流式传输
adb logcat -c && adb logcat
adb logcat -c && adb logcat
Filter by tag
按标签过滤
adb logcat -s MyTag:D
adb logcat -s MyTag:D
Filter by priority
按优先级过滤
adb logcat *:E # Only errors and above
adb logcat *:E # 仅显示错误及以上级别
Filter by package name
按包名过滤
adb logcat --pid=$(adb shell pidof com.yourapp.id)
adb logcat --pid=$(adb shell pidof com.yourapp.id)
Filter by multiple tags
按多个标签过滤
adb logcat -s "MyPlugin:D" "Capacitor:I"
adb logcat -s "MyPlugin:D" "Capacitor:I"
Save to file
将日志保存到文件
adb logcat > logs.txt
adb logcat > logs.txt
Save to file with timestamp
带时间戳保存到文件
adb logcat -v time > logs.txt
undefinedadb logcat -v time > logs.txt
undefinedMethod 2: Android Studio Logcat (GUI)
方法2:Android Studio Logcat(图形界面)
- View > Tool Windows > Logcat
- Use filter dropdown:
- Package:
package:com.yourapp - Tag:
tag:MyPlugin - Level:
level:error
- Package:
- Create saved filters for quick access
- 视图 > 工具窗口 > Logcat
- 使用过滤下拉菜单:
- 包名:
package:com.yourapp - 标签:
tag:MyPlugin - 级别:
level:error
- 包名:
- 创建保存的过滤条件以便快速访问
Method 3: pidcat (Better CLI Tool)
方法3:pidcat(更优命令行工具)
bash
undefinedbash
undefinedInstall pidcat
安装pidcat
pip install pidcat
pip install pidcat
Stream logs for package
流式传输指定包名的日志
pidcat com.yourapp.id
pidcat com.yourapp.id
With tag filter
带标签过滤
pidcat -t MyPlugin com.yourapp.id
undefinedpidcat -t MyPlugin com.yourapp.id
undefinedAndroid Log Priority Levels
Android日志优先级级别
| Letter | Priority |
|---|---|
| V | Verbose |
| D | Debug |
| I | Info |
| W | Warn |
| E | Error |
| F | Fatal |
| S | Silent |
| 字母 | 优先级 |
|---|---|
| V | 详细(Verbose) |
| D | 调试(Debug) |
| I | 信息(Info) |
| W | 警告(Warn) |
| E | 错误(Error) |
| F | 致命(Fatal) |
| S | 静默(Silent) |
adb logcat Format Options
adb logcat格式选项
bash
undefinedbash
undefinedDifferent output formats
不同输出格式
adb logcat -v brief # Default
adb logcat -v process # PID only
adb logcat -v tag # Tag only
adb logcat -v time # With timestamp
adb logcat -v threadtime # With thread and time
adb logcat -v long # All metadata
adb logcat -v brief # 默认格式
adb logcat -v process # 仅显示进程ID
adb logcat -v tag # 仅显示标签
adb logcat -v time # 带时间戳
adb logcat -v threadtime # 带线程和时间信息
adb logcat -v long # 显示所有元数据
Colorized output
彩色输出
adb logcat -v color
adb logcat -v color
Show recent logs (last N lines)
显示最近N条日志
adb logcat -d -t 100
adb logcat -d -t 100
Show logs since timestamp
显示指定时间戳之后的日志
adb logcat -v time -T "01-25 10:00:00.000"
undefinedadb logcat -v time -T "01-25 10:00:00.000"
undefinedCommon Android Filters
常见Android过滤规则
bash
undefinedbash
undefinedCapacitor core logs
Capacitor核心日志
adb logcat -s "Capacitor:*"
adb logcat -s "Capacitor:*"
Plugin-specific logs
插件专属日志
adb logcat -s "CapacitorNativeBiometric:*"
adb logcat -s "CapacitorNativeBiometric:*"
WebView logs (JavaScript console)
WebView日志(JavaScript控制台)
adb logcat -s "chromium:*"
adb logcat -s "chromium:*"
JavaScript errors
JavaScript错误
adb logcat | grep -i "js error|uncaught"
adb logcat | grep -i "js error|uncaught"
Crash logs
崩溃日志
adb logcat | grep -iE "fatal|crash|exception"
adb logcat | grep -iE "fatal|crash|exception"
Network logs
网络日志
adb logcat -s "OkHttp:" "NetworkSecurityConfig:"
undefinedadb logcat -s "OkHttp:" "NetworkSecurityConfig:"
undefinedViewing Crash Logs
查看崩溃日志
iOS Crash Logs
iOS崩溃日志
bash
undefinedbash
undefinedCopy crash logs from device
从设备复制崩溃日志
xcrun devicectl device copy crashlog --device <UUID> ./crashes/
xcrun devicectl device copy crashlog --device <UUID> ./crashes/
View in Console.app
在Console.app中查看
User Diagnostics Reports section
用户诊断报告板块
Or find at:
或者在以下位置查找:
Device: Settings > Privacy > Analytics & Improvements > Analytics Data
设备:设置 > 隐私与安全性 > 分析与改进 > 分析数据
Mac: ~/Library/Logs/DiagnosticReports/
Mac:~/Library/Logs/DiagnosticReports/
undefinedundefinedAndroid Crash Logs
Android崩溃日志
bash
undefinedbash
undefinedGet tombstone (native crash)
获取tombstone(原生崩溃日志)
adb shell cat /data/tombstones/tombstone_00
adb shell cat /data/tombstones/tombstone_00
Get ANR traces
获取ANR跟踪日志
adb pull /data/anr/traces.txt
adb pull /data/anr/traces.txt
Get bugreport (comprehensive)
获取完整bug报告
adb bugreport > bugreport.zip
undefinedadb bugreport > bugreport.zip
undefinedMCP Integration
MCP集成
Use MCP tools to fetch logs programmatically:
typescript
// Example MCP tool for fetching iOS logs
const logs = await mcp.ios.streamLogs({
device: 'booted',
predicate: 'process == "YourApp"',
level: 'debug',
});
// Example MCP tool for Android logs
const androidLogs = await mcp.android.logcat({
package: 'com.yourapp.id',
level: 'D',
});使用MCP工具以编程方式获取日志:
typescript
// 示例:获取iOS日志的MCP工具
const logs = await mcp.ios.streamLogs({
device: 'booted',
predicate: 'process == "YourApp"',
level: 'debug',
});
// 示例:获取Android日志的MCP工具
const androidLogs = await mcp.android.logcat({
package: 'com.yourapp.id',
level: 'D',
});Log Parsing Tips
日志解析技巧
Extract JavaScript Errors
提取JavaScript错误
bash
undefinedbash
undefinediOS - JavaScript console logs
iOS - JavaScript控制台日志
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "JS:"'
--predicate 'eventMessage contains "JS:"'
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "JS:"'
--predicate 'eventMessage contains "JS:"'
Android - WebView console
Android - WebView控制台日志
adb logcat chromium:I *:S | grep "console"
undefinedadb logcat chromium:I *:S | grep "console"
undefinedFilter Network Requests
过滤网络请求日志
bash
undefinedbash
undefinediOS
iOS
xcrun simctl spawn booted log stream
--predicate 'subsystem == "com.apple.network"'
--predicate 'subsystem == "com.apple.network"'
xcrun simctl spawn booted log stream
--predicate 'subsystem == "com.apple.network"'
--predicate 'subsystem == "com.apple.network"'
Android
Android
adb logcat -s "NetworkSecurityConfig:" "OkHttp:"
undefinedadb logcat -s "NetworkSecurityConfig:" "OkHttp:"
undefinedMonitor Memory
监控内存
bash
undefinedbash
undefinediOS - Memory pressure
iOS - 内存压力
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "memory"'
--predicate 'eventMessage contains "memory"'
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "memory"'
--predicate 'eventMessage contains "memory"'
Android - Memory info
Android - 内存信息
adb shell dumpsys meminfo com.yourapp.id
undefinedadb shell dumpsys meminfo com.yourapp.id
undefinedTroubleshooting
常见问题排查
Issue: No Logs Showing
问题:无日志显示
iOS:
- Ensure device is trusted: Xcode > Window > Devices
- Try restarting log stream
- Check Console.app filters
Android:
- Enable USB debugging
- Run to verify connection
adb devices - Try
adb kill-server && adb start-server
iOS:
- 确保设备已信任:Xcode > 窗口 > 设备
- 尝试重启日志流
- 检查Console.app的过滤条件
Android:
- 启用USB调试
- 运行验证设备连接
adb devices - 尝试
adb kill-server && adb start-server
Issue: Too Many Logs
问题:日志过多
Use filters:
bash
undefined使用过滤条件:
bash
undefinediOS - Only your app
iOS - 仅显示你的应用日志
--predicate 'process == "YourApp" AND messageType >= info'
--predicate 'process == "YourApp" AND messageType >= info'
Android - Only your package
Android - 仅显示指定包名的日志
adb logcat --pid=$(adb shell pidof com.yourapp.id)
undefinedadb logcat --pid=$(adb shell pidof com.yourapp.id)
undefinedIssue: Missing Debug Logs
问题:缺少调试日志
iOS: Debug logs are hidden by default
bash
undefinediOS:调试日志默认隐藏
bash
undefinedEnable debug logs
启用调试日志
xcrun simctl spawn booted log stream --level debug
**Android**: Ensure log level is set correctly
```kotlin
Log.d("Tag", "Debug message") // D levelxcrun simctl spawn booted log stream --level debug
**Android**:确保日志级别设置正确
```kotlin
Log.d("Tag", "Debug message") // D级别Best Practices
最佳实践
- Use structured logging - Include context in log messages
- Add timestamps - Helps correlate events
- Filter early - Don't stream all logs
- Save important logs - Redirect to file for later analysis
- Use log levels appropriately - Debug for dev, error for production
- 使用结构化日志 - 在日志消息中包含上下文信息
- 添加时间戳 - 有助于关联事件
- 提前过滤 - 不要流式传输所有日志
- 保存重要日志 - 重定向到文件以便后续分析
- 合理使用日志级别 - 调试级别用于开发环境,错误级别用于生产环境
Resources
参考资源
- iOS Unified Logging: https://developer.apple.com/documentation/os/logging
- Android Logcat: https://developer.android.com/studio/debug/logcat
- devicectl Reference: https://developer.apple.com/documentation/devicemanagement
- iOS统一日志系统:https://developer.apple.com/documentation/os/logging
- Android Logcat:https://developer.android.com/studio/debug/logcat
- devicectl参考文档:https://developer.apple.com/documentation/devicemanagement