Read logs using the new swift os_log api

落花浮王杯 提交于 2019-12-20 09:38:23

问题


Deprecated in iOS 10.0: os_log(3) has replaced asl(3)

So iOS 10.0 apparently deprecates the asl (Apple System Log) api and replaces it with the very limited os_log api.

I use something similar to the code snippet below to read out log writes for the running app to show in a uitextview in app - and now it is full of deprecation warnings. Does anyone know of a way to read the printed log using the new os_log api? Because I only see an api for writing (https://developer.apple.com/reference/os/1891852-logging).

import asl

let query = asl_new(UInt32(ASL_TYPE_QUERY))
let response = asl_search(nil, query)
while let message = asl_next(response) {
    var i: UInt32 = 0
    let key = asl_key(message, i)
    print(asl_get(message, key))
    ...
}

Edit after @Will Loew-Blosser's answer

https://developer.apple.com/videos/play/wwdc2016/721/ explained nicely what is going to happen with logging in the future. The biggest giveaway was that logs are put in some compressed format and only expanded by the new Console application. Which pretty much makes my mission hopeless.

The guy (Steve Szymanski) in the video mentions "All ASL logging APIs are superseeded by new APIs" and "New APIs for searching new log data will not be made public this release" aka asl_search. And that was exactly what I was looking for!

Also he mentions that a swift API i coming.


回答1:


Looks like you need to use the enhanced Console instead of your own log viewer. The logs are compressed and not expanded until viewed - this makes logging much less intrusive at debug levels. There is no text form of the logs however.

See the 2016 WWDC video session 721 "Unified Logging and Activity Tracing" https://developer.apple.com/videos/play/wwdc2016/721/

Also the Apple sample app that demos the new approach has an undocumented build setting that I had to add to my iOS app. See the setting in the 'Paper Company (Swift)' iOS app. The setting is found in the Targets section of the top level xCode window. These are the steps that I followed:

  1. On the Build Settings page add in "User-Defined" a new section = ASSETCATALOG_COMPRESSION.

  2. Under it add two lines:

Debug = lossless

Release = respect-asset-catalog

After adding this build setting then logging worked in my app as per the video session demo.



来源:https://stackoverflow.com/questions/40272910/read-logs-using-the-new-swift-os-log-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!