New Swift logging APIs
Logs are:
- archived on device
- low perf overhead
import os
let logger = Logger(subsystem: "com.foo", category: "bar")
logger.log("This is my log")
logger.log("This is my log with data: \(someData)")Pass data into logs using string interpolation - like print
log isn't like print though...
- logs aren't always converted to String (slow) - only when displayed
- format is based on the type of logged data
Data types supoorted in logs: - Numeric (Int/Double) - Objc-C (description) - CustomStringConvertible
Nonnumeric values are redacted by default
- default to protecting PII
- see privacy section for more info
# fruta.logarchive is the "output" file name
$log collect --device --start '2020-06-22 0:41:00' --output fruta.logarchiveOpen logs in Console.app
- only during debugging
- not persisted
- fastest - compiler optimizes away unless streaming logs
- helpful, but not required for troubleshooting
- persisted for a short time
- required for troubleshooting
- persisted up to storage limit
- error during execution
- persisted up to storage limit
- bug
- persisted up to storage limit
- slowest (but still fast)
- Optional formatting parameters!
- Pass with data (like shown in privacy section above).
- No additional runtime cost
Privacy modifiers can be applied to logged data:
.public- shows info.private(mask: .hash)
Example of hiding PII in logs
logger.log("Ordered smoothie \(smoothieName, privacy: .public)")- New APIs on new OS versions
- backward support for string interpolation via overloads