如何从 iOS 模拟器获取控制台日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10165641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the console logs from the iOS Simulator?
提问by user1335015
I want to see what happens in the iOS Simulator if I'm not testing the app in Xcode.
如果我不在 Xcode 中测试应用程序,我想看看 iOS 模拟器中会发生什么。
For example, if I open a link in the Safari simulator, see what happens in the console, or if I install a web-app, see the links that I'm pressing in console.
例如,如果我在 Safari 模拟器中打开一个链接,查看控制台中会发生什么,或者如果我安装了一个网络应用程序,查看我在控制台中按下的链接。
How can I do this?
我怎样才能做到这一点?
I want to see it in Xcode or Terminal, but it's not a problem if I need to use another bit of software.
我想在 Xcode 或 Terminal 中看到它,但如果我需要使用其他软件,这不是问题。
回答by fbernardo
iOS Simulator > Menu Bar > Debug > Open System Log
iOS 模拟器 > 菜单栏 > 调试 > 打开系统日志
Old ways:
老办法:
iOS Simulator prints its logs directly to stdout, so you can see the logs mixed up with system logs.
iOS Simulator 将其日志直接打印到 stdout,因此您可以看到与系统日志混合的日志。
Open the Terminal and type: tail -f /var/log/system.log
打开终端并输入: tail -f /var/log/system.log
Then run the simulator.
然后运行模拟器。
EDIT:
编辑:
This stopped working on Mavericks/Xcode 5. Now you can access the simulator logs in its own folder: ~/Library/Logs/iOS Simulator/<sim-version>/system.log
这在 Mavericks/Xcode 5 上停止工作。现在您可以在其自己的文件夹中访问模拟器日志: ~/Library/Logs/iOS Simulator/<sim-version>/system.log
You can either use the Console.app to see this, or just do a tail (iOS 7.0.3 64 bits for example):
您可以使用 Console.app 来查看这一点,或者只是做一个尾巴(例如 iOS 7.0.3 64 位):
tail -f ~/Library/Logs/iOS\ Simulator/7.0.3-64/system.log
tail -f ~/Library/Logs/iOS\ Simulator/7.0.3-64/system.log
EDIT 2:
编辑2:
They are now located in ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
他们现在位于 ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
tail -f ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
tail -f ~/Library/Logs/CoreSimulator/<simulator-hash>/system.log
回答by Ben Hymanson
You can view the console for the iOS Simulator via desktop Safari. It's similar to the way you use desktop Safari to view the console for physical iOS devices.
您可以通过桌面 Safari 查看 iOS 模拟器的控制台。它类似于您使用桌面 Safari 查看物理 iOS 设备控制台的方式。
Whenever the simulator is running and there's a webpage open, there'll be an option under the Develop menu in desktop safari that lets you see the iOS simulator console:
每当模拟器运行并打开一个网页时,桌面 safari 中的“开发”菜单下都会有一个选项,可让您看到 iOS 模拟器控制台:
Develop -> iPhone Simulator -> site name
开发 -> iPhone 模拟器 ->站点名称
回答by Mark Bridges
回答by viteinfinite
iOS 8 and iOS 9
iOS 8 和 iOS 9
Under iOS 8 and iOS 9 this location is now:
在 iOS 8 和 iOS 9 下,这个位置现在是:
~/Library/Logs/CoreSimulator/<DEVICE_CODE>
So, the following will work:
因此,以下将起作用:
tail -f ~/Library/Logs/CoreSimulator/<DEVICE_CODE>/system.log
The DEVICE_CODE
value can be found via the following console command:
DEVICE_CODE
可以通过以下控制台命令找到该值:
instruments -s devices
回答by russbishop
You should not rely on instruments -s
. The officially supported tool for working with Simulators from the command line is xcrun simctl
.
你不应该依赖instruments -s
. 从命令行使用模拟器的官方支持的工具是xcrun simctl
.
The log directory for a device can be found with xcrun simctl getenv booted SIMULATOR_LOG_ROOT
. This will always be correct even if the location changes.
设备的日志目录可以通过xcrun simctl getenv booted SIMULATOR_LOG_ROOT
. 即使位置发生变化,这也始终是正确的。
Now that things are moving to os_log
it is easier to open Console.app on the host Mac. Booted simulators should show up as a log source on the left, just like physical devices. You can also run log commands in the booted simulator:
现在事情正在发展os_log
,在主机 Mac 上打开 Console.app 更容易。启动的模拟器应该显示为左侧的日志源,就像物理设备一样。您还可以在启动的模拟器中运行日志命令:
# os_log equivalent of tail -f
xcrun simctl spawn booted log stream --level=debug
# filter log output
xcrun simctl spawn booted log stream --predicate 'processImagePath endswith "myapp"'
xcrun simctl spawn booted log stream --predicate 'eventMessage contains "error" and messageType == info'
# a log dump that Console.app can open
xcrun simctl spawn booted log collect
# open location where log collect will write the dump
cd `xcrun simctl getenv booted SIMULATOR_SHARED_RESOURCES_DIRECTORY`
If you want to use Safari Developer tools (including the JS console) with a webpage in the Simulator: Start one of the simulators, open Safari, then go to Safari on your mac and you should see Simulator in the menu.
如果您想在模拟器中使用带有网页的 Safari 开发人员工具(包括 JS 控制台):启动其中一个模拟器,打开 Safari,然后在 Mac 上转到 Safari,您应该会在菜单中看到模拟器。
You can open a URL in the Simulator by dragging it from the Safari address bar and dropping on the Simulator window. You can also use xcrun simctl openurl booted <url>
.
您可以通过将 URL 从 Safari 地址栏中拖放到模拟器窗口中,在模拟器中打开它。您也可以使用xcrun simctl openurl booted <url>
.
回答by cprcrack
If you are using Swift, remember that println
will only print to the debug log (which appears in xCode's debug area). If you want to print to system.log, you have to use NSLog
as in the old days.
如果您使用的是Swift,请记住它println
只会打印到调试日志(出现在 xCode 的调试区域中)。如果要打印到 system.log,则必须NSLog
像过去一样使用。
Then you can view the simulator log via its menu, Debug > Open System Log... (cmd + /)
然后你可以通过它的菜单查看模拟器日志,Debug > Open System Log... (cmd + /)
回答by djibouti33
tailing /var/log/system.log
didn't work for me. I found my logs by using Console.app
. They were in~/Library/Logs/iOS Simulator/{version}/system.log
拖尾/var/log/system.log
对我不起作用。我使用Console.app
. 他们在~/Library/Logs/iOS Simulator/{version}/system.log
回答by yoAlex5
You can use the Console
application(select your device in Devices) on your Mac to see a log message that were sent using NSLog
(you will not see logs from print
function).
您可以使用Console
Mac 上的应用程序(在“设备”中选择您的设备)来查看使用发送的日志消息NSLog
(您不会看到来自print
函数的日志)。
Please note that if you want to see a log from WebView
you should use Safary -> Develop -> device
请注意,如果您想查看日志,WebView
请使用Safary -> Develop -> device
回答by Gurubaran
XCode > 6.0 AND iOS > 8.0The below script works if you have XCode version > 8.0
XCode > 6.0 和 iOS > 8.0如果您的 XCode 版本 > 8.0,以下脚本有效
I use the below small Script to tail the simulator logs onto the system console.
我使用下面的小脚本将模拟器日志拖到系统控制台上。
#!/bin/sh
sim_dir=`xcrun instruments -s | grep "iPhone 6 (8.2 Simulator)" | awk {'print $NF'} | tr -d '[]'`
tail -f ~/Library/Logs/CoreSimulator/$sim_dir/system.log
You can pass in the simulator type used in the Grep as an argument. As mentioned in the above posts, there are simctl and instruments command to view the type of simulators available for use depending on the Xcode version. To View the list of available devices/simulators.
您可以传入 Grep 中使用的模拟器类型作为参数。正如上面的帖子中提到的,有 simctl 和 instrument 命令来查看可用的模拟器类型,具体取决于 Xcode 版本。要查看可用设备/软件的列表。
xcrun instruments -s
OR
或者
xcrun simctl list
Now you can pass in the Device code OR Simulator type as an argument to the script and replace the "iPhone 6 (8.2 Simulator)" inside grep to be $1
现在您可以将设备代码或模拟器类型作为参数传递给脚本,并将 grep 中的“iPhone 6 (8.2 Simulator)”替换为 $1
回答by bompf
I can open the log directly via the iOS simulator: Debug -> Open System Log...
Not sure when this was introduced, so it might not be available for earlier versions.
我可以通过iOS模拟器直接打开日志:Debug -> Open System Log...
不确定这是什么时候引入的,所以它可能不适用于早期版本。