源于iOS App开发时,控制台日志混乱,不便于分析的需要,产生了这篇文章。
基础票
说说NSLog
调用NSLogv => Logs an error message to the Apple System Log facility.
Output from NSLogv is serialized, in that only one thread in a process can be doing the writing/logging described above at a time.
STDERR_FILENO
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分类)
Apple System Logger
Standard output
standard error
File Logger
HTTP Server Logger
Telnet Server Logger
Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的能力。在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。
TCP Server Logger
UDP Server Logger
TCP Client Logger
UDP Client Logger
Onscreen Logging Overlay
Custom Loggers
Custom Logger fomatters
Basic Libraries Related(理清这几个库的关系)
_
GCDNetworking
GCDNetworking is a networking framework based on GCD available under a friendly New BSD License.
GCDWebServer
GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in OS X & iOS apps.
GCDTelnetServer
GCDTelnetServer is a drop-in embedded Telnet server for iOS and OS X apps.
_
CocoaAsyncSocket
CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for Mac and iOS. The classes are described below.
GCDAsyncSocket is a TCP/IP socket networking library built atop Grand Central Dispatch.
GCDAsyncUdpSocket is a UDP/IP socket networking library built atop Grand Central Dispatch.
日志实现用了CocoaLumberjack
CocoaHTTPServer(FMock工具用的这个)
CocoaHTTPServer is a small, lightweight, embeddable HTTP server for Mac OS X or iOS applications.
底层实现用了CocoaAsyncSocket
RoutingHTTPServer
A routing API for CocoaHTTPServer
底层实现用了CocoaHTTPServer
Guide(演示内容)
XLFacility --> HTTPServer
http://127.0.0.1:8080
XLFacility --> TelnetServer
telnet localhost 2323
XLFacility --> Onscreen Logging Overlay
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()复制代码
看看别人怎么玩日志的