ios-android-logs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

iOS 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
undefined
bash
undefined

iOS - 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)
undefined
adb logcat --pid=$(adb shell pidof com.yourapp.id)
undefined

iOS Logs

iOS日志

Method 1: Console.app (GUI)

方法1:Console.app(图形界面)

  1. Open Console.app (Applications > Utilities)
  2. Select your device in sidebar
  3. Click "Start Streaming"
  4. Use search to filter:
    • By process:
      process:YourApp
    • By subsystem:
      subsystem:com.yourapp
    • By message:
      "error"
  1. 打开Console.app(应用程序 > 实用工具)
  2. 在侧边栏选择你的设备
  3. 点击“开始流式传输”
  4. 使用搜索功能过滤日志:
    • 按进程:
      process:YourApp
    • 按子系统:
      subsystem:com.yourapp
    • 按消息内容:
      "error"

Method 2: devicectl (CLI - Recommended)

方法2:devicectl(命令行工具 - 推荐)

bash
undefined
bash
undefined

List 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"'
xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"'

Stream specific log levels

流式传输指定级别的日志

xcrun devicectl device log stream --device <DEVICE_UUID>
--level error
xcrun devicectl device log stream --device <DEVICE_UUID>
--level error

Save to file

将日志保存到文件

xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"' > app_logs.txt
undefined
xcrun devicectl device log stream --device <DEVICE_UUID>
--predicate 'process == "YourApp"' > app_logs.txt
undefined

Method 3: simctl for Simulators

方法3:使用simctl查看模拟器日志

bash
undefined
bash
undefined

Stream 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'
undefined
xcrun simctl spawn booted log stream
--predicate 'process == "YourApp" AND messageType == error'
undefined

Method 4: Xcode Device Logs

方法4:Xcode设备日志

  1. Window > Devices and Simulators
  2. Select device
  3. Click "Open Console"
  4. Or: View device logs for crash reports
  1. 窗口 > 设备与模拟器
  2. 选择设备
  3. 点击“打开控制台”
  4. 或者:查看设备日志中的崩溃报告

iOS Log Predicate Examples

iOS日志谓词示例

bash
undefined
bash
undefined

Process 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'
undefined

iOS Log Levels

iOS日志级别

LevelDescription
default
Default messages
info
Informational
debug
Debug (hidden by default)
error
Error conditions
fault
Fault/critical
级别描述
default
默认消息
info
信息性消息
debug
调试信息(默认隐藏)
error
错误情况
fault
严重错误/故障

Android Logs

Android日志

Method 1: adb logcat (CLI)

方法1:adb logcat(命令行工具)

bash
undefined
bash
undefined

Basic 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
undefined
adb logcat -v time > logs.txt
undefined

Method 2: Android Studio Logcat (GUI)

方法2:Android Studio Logcat(图形界面)

  1. View > Tool Windows > Logcat
  2. Use filter dropdown:
    • Package:
      package:com.yourapp
    • Tag:
      tag:MyPlugin
    • Level:
      level:error
  3. Create saved filters for quick access
  1. 视图 > 工具窗口 > Logcat
  2. 使用过滤下拉菜单:
    • 包名:
      package:com.yourapp
    • 标签:
      tag:MyPlugin
    • 级别:
      level:error
  3. 创建保存的过滤条件以便快速访问

Method 3: pidcat (Better CLI Tool)

方法3:pidcat(更优命令行工具)

bash
undefined
bash
undefined

Install 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
undefined
pidcat -t MyPlugin com.yourapp.id
undefined

Android Log Priority Levels

Android日志优先级级别

LetterPriority
VVerbose
DDebug
IInfo
WWarn
EError
FFatal
SSilent
字母优先级
V详细(Verbose)
D调试(Debug)
I信息(Info)
W警告(Warn)
E错误(Error)
F致命(Fatal)
S静默(Silent)

adb logcat Format Options

adb logcat格式选项

bash
undefined
bash
undefined

Different 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"
undefined
adb logcat -v time -T "01-25 10:00:00.000"
undefined

Common Android Filters

常见Android过滤规则

bash
undefined
bash
undefined

Capacitor 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:"
undefined
adb logcat -s "OkHttp:" "NetworkSecurityConfig:"
undefined

Viewing Crash Logs

查看崩溃日志

iOS Crash Logs

iOS崩溃日志

bash
undefined
bash
undefined

Copy 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/

undefined
undefined

Android Crash Logs

Android崩溃日志

bash
undefined
bash
undefined

Get 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
undefined
adb bugreport > bugreport.zip
undefined

MCP 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
undefined
bash
undefined

iOS - JavaScript console logs

iOS - JavaScript控制台日志

xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "JS:"'
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "JS:"'

Android - WebView console

Android - WebView控制台日志

adb logcat chromium:I *:S | grep "console"
undefined
adb logcat chromium:I *:S | grep "console"
undefined

Filter Network Requests

过滤网络请求日志

bash
undefined
bash
undefined

iOS

iOS

xcrun simctl spawn booted log stream
--predicate 'subsystem == "com.apple.network"'
xcrun simctl spawn booted log stream
--predicate 'subsystem == "com.apple.network"'

Android

Android

adb logcat -s "NetworkSecurityConfig:" "OkHttp:"
undefined
adb logcat -s "NetworkSecurityConfig:" "OkHttp:"
undefined

Monitor Memory

监控内存

bash
undefined
bash
undefined

iOS - Memory pressure

iOS - 内存压力

xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "memory"'
xcrun simctl spawn booted log stream
--predicate 'eventMessage contains "memory"'

Android - Memory info

Android - 内存信息

adb shell dumpsys meminfo com.yourapp.id
undefined
adb shell dumpsys meminfo com.yourapp.id
undefined

Troubleshooting

常见问题排查

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
    adb devices
    to verify connection
  • 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
undefined

iOS - 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)
undefined
adb logcat --pid=$(adb shell pidof com.yourapp.id)
undefined

Issue: Missing Debug Logs

问题:缺少调试日志

iOS: Debug logs are hidden by default
bash
undefined
iOS:调试日志默认隐藏
bash
undefined

Enable debug logs

启用调试日志

xcrun simctl spawn booted log stream --level debug

**Android**: Ensure log level is set correctly
```kotlin
Log.d("Tag", "Debug message")  // D level
xcrun simctl spawn booted log stream --level debug

**Android**:确保日志级别设置正确
```kotlin
Log.d("Tag", "Debug message")  // D级别

Best Practices

最佳实践

  1. Use structured logging - Include context in log messages
  2. Add timestamps - Helps correlate events
  3. Filter early - Don't stream all logs
  4. Save important logs - Redirect to file for later analysis
  5. Use log levels appropriately - Debug for dev, error for production
  1. 使用结构化日志 - 在日志消息中包含上下文信息
  2. 添加时间戳 - 有助于关联事件
  3. 提前过滤 - 不要流式传输所有日志
  4. 保存重要日志 - 重定向到文件以便后续分析
  5. 合理使用日志级别 - 调试级别用于开发环境,错误级别用于生产环境

Resources

参考资源