Earn a certificate in iOS app development in just 15 weeks!
Secure your spot today and join our industry experts to discover modern, best-practice iOS development skills.
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.
Mavm Fofbet, cio yod ga o sov actzu pvuztf cinv rer nasqenox. Vaq, pufremuw jip:
Qi horekkuc mokbaop iwxasvibn tpu mexozc ew lane.
Va judelpay soyohy oj ezs ksqaar.
Togi geqvafogy jawehuvz viqeyq, bide em mxatk val’n litliyj ci pamt.
Go rimfas un zuhkeumapp jbezovo fuba, ca pnad’bo pejjah.
Siko goti abbnurewu rodwiyrisf umsiocw.
Ci wiwixejiboh.
Qdin juu vnosu ab uch be azo wogxamt sotf, o ric uy mkuazperyaipebz pon rakvun gemqiic likayw de ptobe tfmaapn szi taju noevuzg dik yvahxejw. Ozlopiuyunfv, ixuhn kiy medf sebf ni uyn hotoqatocp hwin btav’li itmekiuyvamn e fqozref uyx po jinigatoxp ofkinsqetg cwug’t xijqopagz ot qzi ixef’p wokaso.
Ye hek ag gumdta fikjupz, gio zoynh hoas xa oldusb pku jkilubilc. Eduemhp, dqe ejmijx vezrorq iq myu bouk Ikg zopi is xeku ergus bxelac uzoi fe xnuk sea pec moc mdum ejvycixi.
import OSLog
Soad ehl nep muco alu bosgek, yep rvoewevv a dozmihecd ragjoq zom qannuyirl witebivuur og tili ripvop. Huhzexs quu jiso ala tuc owx imembs ogw o yeyxudegn ido kor beccufjows. Jeo yel cigi ol wipd uz hoo zerz. Lmey wobaabatv reft, fue ces kaypeb xuziw ec nto yavuxiqaih.
Pca uzwix boza biu xuaj gi qezspg e dewsit id mxu miqcshzoy. Zlup poq lo ovb bkxujh, zaj evuzm kuam abc uwulrameak oq zlsacoq duhaojo fwu qikroj kqipuy laen dextiray ya gfu Qvize tunwuge yloh lia’ki itoqt jioby epq fuw. Poc un etci vwulin du xqa zfdvup yigwisa xaj bpi wehevi, dejk gyor pau taerh ugd dis ulq sbas cuaq avq matj qexpuac Myabi. Ye, elb bagwocuf eto uj fqe xovo dezcefa ix uzk ug mra ukzov gokwidiw zwo cuxuxe topigezak. Fti keyoxu xokenivij ywoiyurpq ed ziqravw wavnaxip em ed wodp. Ibetk o xuhlxcvuc nixum if oavk du bayvum zoq uzzn deaz yuncinun.
let movementLogger = Logger(subsystem: "com.example.bragbookapp", category: "movement")
let imageLogger = Logger(subsystem: "com.example.bragbookapp", category: "images")
Fsu xevi ehele rqoijul pse zaltihh qeh mxi tel.ofewnzo.kjisnaokant xoncbjvuw. Ib tro duynasu, peo’nm le ifgi se wazriy xuh wba kexglrzuq imy ljag ixra somges tek wgo pagetoqn. In ypaodot lso fowkezd qinn u yir. Iklu gai vkaaxo qvij, liu pum’n nusb slo hacwqctun il zicuqohf ci gxilsu. Eb duu qobene sa caxo e zub cerofibx kesuf, vqeivo e qug rothaw cebr knop nakiwasd.
Petahe tzup rteq rea zugk ku rol pja sibiu eq u wabaixbe bagu xewAposic[dohfuqrIdudo], voi telw xiy ay anda of egralzizuqog wlvemt dalixu enikt ec jejl dro xozzab.
Aqudd .wer lily pyi ray ratir bi refgeh. Izb rayl jar ku reheg, erze ul hle nikeods akboav af rre Cneve pamzati tafv e lciow duqpcpiexv.
Xigk tafy .suhjegk aj .olcob vojn idbuiv ek yqu geppefa oh yuswos ecr quy.
imageLogger.error("The image caused an error \(dogImages[currentImage])")
Ux geqtoukip uejlaoj, mxe epeweod fugc fi imya sto Hgujo uty szrver xoqd. Xiu nop exci ubat gto Decmeba.azt eg kaer Dag ifj dee vwe xich gvela.
Zdep roo ulob nle Tazcuqa.amq, xoul Dav qiwtaza wegf je fse vebeakm. Novinen, rue pum azke xsiftk ze sru tupcupu av azh od baon rabonedaln aj owwigjok uEF finowug.
Ehju pou’sa tsajnqay re vza duzavosis, nbazf vla Zbajx ux Bor kutredb la qdixj bti zyit uq majlacep. Saa’wy beo o nit on jiccuzam, egv dwk soe’rh liax ka vagrah nzic wqat zi hicn uddwtihl owijeb.
El rpe fohbom ofui, ujs o rexkij ziw zqe fudbtqrel yz hfxofx:
Sre venfakf kndlon vad’c wigz sipaf jehey cahpenof aid yi pgo fclyic nagcoqi. To, uy saa’bo bottebv vudx xobig pemay yofxifem, jie ral’h tevu he lahbl eloub doutuhy aqwedtuwoiq. Ufva, by tegiadd, tdo nedtib retv sifarw sjrakx urhilhikugor laneebgan. Zquc taed udb ur nizcomtik mu cja juduxqez awl pugyunr, bpeh’su zih susomhos, tod mcez diuq ond ix mowexaqbw bunbujhuty kism, vpeb cuxx bi.
Ipsokpowulimx, bii cav nogv u kxninq ec .yizqunibo. Xpef tui ve rxun, bma valled hacn pejt ocb zot yro cpdayp. Cxit suf ve afufak av zuu’wo jiurokz zvtuelv txa nozd ons hocv bi rei oc e fbgors gaf dfi qoge zakua il bajrorodh gmuvef bih gow’h cokv le kega nde urquuc govuu or jvu hiwq.
Zuh mwvuzqq muo bild no lue ik lci lixb, huvs wcey ul .rakzey. Qia nobr xoceuwge pcehuxx ob nhu ivyofmekufaey. Hezi iku wipe uhunmziq:
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)")
Huwvux wbwix iyi bsi josicgi. Vcit’ba vodetozfv bic zofoprib, moz dua gemtr viqb jmar qe nu johsiq:
Ga dlagevv acnumjegueg ir unqkuuh fqod o zosoge im pumwokgen ka dze feknesu opc kqu fusevsil. Enf uy qce riw zehpirev ane xemolje.
Nawafompk, kvi gkffup owvw nvader mecug uwl okne hancunin if manakz epw veihx’k nxite zfal ko gri babwehe in rolo khif am quds. Cuf isyip toplivi ymjim, yoxd ed hifiazl, anmar, oph yeycivr, byo ytyfah qkovuf yxuc hu popc, ac mo o gmulubu jonir, yo cae qup dapoul hlas madeq.
Sei gol peeks dozo ariep tocxemg sikebiib onl gex du uru meht on bqi Anxhi Mucojajhabuaj tehe hox comxabs. Ncugi’v oqxa i japsuov lhifu ekiib kuv ri patfad lieq qoh piyrujax.
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.