【问题标题】:How to read and store System Log messages in IOS using Objective-C如何使用 Objective-C 在 IOS 中读取和存储系统日志消息
【发布时间】:2016-01-01 09:10:09
【问题描述】:

我的目的是读取与我的应用程序相关的所有系统日志消息(所有这些日志消息都存在于 system.log 文件中),并将其存储在应用程序 Document 文件夹中的日志文件中。 我浏览了this code 并使用了this answer

这是我的代码:

NSDate *currentDate = [[NSDate alloc] init];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[NSString stringWithFormat: @"dd-MM-yyyy-HHmmss"]];

NSString *logFileName = [[formatter stringFromDate:currentDate] stringByAppendingString:@".log"];

NSError *error;
NSMutableString *content = [[NSMutableString alloc] init];


 aslmsg q, m;
 int i;
 const char *key, *val;

 q = asl_new(ASL_TYPE_QUERY);

 aslresponse r = asl_search(NULL, q);

 while (NULL != (m = asl_next(r)))
 {
     NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];

     for (i = 0; (NULL != (key = asl_key(m, i))); i++)
     {
         NSString *keyString = [NSString stringWithUTF8String:(char *)key];

         val = asl_get(m, key);

         NSString *string = [NSString stringWithUTF8String:val];
         [tmpDict setObject:string forKey:keyString];
     }

     [content appendString:[NSString stringWithFormat:@"%@",tmpDict]];
 }

asl_release(r);

NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:logFileName];

[[NSString stringWithString:content] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

这只是我的 IOS 应用程序代码的一部分。我在 IOS 模拟器上运行我的应用程序。

日志文件已成功创建。它有一些主要是 JSON 的内容。

但是,该文件没有我通过 NSLog 输出的那些日志消息,但这些日志消息存在于系统的日志文件中。

我对 IOS 开发很陌生。如前所述,我访问系统日志文件的目的。所以我想捕获每条日志消息并将其保存到我的个人目录中。

我在这个 Stackoverflow 上搜索了很多。但是,我还没有找到可以帮助我的确切线程。任何人都可以适当地指导我吗? (我希望这个问题不要重复。)

【问题讨论】:

  • 为什么不使用自定义日志功能来保存日志?
  • @SolaWing 你的意思是,我应该为此编写自己的函数吗?嗯,不错的主意。但我仍然想知道,我怎样才能访问 system.log 文件?

标签: ios objective-c logging nslog


【解决方案1】:

我猜你希望他们稍后检查它们以防你发现错误。也许你可以试试 Bugfender (http://bugfender.com),它是我们创建的一个工具,用于在开发应用程序时从客户那里获取日志。

有了它,您还可以获取所有 NSLog 调用。

如果您不想使用 Bugfender,您检查的答案是正确的方法。在我们的例子中,我们使用记录在 here

的函数

【讨论】:

猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2018-08-31
  • 2015-06-28
相关资源
最近更新 更多