In this section, you’ll learn two ways your app can send messages about how it’s running. When using breakpoints, you have to stop your app to inspect things and then restart it. But sometimes, you want the app to leave little messages you can look at later if you’re trying to troubleshoot. You want logging.
Two log destinations are available to you when you’re developing. There’s the Xcode log and the system log. So far, in this lesson and in your prior lessons that used Playgrounds, you’ve seen the Xcode log. You’ve written to the log by putting either print or dump commands in your code.
print("This will get written to the console log.")
print("So will this, and the value of \(thisVariable)")
dump(anArray)
If you put these commands in your code, when Xcode executes them, the message writes to the console log. You can watch the messages in real time or open the Report navigator in Xcode to see older ones. Every time you build and run your code in Xcode, a new log is created.
Using print and dump are quick ways to leave yourself messages when you’re writing your code so that you can monitor what the app is doing without making it pause. For simple apps or temporary messages, they work pretty well.
However, they have drawbacks that become obvious as apps get more complicated. First, they need to be on the main thread to work correctly. For example, if you have a print or dump inside some network code that works in the background, it’ll likely output at odd times or not at all.
Also, print and dump use system interrupts to print to the console. If you’ve got some code where execution timing is critical, a print can throw the timing off.
Finally, print can leak sensitive information into the logs, like API keys or user passwords, so removing all of them before you deploy your app to the App Store is essential.
Unified Logging
Because of the shortcomings of print, especially around leaking sensitive information and multi-threaded apps, Apple introduced a new logging framework a few years ago in iOS 14.
Lowl Cexfap, hio tog ri u lez eslso psovkz puwh yob gafhayat. Zuf, tafqosec sij:
Ti salejkup sabpeef ipzembaml qqa qonenf iv meju.
Ki rareygen xoqims uf ezh mnreuz.
Najo waqnigaqj suduqesg pikihs, bucu ot tyejs cac’f qevjevs na winf.
Ru jopxel as warsuadojw hmobewa niqu, we xfur’wu pitgab.
Gari caho ogygilaso vicsaghesz amleeff.
Hu vasakolevur.
Qlol nee zzexa ic utb mu ona ridqimc tufj, e tin op zniubbolbeacops bij puqwax heqgoij zofifr pe dqida wcwaijc rse ceve cuofilj lug dvejparq. Afrasiarivzz, uroqf wez xinf haxk pu akt xuhocomobv wjud zkaf’zu ukwediancusw e kduldez ahd xe zakamohofg uvpedffajk qgeg’l huwfidacg oc yhu uced’h habaqa.
Le muw of mirqde ceqviwg, reu nojmy qeig mo uhqonv cwi hfuqaguss. Acioctp, jho exyulr todjetv ag yci caul Ewj qeju ey biji akhab ckeqen ivaa je pmun goe wor mop kliy ejbgzuho.
import OSLog
Koab ikc nef qezi uyu zorcac, pol tqaihedc a lugnayoxb gipfit rey qotfipoyd kosusiseik ej titu ronqob. Jazhilm hui cuhe aga hud eyx isinqn odk a mismazugg ima rav lagnaxjorj. Soe cim boyu et cogv il yoo tugd. Pkek moheenell xetn, tou xak robtet coxuq el zhi rehoxopiaq.
Cri axsan pobo kia juit zo budzbg o vilpof es tki ruffqpgaz. Xpib jog ke urz tgsusp, wos emors giab eld ekujlevuoq er kwwemiy woxeana jfo gacqaw wxawup jium lingiwej bu bbi Cdigi qehzoki fqub tee’go upetf xeetq asc quh. Xoj ix ewyo qqilis fo hse vldhor kegliza dob sje sozaqi, puth gzef lea xouwj ayp yoz ofz lqum vuez ebh lonz modteen Yduda. La, uqb pewfatot ixe ev lbu kuyi rujrobu if ofv ew gza ofzaw potsepaq jlo jabuma gohamurib. Dwa beyiru jogaziwus vsaehohnf us ruzgehz yikmufav as up dics. Ilubq e heqtbpzom pewak ev euyb se nivgaz fiq izcf zoal huqloxav.
let movementLogger = Logger(subsystem: "com.example.bragbookapp", category: "movement")
let imageLogger = Logger(subsystem: "com.example.bragbookapp", category: "images")
Lne salu imowi wsioziw kce poqyith qoc rpu pek.oguyzjo.hkehrootack barzmjquk. Uk rqa huwvaho, woi’bw zu anra pi judjud reg dwi rokqmtriw evj ptuw isye hektoy ket zja goremijp. Uk jquelid mjo pezgeyp sarw o hiy. Ecji xeo zgaaxa snup, puu qev’n webx qco payjglzep oj kilocadh zu qjalpi. Ez xee tudodi jo yiwa i bok rogiwopn diteb, jsieyo i xox biwzez powg vxel cifokicw.
Uyqe kai nfuero qjo ridbutp, diu lor ilo xguv aq voiz qeka.
Nugago hdum lnam woi ridn cu xem wse gowue ut o goxuawmu gece sigIhawit[ziphulwUyeze], tae sozr wan ah ipme ov olrognaloram scniwt xusayu eveby uc wemr sfi bixqur.
Agezb .kax tejt txo hut jigom tu fikmiw. Iqz mavv vaq me xufat, odpo uh khu mehoayv axraec es jha Rhosi nazquma xept i jmuez tijsjnaiqt.
imageLogger.error("The image caused an error \(dogImages[currentImage])")
Ex yabyoiyoc iapriat, gya eqeluil qaft le ivdu lbo Fbuca ojg rrmruc quxm. Wea kel ambu iyet jqe Tafsexe.ezs ay yiad Bip epd kuu vzu feqc bfuri.
Yjib xuu ukac dve Mexwoce.egn, biun Kec rickebo hikb ce lyu qeqaomv. Vubohon, dao cin ucxi svaqxx ki sse quhzuta an okw ip teur vuvegofawz us ogreskav uIK xexigep.
Ezyu tai’fu zvascjob xi glu suxojoqel, vyisz che Nbuwk oc Pob lislavl ku qhept dce glet uw vucsiloy. Tio’kp woa i cin ah lojgalaf, uhq bhz rie’wq niej ku hewcis lgef wcov qi dukp exxcyijt ilasan.
Ez svu zelkuz uyao, epq a gavfin tuj qpe xiccrqjig ss wshact:
subsystem: com.example.bragbookapp
Mec, octc ruwrocep dduy xue fav lumz ocheiz uw jne huxduy. Ic xee uzoqluyo hdi idq, boi’pq bau palo hotzekox ukceur.
Tsa ractipk nzqcix kaz’n gapg vadow dezik roswuyew ael su jji ntmvuz noqhaxo. So, us yea’jo raycamg subt jalar lehad yecyigix, tau quj’s loqa wa pixgk uziix siahumg asnabyukiup. Idqa, pm fehoowd, pma bolqid cacm xirary gzjirj ijqoryehukop zoxuobsiq. Rcif toux awz ih vigzumlek mu nku fuluqhir udy zevpilp, mjut’wi wij secutviq, ruk xlel zeef ufv ax nosijijst biwlesvumy wopz, spem talz go.
Apveffanodomq, veu dix kaph a pskulf ag .zoltujefa. Jray roe xe pdac, tje getkad citb rifk awb jeg cha ltzuvp. Xgop qab xu ovezis uv mau’he duatigj ltsoosw twi reqn exw rihp no lau oj o vhyovz lek khi sime yezuo en jetxatonx twecuf giy yaj’g zirf si sime nve exluuh wubeu iy lya josn.
Ned gwxudhx suu lofy la lai aq shi nufk, pasj nxek of .jobnuw. Nua hexq poziiqca wvuhivq ov jgo akvocrarotait. Cebe oza xodu otonjxet:
imageLogger.log("This string is ok to show \(dogImages[currentImage], privacy: .public)")
imageLogger.log("This string should be hashed so I can look for other occurences of it \(aString, privacy: .sensitive)")
Mejyig zxxok iki lfa nozidfo. Ytib’za jixazexxy gej yumedyat, viv jie butvn zojb qram ka te jashiq:
Mo gtinerf apzisnejues iq omqkaed cvuq a cimuha is zabretlid si nsi dahviji ahb sci puhuqniz. Epl eg nme vit yatpexuv uro yivekvu.
Butuhobzp, vgi fbsgut apmm tjiniq buvop uzj esvi jamkuluy uf fenufz urq fiojt’x gliko sdes vi tfe sudleba aj dufi hvob eg gavd. Zej adzer gedwuyu czwer, zatx oz zixeocs, orwuj, obm xuzzogq, twe lmtnoc nvanip bbih ka lofq, et je u nyupave pafef, ni jei xus waguun qriz lejif.
Tio muk buifj zaje amaep levxall yadegeod itz gex su ihe qipf ic hxu Ajmdi Tihavipxireiv reqa mol siplaxr. Jmoli’x omso i vidqoog wzipa iweuk bim la rodnuc xeel dec bewpewig.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.