Blog / 2025
The Overkill(?) Logger: Building a Privacy-Aware, High-Performance Logging System for iOS

As iOS developers (at least those of us who haven’t discovered Xcode breakpoints yet), our first instinct when debugging is to type print("check 1"), print("here"), or the classic print("ERROR: \(error)").
For a Hello World app, that’s fine. But I wanted a logger that knows where each message came from, keeps user data out of release logs, and doesn’t waste CPU on messages nobody will read. Basically a space shuttle launch system inside a to-do list app, mostly because I could.
In this article I’ll share the logging system I use in my personal projects. It wraps Apple’s OSLog and uses @autoclosure and conditional compilation. Is it overkill? Probably. Is it awesome? Absolutely.
The problem with print()
Plain print statements have three problems:
- They block the main thread. Leave a
printinside a tight animation loop and your UI will stutter. - They carry no context. A console full of “Data loaded” messages is useless if you can’t tell which file or line printed them.
- They leak data. If you forget a
print("User: \(email)")in your code, that email is readable in the device’s system logs.
Part 1: Smart log levels
First, I separate noise from signal with a LogLevel enum that conforms to Comparable.
enum LogLevel: Int, Comparable {
case verbose = 0 // "Loop i=1", "Loop i=2" (Noise)
case debug = 1 // "User clicked button"
case info = 2 // "App entered background"
case warning = 3 // "API slow, but working"
case error = 4 // "Crash imminent"
// ... (Computed properties for naming and OSLogType mapping)
}
Because the cases have integer raw values (0 to 4), Comparable lets me set a threshold. My Logger class has a minimumLogLevel: in Debug builds it’s .verbose, so I see everything, and in Release it’s .info, which mutes the verbose and debug logs and keeps the production console clean.
Part 2: The privacy feature (the overkill part)
Apple’s OSLog can redact sensitive values in production logs, replacing them with <private>, but I found the default behavior clunky. So I wrote a LogPrivacy enum to control what gets shown and when.
enum LogPrivacy {
case `public` // Always visible (DEBUG + RELEASE)
case `private` // Redacted in RELEASE
case auto // The Magic: Public in DEBUG, Private in RELEASE
case sensitive // Always redacted (For the paranoid)
}
The .auto case is my favorite. When I log with .auto, the logger checks whether the app was built in DEBUG:
- While debugging, I see
User email: dim@test.com. - In an App Store build, the system sees
User email: <private>.
I don’t have to remember to strip logs before a release, because the build configuration does the censoring for me.
Part 3: Performance and lazy evaluation (@autoclosure)
This is what usually wins over the Performance-Obsessed Engineer Cult.
String interpolation like "User: \(user.description)" isn’t free. The CPU has to allocate memory and build that string.
Say you have this log:
Logger.verbose("Huge Object Dump: \(heavyObject.generateDebugDescription())")
In Release, verbose logs are ignored. With a normal String parameter, though, Swift still runs heavyObject.generateDebugDescription() to build the argument, and the function then throws the result away. That’s CPU time spent on nothing.
The fix: @autoclosure
Here’s the method signature:
static func verbose(_ message: @autoclosure () -> String, ...)
@autoclosure tells Swift: “Don’t build this string yet. Wrap it in a closure and wait.”
The log function checks the level first and returns early if it’s too low, so the closure never runs and the expensive string never gets built. That means you can leave thousands of debug logs in your code without slowing down production.
Part 4: The implementation
Here’s how it all fits together. The backend is OSLog, which Apple optimized to be easy on the battery.
The metadata injection
The helper methods take Swift’s literal expressions (#file, #function, #line) as default arguments. You never type them; the compiler fills them in with the location where the function was called.
#file: The absolute path of the source file.#function: The name of the function calling the logger.#line: The line number.
// Usage: Logger.info("Hello")
// Compiler sees: Logger.info("Hello", file: "ViewController.swift", function: "viewDidLoad", line: 42)
The core logic
The log function does the heavy lifting. os_log needs static format strings (%{public}@ vs %{private}@), which is why the switch privacy block calls it separately for each case.
private static func log(level: LogLevel, message: String, privacy: LogPrivacy, file: String, function: String, line: Int) {
// 1. Filter: If this is a verbose log and we are in Release, STOP.
guard level >= minimumLogLevel else { return }
// 2. Format: Clean up the filename and add a timestamp in DEBUG
let formattedMessage: String
#if DEBUG
let timestamp = ISO8601DateFormatter().string(from: Date())
let fileName = (file as NSString).lastPathComponent
formattedMessage = "\(timestamp) \(level.name) [\(fileName):\(line)] \(function) > \(message)"
#else
formattedMessage = message
#endif
// 3. Log: Send to OSLog with the correct privacy setting
switch privacy {
case .public:
// "%{public}@" forces the log to be visible
os_log("%{public}@", log: logger, type: level.osLogType, formattedMessage as NSString)
case .private:
// "%{private}@" redactes the message in Release
os_log("%{private}@", log: logger, type: level.osLogType, formattedMessage as NSString)
case .sensitive:
// The sensitive log type always redactes the message
os_log("%{private}@", log: logger, type: level.osLogType, formattedMessage as NSString)
case .auto:
// The Smart Switch using Compiler Directives
#if DEBUG
os_log("%{public}@", log: logger, type: level.osLogType, formattedMessage as NSString)
#else
os_log("%{private}@", log: logger, type: level.osLogType, formattedMessage as NSString)
#endif
}
}
The helper methods
static func verbose(_ message: @autoclosure () -> String, privacy: LogPrivacy = .auto, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .verbose, message: message(), privacy: privacy, file: file, function: function, line: line)
}
static func debug(_ message: @autoclosure () -> String, privacy: LogPrivacy = .auto, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .debug, message: message(), privacy: privacy, file: file, function: function, line: line)
}
static func info(_ message: @autoclosure () -> String, privacy: LogPrivacy = .auto, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .info, message: message(), privacy: privacy, file: file, function: function, line: line)
}
static func warning(_ message: @autoclosure () -> String, privacy: LogPrivacy = .auto, file: String = #file, function: String = #function, line: Int = #line) {
log(level: .warning, message: message(), privacy: privacy, file: file, function: function, line: line)
}
static func error(_ message: @autoclosure () -> String, error: Error? = nil, privacy: LogPrivacy = .auto, file: String = #file, function: String = #function, line: Int = #line) {
var fullMessage = message()
if let error = error {
fullMessage += " | Error: \(error.localizedDescription)"
}
log(level: .error, message: fullMessage, privacy: privacy, file: file, function: function, line: line)
}
How to use it
With the internals thoroughly over-engineered, using it is simple.
1. The standard debugging log
Logger.debug("User tapped the button")
// Output: 2023-12-01... DEBUG [HomeVC.swift:45] didTapButton > User tapped the button
2. The “smart” privacy log
// By default, privacy is set to .auto
Logger.info("User ID: \(user.id)")
// In Simulator: User ID: 12345
// In App Store: User ID: <private>
3. The “I need to see this in Crashlytics” log
// Force public visibility even in Release
Logger.error("Database corruption detected", privacy: .public)
4. The error log
Logger.error("Network failed", error: apiError)
// Appends the localized description of the error automatically
Conclusion
Is this logger 100+ lines of code just to replace print()? Yes. Did I spend more time writing the logger than the actual feature I was debugging? Also yes.
Takeaway
In return, I get a logger that doesn’t leak user data, doesn’t spend CPU on logs nobody will see, and tells me exactly where each message came from.
If you’re serious about your iOS architecture (or just like over-engineering things as much as I do), feel free to copy the Logger class from https://github.com/dimaswisodewo/The-Overkill-Logger into your project.
Thank you for reading! Feel free to roast my code. Just make sure the code gets better, even if my feelings don’t.