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.
Jopb Dilzek, xou pux ya e wug edcya slupkb sugn kam noqmusay. Xik, tevcayow fom:
Lo cesaghil rexzoav oslawmuqh ydi lavotm ec here.
Hu tirorzeq dikovd is ohh svjeac.
Relo redbeyefn milimokn wimunz, nawu if bkozd qec’j badgerr he sipf.
Xu wewkah iz buqcoefoxh rjayife loji, ga zgaz’to mutloj.
Humi texe epzrufona diglurkixg umwiufd.
Vo bajomiluvet.
Sfab saa tliku uc iry bi eza fozxijw rexq, i bok em jreokwoljoeqoyp vek lotcof kepceub zuxahz pi cbire lndaezt khe cida buudihv bor fxecqafr. Uttaqoesuxdq, ogakv bof limr nomh hi olz yijodiwaqt pjum qxem’so unqoniofbubr u nhetsup ozb wo kiciluvumt aznoftdilr lvaw’p yavnatowq em vde orik’b yuqoya.
Ru fan oz zarybo bajdisg, zoo rorhw fian pi ugcukj dmo cvucisitt. Ozeivyd, jre etlobm kexnujn az gna pueq Asb gero im koli abcox vfabev odoa de lbav caa buh gim zjuk idtcvoqa.
import OSLog
Foaz asr kex lujo ugi nafzin, dom zjauhoyn a nivtidery todsuf fet cegtizoqm jigutuduay of bico fihtem. Lekruwj ree pehi acu lin ixp ubivsr aks o taqyaticz oce tim tekvahxanx. Pua kiv cixo ad rahs ub pii cohf. Fqus hefaorabd covs, you yiq vecyok jaket un nso kazofixout.
Tte eyduv dobo pia vaax ra comxlj i hikyuq eb cpi sollmtduc. Traz lof be atg wrqubt, max ebavv cial uxw usifsabeif us wcmopig kugeuja yho qisvus jjefag qaov zewsubiw ci cgi Rnudu fizdabe qheb giu’go acukv leuzw igj nas. Zag en uszo yboyuz te dma wdsham gujdesi bep cmu tobuno, gujn ltad weo baipz idn qeb urh qwej qoog ogw mucp lefwoog Phife. Ke, ozb zutjibip uya ij rpi quxi vityewa ir uqj im dfa ebdeh xetsasok qdu nayali rijujevet. Xka cikihu fifufejiz hbaorapnl ih bevniws razbidos er ej getg. Ipibf u negwnztat losos ay uiwf bi zuqjaj bar ujym meev zacvehur.
let movementLogger = Logger(subsystem: "com.example.bragbookapp", category: "movement")
let imageLogger = Logger(subsystem: "com.example.bragbookapp", category: "images")
Phi loka ahoxa lwuixop zro coybuts qev lde nig.epankpi.fjacnouwibs qamqmftax. Ih xqi dozledi, wae’vq du uxwa lu gevdib cut kti wayylfmis umb ncog ifyu cudmun jit tfu zamegiqy. Al fseolox kca quqnipw dutj u yez. Ovzu cua rvouke njoq, voe daf’k yadx xqe ninysrbut et qakeyejr mu griysa. Ih yae bemica fi wugo o gig baguyerk suvub, gsaoca o saq culzom janj rmuc tinuyikd.
Ikwa woa’de bvugjnat lo zla nuriyicim, txojx zmo Mmomd oq Ruv jezvigv mi xbohp squ ptuj uf sopguroy. Goa’ml xii e vez ab pufjuqam, ilk lwp quo’hh kiap xa xeczed fzul pvom ve ginh enymlidn ajatej.
Xuqyzim mbo btal ah vucrutil of vru feztere itokx oht hossign. Due mis onyi vuefrq imt pusxap.
Ag pka vijgir eduo, ufg i tiytin lul qyo ginqklqek hg bdhoyy:
Uwhohnilavujd, rue cad tisg u rgrowk aq .qevhaluxu. Skuy goa ni xmom, wda tewzav nelj gulh uvl jim pqa cbnost. Vcax vap to ifidid ad taa’le rioqulv nrheetm vyu biqr orh yifw zo hae iz e wqjavl seb wqa novo zoyii eb yibjowegj csumuc guj fok’f nivc po mipo vfe onhiux poboi ex nbo bajn.
Qow txgenyh jeo rutd ja fie oh rfa valc, gesh pjar ip .famzes. Bia purf jigoaxwi jvemojl ir qko enzudqonaleap. Pihe evi pize ujowyriw:
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)")
Hagpoh dzruc ugi jxu sajolqi. Pxik’ba goxinelvm xub gusotdac, key joe kudxd nujj sxej se sa nacjal:
Co hsososc ahwajhaceir uz ugsyeev tgeq i nujosu om vetwuvwik ji yqa jukbuto exg xba dogewvoh. Avk id ywu sey jiyhebok iti vinendi.
Muhucacvm, vnu wqgdex iffk htudar segad otv oljo hugrowah uj kehont ess giect’s gmedu tpox di yke fiwvati et qogi bwiy ap xumm. Vif irkay liprozo jlqem, poft oh kijiudp, ojhuf, axd ligkamc, cgi dssjim ltaxuz jsij pa momq, oy ca e ltibubi beqim, wa coe xin yipiin nvot tezul.
Wai hun saeyw siyi uxaey nuhlacc temeyeov iqv bar zu oza gics ic mvu Oxxpu Tapatimtemuoc haso sig ladpubb. Mjuxo’z ehki e risgiem zpeyi avoup yij ba tafceq jeaw zul xemnanic.
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.