31

How can my iOS application read messages from the devices console log. I want to programmatically read through these log entries (like reading a file?), select some, and email them to support.

I'm aware of one iPhone application which lets you view the log. It's name is Console. However, I can't figure out what classes or APIs he used. One person suggested it was done using ASL functions, but I don't know what these are or where they are documented.

I'm also aware of several alternatives to using NSLog such as NSLogger and CocoaLumberJack, but we aren't ready to implement these at this time.

Thanks very much for any help!

Richard Brightwell
  • 3,012
  • 2
  • 20
  • 22

4 Answers4

28

This entry in the Cocoanetics blogs has sample code to access the system log on iOS using the ASL (Apple System Logger) API (man page):

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

q = asl_new(ASL_TYPE_QUERY);

aslresponse r = asl_search(NULL, q);
while (NULL != (m = aslresponse_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];
    }

    NSLog(@"%@", tmpDict);
}
aslresponse_free(r);

Note that you need to poll ASL to read the latest messages. The code above will also fail when ran on the iPhone simulator, but works just fine on an actual device.

If you don't want to fight the C ASL API, have a look at this Objective-C wrapper called ASLogger.

Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
  • 2
    Actually there is a problem with this code. Most of the messages which are written to the console on iOS are marked as sensetive. And only system apps can read them. So, you will end up being able to read only your own logs. – Victor Ronin Oct 16 '12 at 22:55
7

As of IOS 7, this method won't work any more. Apple removed access to ASL due to security reason.

Trung
  • 1,655
  • 2
  • 18
  • 26
  • Really? In IOS7, I can still read messages from ASL which are wrote by NSLog. But for messages logged by DDLog, it cannot work on a real device. – Andrew Nov 26 '13 at 13:38
  • 14
    In iOS 7 you can use ASL to read messages output by your own app, including on previous runs of the same build of your app, but not messages output by the system or by any other apps. – sup Jan 03 '14 at 23:07
  • Thanks sup. Spot on. You answered before I could. – Trung Jan 06 '14 at 17:35
3

Here's a Swift implementation if anyone's looking for one:

static func systemLogs() -> [[String: String]] {
    let q = asl_new(UInt32(ASL_TYPE_QUERY))
    var logs = [[String: String]]()
    let r = asl_search(nil, q)
    var m = asl_next(r)
    while m != nil {
        var logDict = [String: String]()
        var i: UInt32 = 0
        while true {
            if let key = String.fromCString(asl_key(m, i)) {
                let val = String.fromCString(asl_get(m, key))
                logDict[key] = val
                i++
            } else {
                break
            }
        }
        m = asl_next(r)
        logs.append(logDict)
    }
    asl_release(r)
    return logs
}
Harlan Haskins
  • 557
  • 1
  • 6
  • 9
  • Thanks but I still can't capture the logs sent by swift's "print", it only captures the ones by NSLog, any idea ? – Jimmar Oct 05 '15 at 13:58
  • 1
    @JiMMaR You won't be able to capture those. Swift's print skips Apple's logging facility entirely. If you want those logs to go to Apple's logging facility, use NSLog or make your own wrapper around both. – Harlan Haskins Oct 05 '15 at 18:15
  • 2
    This is great, but now if only we had something that was compatible with iOS 10 and the new os_log. You wouldn't happen to have that, would you? – John Bushnell Oct 07 '16 at 23:49
  • 1
    @HarlanHaskins This is not working in iOS 10.2 , Any help. – Madhu Jan 19 '17 at 11:09
  • I haven't found a way to get this working in iOS 10+. – Harlan Haskins Mar 28 '18 at 17:30
0

The older way people suggested work perfectly till iOS 9 but that will not work with iOS 10/XCode8.

There is a new way of logging introduces in iOS10/XCode8, Unified Logging and Activity Tracing.

https://developer.apple.com/videos/play/wwdc2016/721/

User this for logging:

https://github.com/SRGSSR/srglogger-ios

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Nilesh
  • 1,493
  • 18
  • 29