With iOS unified logging, a boat-rental app's collected logs show every renter ID as <private>; why, and how do you keep one renter's lines correlatable without revealing the ID?
answer
- default privacy decides by type
- strings hidden, numbers shown
- public, private, sensitive, auto
- a mask instead of the placeholder
- hash is unique per process
basics
~20 sInterpolated strings default to .auto privacy, which redacts dynamic strings, so the ID appears as <private>. Log it with privacy: .private(mask: .hash): equal IDs then share a hash, which correlates lines without revealing the value.
solid answer
~40 sEach interpolated value has an `OSLogPrivacy`; without one it is `.auto`, which shows integers, floating-point values and Booleans but redacts dynamic strings and complex objects. The renter ID is a `String`, so collected logs show `<private>`. Making it `.public` would publish personal data to anyone with log access. Instead write `\(renterID, privacy: .private(mask: .hash))`: the system logs a hash in place of the placeholder, equal IDs give equal hashes, and Apple states the hash carries no identifying information. Apple also describes it as unique for the current process, so it correlates lines within one run. Mark non-personal values such as firmware versions `.public`, and mark identifying numbers `.private` explicitly, since `.auto` shows integers. `.sensitive` redacts exactly like `.private`.
code
swift · 14 linesimport os
let checkInLog = Logger(subsystem: "com.example.boatrental", category: "dock-checkin")
func logCheckIn(renterID: String, dockNumber: Int, lockFirmware: String) {
// String with the default .auto privacy: redacted, shown as <private>
checkInLog.notice("Renter \(renterID) at dock \(dockNumber)")
// Still hidden, but equal IDs produce the same hash, so lines correlate
checkInLog.notice("Renter \(renterID, privacy: .private(mask: .hash)) at dock \(dockNumber)")
// Not personal data: shown as written
checkInLog.notice("Lock firmware \(lockFirmware, privacy: .public)")
}go deeper
Recall that interpolated strings are redacted by default and appear as <private>, and that privacy: .public shows a value.
Explain the four OSLogPrivacy options, which types .auto shows, and that .sensitive behaves like .private.
Fix a field log without leaking data: a hash mask for correlation, explicit .private on identifying numbers, and the per-process scope of the hash.
Decide who may approve a .public value and how reviews catch identifiers logged as integers, which .auto would show.
## Why the ID shows as `<private>` Every value you interpolate into a `Logger` message carries an `OSLogPrivacy` option. If you write none, it is `.auto`, which applies the default rules. Apple's documentation states those rules: integer, floating-point and Boolean values are **not** redacted; dynamic strings and complex dynamic objects **are**. A renter ID held in a `String` is therefore redacted, and a log collected from a renter's phone shows the generic placeholder instead of the value. The static text of the message is never redacted; it is the literal you wrote. The reason is in the documentation too: anyone with access to the logs can read them, so user data should not land there in clear text. ## The four privacy options | Option | What it does | Mask form | |---|---|---| | `.auto` | redacts or shows by type (the default) | `auto(mask:)` | | `.public` | always shows the value | none | | `.private` | always redacts the value | `private(mask:)` | | `.sensitive` | always redacts; documented as behaving identically to `.private` | `sensitive(mask:)` | `OSLogPrivacy`, like `Logger`, is available from iOS 14. ## Hash masks: correlation without disclosure `OSLogPrivacy.Mask` has two cases, `.hash` and `.none`. Passing `.hash` to `private(mask:)`, `sensitive(mask:)` or `auto(mask:)` replaces the generic redaction text with a **hash of the value**: - equal values produce equal hashes, so support can follow one renter through "check-in started", "unlock sent" and "unlock failed"; - Apple states the hash does not provide identifying information about the value, so it cannot be turned back into the ID; - Apple describes the hash as unique for the current process, so treat it as a correlation key within one run of the app, not across launches or devices. ## Fixing the boat-rental logs 1. Keep the renter ID redacted and add the mask: `\(renterID, privacy: .private(mask: .hash))`. 2. Mark values that are not personal data `.public`, such as the lock's firmware version or an error domain, so they stay readable. 3. Mark numbers that identify a person `.private` explicitly, because `.auto` leaves integers visible. 4. Do not "fix" the problem with `.public` on the ID: that publishes it to anyone who reads the logs. ```swift import os let checkInLog = Logger(subsystem: "com.example.boatrental", category: "dock-checkin") func logCheckIn(renterID: String, dockNumber: Int, lockFirmware: String) { // String with the default .auto privacy: redacted, shown as <private> checkInLog.notice("Renter \(renterID) at dock \(dockNumber)") // Still hidden, but equal IDs produce the same hash, so lines correlate checkInLog.notice("Renter \(renterID, privacy: .private(mask: .hash)) at dock \(dockNumber)") // Not personal data: shown as written checkInLog.notice("Lock firmware \(lockFirmware, privacy: .public)") } ``` ## Rules the compiler enforces - The `privacy:` argument must be a static member of `OSLogPrivacy`. A runtime variable such as `let p: OSLogPrivacy = isDebug ? .public : .private` is rejected with "argument must be a static method or property of 'OSLogPrivacy'". If debug builds need different output, write two calls under `#if DEBUG`. - `.public` has no mask form; only `auto`, `private` and `sensitive` take `mask:`. - The same options work in `OSSignposter` messages; Apple's own signpost examples interpolate an account number with `.sensitive(mask: .hash)`. ## Common mistakes - **Interpolating a whole model object.** A booking object is a complex dynamic value, so it is redacted as a unit; log the specific non-personal fields you need instead. - **`.public` on a description that contains a name.** The privacy option applies to the whole interpolated string, so a public description publishes everything inside it. - **Assuming numbers are safe.** Phone numbers or membership numbers held in an integer type are shown under `.auto`. - **Expecting the hash to survive a relaunch.** Plan correlation within one run of the process. ## What the mask is not A hash mask is not encryption and not a support lookup table. If support must find "renter 1234's" lines on request, the app needs an identifier that is not personal data, or the renter's consent to share it through a separate channel. Scrubbing personal data from logs that leave the device for a server is a separate pipeline concern, not something `OSLogPrivacy` does.
- If the renter ID were an Int, would the default privacy hide it?No. Under `.auto`, integer, floating-point and Boolean values are not redacted, so a numeric renter ID appears in clear text. Mark identifying numbers explicitly, for example `\(renterNumber, privacy: .private(mask: .hash))`.
- Can the app choose privacy at runtime, public in debug builds and private in release?Not with a variable: the `privacy:` argument must be a static member of `OSLogPrivacy`, and the compiler rejects a runtime value. Use two separate calls under `#if DEBUG` if debug builds really need different output.
- Will the same renter ID produce the same hash after the app relaunches?Do not plan on it. Apple describes the hash as unique for the current process, so use it to follow one renter through one run. To link runs, log a value that is not personal data, such as a check-in attempt number, publicly.
A cloakroom ticket for one evening: the same coat gets the same number all night, so staff can trace every time it was handed over, but the number says nothing about whose coat it is, and tomorrow the numbering starts again.
- Marking the ID .public is the only way to correlate one renter's lines
- A hash mask lets support recover the original renter ID
- The default .auto privacy redacts every interpolated value, numbers included
- .sensitive redacts more strongly than .private
- The privacy option can be chosen with a runtime variable