Teleport-NSLog 在您的应用运行在用户设备上时捕获 NSLog 消息,并将它们发送到指定的后端服务器。
当我们在 Xcode 中调试时,我们使用 NSLog 在控制台打印大量的有用信息。如果我们在应用在用户设备上运行时能访问相同的信息,那就太好了。
正是这种需求促使我编写了 Teleport-NSLog。
Teleport-NSLog 将 stdout 和 stderr(NSLog 将消息写入的地方)重定向到后端服务器(聚合器)。Teleport 包含了一个基本的基于 HTTP 的聚合器 SimpleAggregator。Teleport 可以轻松扩展以发送到其他聚合器,如 logstash、Fluentd、Logentries 等。
您可以使用 Linux/Windows/Mac 作为聚合器。唯一的要求是,该服务器的 8080 端口需要可以从互联网上访问。
安装 GO。如果您在 Ubuntu 上,只需这样做:
sudo apt-get install golang
将 Teleport-NSLog 克隆到聚合器服务器上
git clone https://github.com/kennethjiang/Teleport-NSLog.git
编译和运行聚合器
cd Teleport-NSLog/SimpleAggregator && go build aggregator.go && ./aggregator
在 AppDelegate.m
中
#import <Teleport.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// TELEPORT_DEBUG = YES; // Uncomment this line if you want to redirect NSLog even in Xcode
[Teleport startWithForwarder:
[SimpleHttpForwarder forwarderWithAggregatorUrl:@"http://hostname_or_ip_addr.of.your.server:8080/"]
];
// [...]
return YES;
}
登录聚合器
所有捕获的日志文件都存储在 Teleport-NSLog/SimpleAggregator/logs 中,以设备 UUID 为文件名。
cd Teleport-NSLog/SimpleAggregator/logs && ls
**注意:Teleport-NSLog 在应用在 Xcode 中运行时不会重定向 stdout 或 stderr,因此 NSLog 仍然会将信息写入 Xcode 控制台窗口。要覆盖此行为,取消注释 // TELEPORT_DEBUG = YES;
。
默认情况下,Teleport-NSLog 仅在应用在用户设备上运行时将 NSLog 消息发送到聚合器。在 Xcode 中启动应用时,它不执行任何操作,因此聚合器将收不到任何信息。
这是一个经常被问到的疑问:在发布我的应用之前,我该怎样测试聚合器呢?答案是:不需要,因为SimpleAggregator已经能够正常工作。但如果你坚持要测试,或者你使用自己的聚合器,有2个选项可供测试聚合器
为你的应用创建一个发布版本,并使用HockeyApp或同类服务进行安装,或者
开启TELEPORT_DEBUG = YES;
。在发布应用之前,请勿忘记将其关闭。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TELEPORT_DEBUG = YES;
[Teleport startWithForwarder:
[SimpleHttpForwarder forwarderWithAggregatorUrl:@"http://hostname_or_ip_addr.of.your.server:8080/"]
];
// [...]
Teleport-NSLog可以很容易地扩展到发送到其他聚合器。为此,你需要在Forwarder
协议中进行实现,这是一个只有一个必要接口的简单协议
@protocol Forwarder <NSObject>
@required
- (void)forwardLog:(NSData *)log forDeviceId:(NSString *)devId;
@end
你可以使用SimpleHttpForwarder
作为参考。
要运行示例项目,先从git仓库克隆,然后在示例目录中首先运行pod install
。
Teleport-NSLog受MIT许可协议的许可。有关更多信息,请参阅LICENSE文件。