You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func demonstrateLogLevels(){
// .debug — Verbose developer info. Not persisted by default. Not collected in production.
Logger.network.debug("Request headers: \(headers)")
// .info — Helpful but not essential. Persisted only during log collect.
Logger.network.info("Starting request to \(endpoint)")
// .notice (default) — Essential for troubleshooting. Persisted up to storage limit.
Logger.auth.notice("User signed in successfully")
// .error — Error conditions. Always persisted.
Logger.data.error("Failed to save context: \(error.localizedDescription)")
// .fault — Bug in the program. Always persisted. Captures calling process info.
Logger.data.fault("Core Data stack not initialized — this should never happen")}
String Interpolation Privacy
func logWithPrivacy(userId:String, email:String, itemCount:Int){
// Default: dynamic strings are PRIVATE (redacted in production logs)
Logger.auth.info("User logged in: \(userId)")
// Output in production: "User logged in: <private>"
// Explicitly mark as public when safe
Logger.ui.info("Screen loaded: \(screenName, privacy:.public)")
// Mark as private (default for dynamic values)
Logger.auth.info("Email: \(email, privacy:.private)")
// Hash private data for correlation without revealing values
Logger.auth.info("User hash: \(email, privacy:.private(mask:.hash))")
// Numeric values are PUBLIC by default
Logger.data.info("Items count: \(itemCount)")
// Format specifiers
Logger.payment.info("Amount: \(amount, format:.fixed(precision:2), privacy:.private)")
// Boolean and numeric types
Logger.network.debug("Cache hit: \(isCached, privacy:.public), size: \(responseSize) bytes")}
os_signpost for Performance Profiling
import os
// Create a signpost log
letpointsOfInterest=OSLog(subsystem:"com.yourapp", category:.pointsOfInterest)letnetworkLog=OSLog(subsystem:"com.yourapp.network", category:"Requests")
// Mark an interval (begin + end)
func fetchData()asyncthrows->Data{letsignpostID=OSSignpostID(log: networkLog)os_signpost(.begin, log: networkLog, name:"FetchData", signpostID: signpostID,"URL: %{public}@", url.absoluteString)let(data, _)=tryawaitURLSession.shared.data(from: url)os_signpost(.end, log: networkLog, name:"FetchData", signpostID: signpostID,"Received %d bytes", data.count)return data
}
// Mark a single event (point of interest)
func userTappedCheckout(){os_signpost(.event, log: pointsOfInterest, name:"Checkout","User tapped checkout button")}
// Modern signpost API (iOS 15+)
letsignposter=OSSignposter(subsystem:"com.yourapp", category:"Performance")func modernSignpostExample()asyncthrows{letstate= signposter.beginInterval("DataLoad")letdata=tryawaitloadData()
signposter.emitEvent("DataReceived","\(data.count) bytes")
signposter.endInterval("DataLoad", state)}
// Automatic interval with withIntervalSignpost
func automaticSignpost()asyncthrows->[Item]{tryawait signposter.withIntervalSignpost("FetchItems"){tryawait api.fetchItems()}}