Blog Layout

How to disable os_log logging in release and production builds

Raphael Haase • Jan 13, 2021

Of course, there is a newer LoggerAPI since iOS 14. But maybe, you can not yet set iOS 14 as a minimum version yet since it was just released in 2020.


There are some reasons why you may not want to log anything or not log certain messages in your released (aka production) builds, even if you use the privacy features of the os_log  API.


How to use the built-in privacy features of os_log


Apple has thought about this topic so there is a great deal if ways to prevent sensitive data being leaked through logging already. For some reason, you might end up logging a password or, more likely, a user-specific API token. The best practice is probably not to do this at all, but if you just have to at least for a while during development, then it will still be better to do this in a way where if you forget to remove that log statement from your code, it will not be a catastrophe or where you could even leave it there without leaking sensitive info to the log.


So you could start off like this:


let apiToken = API.token(user: "johnny@example.com")

os_log_debug(OS_LOG_DEFAULT, "Obtained API token: %@.", apiToken)


This may however expose the API token. The user could for example attach their Mac to the phone, view the logs and then they see the API key. Theoretically, this should not give you goosebumps, but consider that malware could also do this and steal the API token. 


That is why Apple introduced this extension:


os_log_debug(OS_LOG_DEFAULT, "Obtained API token: %{private}@.", apiToken)



Now, the API token will only show up in Xcode’s logger, but not to the macOS Console application. You can only see the API token while debugging your own app with Xcode, but not in production and somebody else than your dev team can not see the tokens. Instead of the API token, they will see a message like this in the log:


Obtained API token: <private>.



One step further: Not logging in production (at all)


If you want to make reverse engineering harder, you may stop logging certain messages at all, even if it is now about runtime data like API tokens. So you might want to not log the steps that an app goes through at all in production to make it impossible for someone to simply start up the Console app and see what your app does. This of course does not prevent anyone from disassembling your code, but at least it switches off an easier variant of that.



In the os_log methods, you can specify which log you want to use. One use case of that is having multiple logs for different parts of your app. Now if you give os_log the special log constant OSLog.disabled it will simply disable the logging.



So to deactivate the production logging entirely you basically have to ensure that you always plug OSLog.disabled into os_log for your production builds. One way is to use Swift’s compiler macros aka Active Compilation Conditions


So you could end up having a global function like


func customLog(_ category: String) -> OSLog {

  #if DEBUG

  return OSLog(subsystem: Bundle.main.bundleIdentifier!, category: category)

  #else

  return OSLog.disabled

  #endif

}


and then you can use the OSLog returned from that function in all your logs.


In the Build Settings in Xcode you then specify DEBUG in the Active Compilation Conditions for the appropriate builds et voila. No more log messages in your production builds.






A robotic hand automates testing on Apple devices by touching them
By Raphael Haase 09 Feb, 2023
Automated testing is a must for any app on iOS. It not only saves time, it is crucial for ensuring consistently good user experience and data security of your apps.
By Raphael Haase 15 Dec, 2022
Automatisiertes Testen ist eine Technik, bei der vorher festgelegte Tests für eine mobile Anwendung automatisch ausgeführt werden. Dies kann dazu beitragen, die Qualität und Zuverlässigkeit einer App sicherzustellen, indem Fehler und andere Probleme erkannt werden, bevor die App veröffentlicht wird. Gerade auch, wenn es nur um ein Update gibt. Denn so genannte Regressionen, Fehler die wieder zurück kommen, können einer App ordentlich Probleme bereiten. Einige Vorteile von automatisierten Tests für iOS und Android sind:
Automatisiertes Testen auf iPhones durch Roboterhände
By Raphael Haase 01 Dec, 2022
In der Medizin- und Finanzbranche ist die Entwicklung von iOS-Apps, die zuverlässig und genau sind, unerlässlich. Automatisierte Tests können dabei helfen, diesen Prozess einfacher und effizienter zu gestalten. Werfen wir also einen Blick auf die Vorteile des automatisierten Testens bei der Entwicklung von iOS-Apps in der Medizin- oder Finanzbranche.
By Raphael Haase 20 Oct, 2022
Entwickelt man heute mobile Anwendungen für iOS oder Android, dann heisst es häufig man müsse unbedingt einem bestimmten Architektur-Pattern folgen wie MVVM oder VIPER. Sonst würde sofort die App schlecht werden. Nun ist es nicht verkehrt gewissen Mustern zu folgen, denn das macht es für andere Entwickler einfach den Code zu verstehen und man hilft natürlich auch sich selbst dabei, das Rad nicht neu zu erfinden. Das Problem ist aber, dass nicht alle Architektur-Muster gleichermassen geeignet sind. Am wichtigsten ist dabei wohl, dass viele Architektur-Muster für kleine Anwendungen mit 1-2 Entwicklern irgendetwas zwischen Overkill oder sogar Kontraproduktivität sind. 
Standup Paddling ist manchmal besser als ein Daily Standup
By Raphael Haase 06 Oct, 2022
Viele tun es, nicht alle lieben es: Das "Daily Standup". Heute geht es darum, warum ich dieses Ritual kritisch sehe und zu einem Umdenken rate.
A software developer leaving the team
By Raphael Haase 22 Sep, 2022
It's every CTO's worst nightmare: your developers are the lifeblood of your company. They manage our most valuable digital assets and without them, you can't launch or maintain any meaningful customer engagement plans. But what happens when one of these key members suddenly walks away from their post with little to no notice? Suddenly, you've got a critical hole in your product development team that needs to be filled – but fast! You must find a replacement quickly. But even then, there’s still the daunting task of ensuring continuity in launching and maintaining new products as well as existing applications already live on the market. In this blog post, we'll discuss ways to make sure your app stays up and running even if (or especially if!) a developer decides to move on swiftly leaving behind incomplete projects for whoever steps up next.
By Raphael Haase 07 Apr, 2022
If you get an Undefined Symbol error in test cases, here is the fix for the most likely simple issue.
Monterey landscape
By Raphael Haase 17 Feb, 2022
A quick recipe on how you can add a new path or folder to "the" command line path.
Pipelines
By Raphael Haase 20 Jan, 2022
Add multiline bash scripts in Azure Pipelines like this.
By Raphael Haase 06 Jan, 2022
How to setup the Jazzy documentation tool for Swift on an M1 / Apple Silicon Mac.
More Posts
Share by: