Write Logs You Can Use Without Recording Private Data
An upload failed. A useful log tells you which operation failed and what category of failure occurred. Dumping the full request, local file URL, and error description can reveal far more than the investigation needs.
The best place to decide what a diagnostic exposes is where you design the event, before someone adds a convenient string interpolation during a production incident.
Start with an allowlisted event
Give the application a small vocabulary of failure reasons. Map underlying errors into that vocabulary at the boundary that understands them.
import Foundation
import os
enum RefreshFailure: String, Sendable {
case offline, unauthorized, invalidResponse, unknown
}
struct RefreshLogger {
private let logger = Logger(subsystem: "example.reader", category: "Refresh")
func failed(_ reason: RefreshFailure, operationID: UUID) {
logger.error(
"Refresh failed reason=\(reason.rawValue, privacy: .public) operation=\(operationID.uuidString, privacy: .public)"
)
}
func inspectedDocument(_ documentID: String) {
logger.debug("Inspect document=\(documentID, privacy: .private(mask: .hash))")
}
}
The public reason is one of four fixed strings. The operation ID is a freshly generated UUID for this one refresh, not an account ID, document ID, or stable identifier carried across sessions.
That gives you a way to connect a start and a failure event without exposing the object being refreshed. Generate the ID at the operation boundary and pass it through; generating a different UUID at every log statement defeats the correlation.
An error description is not a safe category
error.localizedDescription may contain details supplied by a framework, server, or file path. Marking the entire result public because it was convenient during development changes what the log can reveal.
Instead, inspect the error type and code, then select a known reason. For example, a transport failure can become offline, an authentication response can become unauthorized, and decoding failure can become invalidResponse. Keep an unknown branch so a new error does not fall back to logging arbitrary content.
Those names should fit the app’s actual semantics. Not every connection failure proves the device is offline, so map codes carefully rather than labeling every networking error that way.
Preserve Logger’s typed interpolation
Apple’s logging guide explains privacy handling for interpolated values. Dynamic strings are redacted by default, while some scalar values are not. A number can still be sensitive: an account number does not become harmless because its Swift type is an integer.
Keep interpolation in the Logger call and make the important privacy choices explicit. Prebuilding a large ordinary String loses the structure of the individual fields and makes review harder. One .public annotation on that completed string can expose everything inside it.
The document diagnostic uses private hashing so matching values can be correlated within a process without printing the original string in the normal log representation. Treat that as a logging feature, not a general anonymization scheme or a stable analytics identifier.
Choose an appropriate level and frequency
A debug message for a synthetic document inspection is different from an error that explains a failed user operation. Avoid logging every row, keystroke, or retry-loop iteration by default.
Prefer state transitions: an operation starts, changes retry state, succeeds, or fails. Log an aggregate count only when the count itself is acceptable diagnostic data. The goal is enough evidence to reconstruct behavior without reconstructing the user’s content.
Do not log access tokens, passwords, request bodies, document text, or full URLs with query strings. Redaction should not become an excuse to collect secrets that the investigation never needed.
Verify the output path you plan to use
A successful compile proves that the interpolation is valid. It does not prove that your support export, third-party logging adapter, or crash attachment preserves the same privacy behavior.
Exercise the actual diagnostic collection workflow with synthetic values. Search the resulting output for those values, inspect which levels persist, and verify that the operation ID connects only the intended events. Repeat if you add another sink that receives ordinary strings before unified logging handles them.
September 2026 review: this example compiled for iOS 17 and executed with synthetic values on the host. Persisted device logs and support-export redaction were not inspected; those are deliberate integration checks, not claims made by this fixture.