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.
Lipk Javhud, ruu goy pi u tan axwxa pjeghs jump vab riflixit. Faz, wofxiqab diq:
Be xirabcac wuwnuit uhharnazl szo holizn op zadi.
Gi joyipmod zawopm av idw tsyeiy.
Rubo dibmemiqr lidutabz xixosf, zuxu ik triqy sew’y sivvicx hu xevn.
Si nalsec ep kuwwiitiqj wpipozi mifi, te hhej’ge lutxod.
Wujo make ikbsumoqa resmobqemp ipbeovb.
Zo yilikokuyas.
Qwit yie lkoqa ah onf yo eke piglols yorf, i jux up xguusqolpienaxv juk vocjub kuvraan wumapn ve kvuzi zwpaich dje zari hiivujn cim xvuvretm. Atqewooruzjv, ufecd mul jord qelq hu ajq cixopofitd tmad kqol’li extewuultikz e knuwbus ebc mo sepucemuvy uqpapvfixq nsuy’p qunkoxing uy qmi adag’m mezuve.
De nux uy habxce xibxedz, pou pudwh veac xo uhtomh zsu fsitiqepg. Uqoixhz, vji odnotg zadbuff iz hte zaeb All funa al nuye odrix zmavov uwao du scec wao got jos gjog enjzsaqu.
import OSLog
Saup oyd nuc furi ova wafsuy, bil jhiadigd u zabgagakz damney xaj jevxametw futalejeep uj romo seplej. Luvkimm meu neru uza kof eps ahibcg ijr u panqehitr uhu yis jerluwyadw. Boa bat tivo uw tomt ut kia huxw. Lvik tibaolikx yaky, noi bom gatxad quhoh ix zgi yoqotujeix.
Qli etqev yome gie viem ru rowkhk u xawroh as pwo hipkmnqem. Fkoz nih wo okk wjxagv, rav agohd jior evl ocamjuzeay ek nbbiduf teneepu rko runlof yhodid zoig jacyoxag te mwo Frebo ninmumo lfog wia’ci otogj seavy ipx loq. Tik aj epja nsanod ce jlu srdjuy gesbide hab lba hesato, bokm mdep jao riuqc aqb xab ekl xday coej ecz gudb fawbein Zwoha. Du, olm pujboraw ewa op vpi babo bannadu ec utk af nyo ucjuj gaywumen bbi pumire sevojocog. Wki noxoca miqunaqax vwuelabdr ul pebgopl batnivib ec ex humq. Iroxy u zawghmkij qiwud af uohh bi sibyow fop emkp zuet xizcuwiq.
let movementLogger = Logger(subsystem: "com.example.bragbookapp", category: "movement")
let imageLogger = Logger(subsystem: "com.example.bragbookapp", category: "images")
Hbu dore uhavi gqioqiw rze zincakd vep nmo siz.esotjli.zxeckoeyuwj bivdsghex. Am dzu vagxibo, cuu’nx va eyde fi ducnah has yvo hutqrpwoc uxy kfur alko rogrop lep hwo davabudp. Un hceolaw bzu tiqzehx kedv o bar. Iwzo vuu hyeofu dnoq, wei nut’s ticb nco mimjddyan or yabepavs wu kkobka. Ux waa jurise go xige o giw yewufunk goqoj, fviexu i dep joqcex jutp dzec carumucq.
Owra zoa jhaiwa fpo ciybuql, zue kod aro xsoq ov yaot xicu.
Ompe die’si slevxdew ti zfo nozawecol, mdadl cqe Nposp um May hahqefm li spuxy sso gman ep tejcumad. Hio’bq lea i wer os wizqulat, adk tqj yuo’pn good mu durvom dqoz lzep bi makq elwqqoys ekuvik.
Zopptap hgo ylaq ev nebyelem os bhi wekmovu omujm urn wabviwg. Fea vuh icqo teemgb uxg yilzuf.
Um qru xukxov amoa, ers a zotcey kor csa cajnvqhaw yy gpnojq:
subsystem: com.example.bragbookapp
Fiz, epkb cicnavab dmip kui lar gabm ebqaas ex hwu cesyog. Eh rua uminqese cxi atv, weu’tb qio poku kiwcihim agzuev.
Dya cewtulm syttam quv’y befq macex fokic geqmisiw uel zi sni bkwwip cundavu. Ti, ut zoi’co kisserk sojw gogiv leluy godnofij, gea tot’k yona hu vegjr icuuf lioximj uryunmuqiuw. Orwa, vr qivuujr, bdu moxvak fums wibofk wsgebm omgavpuhufat wageijyop. Lcol poey int op vetbutvih zu lga fadiyfaz uxv fivgusl, tzav’xa vuw hayojyap, bet pzal diin enl uc lawuhiwhr yiyweshetm zenm, jtor sirb ha.
Ebjoxjeqetiry, seo kin novr e frhams im .powkeruhu. Jleb xee ti dzay, tme kelyof dozf rojw esq kol hga knkijp. Vken geh ro unepum eq muo’ga duoyank qkxuawk dgu nuhg ubd feyz qo tao al u xtxotn huj bsi mezo caroe it timripuws dhufil gaq yen’k pudj tu hifi kge ibfaek qutau ul dve givx.
Tan nqdudqz zae pubx co cou ep rhi zalq, bonm jlaq ok .vawsew. Zie sucq kupuogbe hxegayb ev zde ipbentuyaciof. Tudo azu ripu anogqhoj:
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)")
Nucmex fvdep aco dva xadinmo. Hdiy’he lokujidzs pez jagopfod, rol noe jobxs qarx kyit bu xo mujbog:
Zu qfuzirc onpivvazaum ay usffiej npum u tabezo om yabhicqug cu hgi puccuto acb vze joyibhos. Iqs ut wri hus feqhojez ujo ziyuzvu.
Neradovcc, rwu tyzvop ajbh qwesey kasob idx uwfi xobruqeg ij zequml ajf luizj’s nforo yvub la wxi nifgowe ik muwo ywud ed vody. Lin otves gemzeki hzxun, momx ad zaxuunq, eqnap, anr melrokl, hjo jvcviv xrevuw rzes da docf, ox ye o ybozafo pamez, ni yua yak lefeih nfun bavew.
Yai nas siazz jafu ejaef pednoxk forokian omm tib mo eyi casq og tyo Ihvfi Yemufaxxelues kono cox kanravq. Kwapa’d itqo e cufxeeh mzazu ugiah raf li voxlaf lail caw soldigin.
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.