iOS

iOS 日志分析

2017-03-17  本文已影响244人  码农二哥

源于iOS App开发时,控制台日志混乱,不便于分析的需要,产生了这篇文章。

基础票

说说NSLog

iOS日志重定向到文件

{
    //to log to document directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *loggingPath = [documentsPath stringByAppendingPathComponent:@"/mylog.log"];
    NSLog(@"%@", loggingPath);
    //redirect NSLog
    freopen([loggingPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}

这样我们就可以在电脑上面直接tail -f这个文件,配合grep等命令进行日志分析了。

不进行日志重定向

iOS模拟器的日志其实是输出到系统中某个文件中了,好像是定时压缩一个保存起来,我们只要找到那个文件就可以tailf了。


system.log文件在哪儿.png

根据当前模拟器的identifier(windows-devices菜单)按图示找到当前模拟器腺癌的system.log文件,右键finder显示,然后直接tailf就可以啦。

如何更方便呢?

To give yourself faster access to logs via terminal, you can add an alias to your .bash_profile file by typing:
echo "alias simulatorlog='tail -f ~/Library/Logs/iOS\ Simulator/7.0/system.log'" >> ~/.bash_profile
After it you can simply type simulatorlog into terminal window to access the logs.
像我自己的电脑,是这样配的(查看iPhone6的日志):
alias mylog6='tail -f /Users/test/Library/Logs/CoreSimulator/0A5E599A-D202-4502-8E3C-B6E97840E7ED/system.log'
查看日志时,直接mylog6 | grep something
Enjoy.

============================分割线===============================

分享篇

下面是组内分享时用的md文件,是一个大概的总结性的东西,虽然没有详细的展开(分享时口说呗,写起来好累赘的样子),但感觉对于理思路还是挺好的。

Basic Loggers Related(主要是Logger分类)

Basic Libraries Related(理清这几个库的关系)

Guide(演示内容)

  1. XLFacility --> HTTPServer
    • http://127.0.0.1:8080
  2. XLFacility --> TelnetServer
    • telnet localhost 2323
  3. XLFacility --> Onscreen Logging Overlay
  4. CocoaLumberjack --> UDPClient
    • start udp server
    • test udp client logger
    • 下面是一个简单的udp server代码:
#!/usr/bin/env python
# UDP Echo Server -  udpserver.py
# code by www.cppblog.com/jerryma
import socket, traceback

host = '127.0.0.1'
port = 8888

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

while 1:
    try:
        message, address = s.recvfrom(8192)
        print "Got data from", address, ": ", message
        s.sendto(message, address)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()

看看别人怎么玩日志的

上一篇 下一篇

猜你喜欢

热点阅读