asl_add_log_file not working

In Xcode7/iOS9 with the code below I was able to redirect the logs to an external file. Now with XCode8 with deployment target iOS 8.1, this seems to work only if I run on the simulator, on the actual device running iOS10 the file is empty. Is this a known issue? Is there a solution?


    asl_add_log_file(NULL, STDERR_FILENO);
  
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"logfile.txt"];
    [[NSFileManager defaultManager] createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL];
    int fd = open([path UTF8String], O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd != -1) {
        asl_add_log_file(NULL, fd);
    }
  
    NSString *message = @"message123";
    asl_log(NULL, NULL, ASL_LEVEL_DEBUG, "%s", [message UTF8String]);

Accepted Reply

Devs typically access the logs from Xcode console log in the Debug area.

QA typically has the iOS device plugged into a mac with Xcode so they can view the logs from Xcode > Window > Devices.

os_log
will do both of these. In fact, it makes the latter easier because you QA folks don’t even need Xcode installed: with iOS 10 and macOS 10.12, you can get logs off the device using macOS’s Console utility.

In addition though the app can log onto a file that can be shared via email etc.

This is harder. One option here is sysdiagnose. If the user takes a sysdiagnose (there are instructions for this on the Bug Reporting > Profiles and Logs page), it captures recent

os_log
entries and includes them there. However, there’s no way for your app to get at these programmatically. If you want this right now, you’ll have to wrap
os_log
with something that writes your own copy of the log entries to disk (and that will only capture log entries written by your code).

btw If you wrap

os_log
, it’s best to do so in a macro so that the
os_log
infrastructure can capture the call site correctly.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

ASL has been deprecated in iOS 10 and friends, replaced by the new unified logging system. WWDC 2016 Session 721 Unified Logging and Activity Tracing has the background to this. Right now there’s no direct way to achieve what you’re looking for, that is, to redirect log messages, including messages logged by other components, to a file.

If you post some details about your high-level goạl, how this fits into the overall workflow of your app, I may be able to suggest an alternative approach.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo! The logging utility that we are using is a wrapper around asl_log with log levels etc.. Devs typically access the logs from Xcode console log in the Debug area. QA typically has the iOS device plugged into a mac with Xcode so they can view the logs from Xcode > Window > Devices. In addition though the app can log onto a file that can be shared via email etc..


I just tested all this on iOS 9 and indeed all this works fine but like you said on iOS 10 logs are not longer written to file and I also noticed that the same log is printed twice in Xcode console log. Are you saying that the new unified logging system can't do this either?

Devs typically access the logs from Xcode console log in the Debug area.

QA typically has the iOS device plugged into a mac with Xcode so they can view the logs from Xcode > Window > Devices.

os_log
will do both of these. In fact, it makes the latter easier because you QA folks don’t even need Xcode installed: with iOS 10 and macOS 10.12, you can get logs off the device using macOS’s Console utility.

In addition though the app can log onto a file that can be shared via email etc.

This is harder. One option here is sysdiagnose. If the user takes a sysdiagnose (there are instructions for this on the Bug Reporting > Profiles and Logs page), it captures recent

os_log
entries and includes them there. However, there’s no way for your app to get at these programmatically. If you want this right now, you’ll have to wrap
os_log
with something that writes your own copy of the log entries to disk (and that will only capture log entries written by your code).

btw If you wrap

os_log
, it’s best to do so in a macro so that the
os_log
infrastructure can capture the call site correctly.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"