For a macOS GUI application (with a UIKit or AppKit entry point), I want to reliably capture diagnostic logs sent to stderr — especially useful when the app is launched from a terminal script or runs in the background, and traditional GUI elements (like alert dialogs) may not be viable. This is to log startup failures or even success messages for later inspection. However, when the app is launched via open MyApp.app,
stderr redirection like open MyApp.app 2> log.txt
does not capture any output — the file is created, but remains empty.
The only way I can capture stderr reliably is by bypassing the bundle and directly launching the binary inside with ./MyApp.app/Contents/MacOS/MyApp 2> ~/log.txt
This logs as expected, but is not the user-friendly or typical way to launch apps on macOS. Double-clicking the app in Finder also does not show any stderr output.
Is there any recommended or supported way to redirect or access stderr output when launching a .app bundle via open, or any best practice for logging critical failures from a GUI app when terminal output isn't visible?
If this is an app you’re developing then you can do this redirect in the app. Add a debug option that causes the app to open your log file and dup2
it on to stderr
(technically STDERR_FILENO
).
Having said that, logging to stderr
isn’t best practice on the Mac because, by default, stderr
goes nowhere. So, lemme answer this from your original post:
any best practice for logging critical failures from a GUI app when terminal output isn't visible?
I generally recommend that you log to the system log. See Your Friend the System Log for links to documentation and other resources.
And that brings me to this:
The idea is that if something goes wrong, the user or support team can rerun the app from a terminal
If you log to the system log, this re-run step isn’t necessary. The user can immediately trigger a sysdiagnose, which snapshots the system log, and you can debug based on that.
Now, you don’t have to use this. It’s fine to invent your own logging infrastructure if the system log doesn’t work for you. However, doing logging correctly is hard, so you should at least understand what the sytem log can do before you decide not to use it (-:
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"