1

My intention is to read all the system log messages pertaining to my application(all those log messages present inside the system.log file), and store it in a log file within the app's Document folder. I went through this code and used this answer.

Here is my code:

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];

This is just a part of code of my IOS app. I am running my app on the IOS Simulator.

The log files gets successfully created. It has some content which is predominantly JSON.

However, the file does not have those log messages which I outputting through NSLog, yet those log messages are present inside the System's log file.

I am quite new to IOS Development. As stated, my purpose to access the System's log files. So I want to capture each log message and save it into my personal directory.

I searched a lot on this Stackoverflow. However, I haven't found the exact thread which could help me on this. Can anyone guide me appropriately? (I hope this question isn't duplicated.)

Community
  • 1
  • 1
Archit
  • 976
  • 16
  • 28

1 Answers1

1

I guess you want them to check them later in case you find an error. Maybe you can try Bugfender (http://bugfender.com), it's a tool we have created to get the logs from our customers while developing apps.

With it, you are going to be able to get also all NSLog calls.

The answers you have checked are the right way to do it if you don't want to use Bugfender. In our case we are using the functions documented here

ventayol
  • 635
  • 4
  • 16
  • 1
    Note future reader that the here referenced `asl` functions are deprecated as of iOS 10.0 and are replace by the `os_log` functions (https://developer.apple.com/documentation/os/os_log) – Bruno Bieri Dec 15 '20 at 15:28