<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexey Kozhevnikov on Medium]]></title>
        <description><![CDATA[Stories by Alexey Kozhevnikov on Medium]]></description>
        <link>https://medium.com/@kozhevnikoff?source=rss-c54f71388859------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9fAgDKqFCGuoE20wyYpRRw.jpeg</url>
            <title>Stories by Alexey Kozhevnikov on Medium</title>
            <link>https://medium.com/@kozhevnikoff?source=rss-c54f71388859------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:56:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kozhevnikoff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[On the Logging in Mobile Applications]]></title>
            <link>https://medium.com/@kozhevnikoff/on-the-logging-in-mobile-applications-8f9b5538660d?source=rss-c54f71388859------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f9b5538660d</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[logging]]></category>
            <dc:creator><![CDATA[Alexey Kozhevnikov]]></dc:creator>
            <pubDate>Thu, 14 Sep 2017 20:35:31 GMT</pubDate>
            <atom:updated>2017-09-14T20:35:31.445Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Here I describe an approach from iOS point of view. An approach for Android platform is no different. I can recommend to use </em><a href="https://github.com/JakeWharton/timber"><em>Timber</em></a><em> framework as an Android equivalent for </em><a href="https://github.com/CocoaLumberjack/CocoaLumberjack"><em>CocoaLumberjack</em></a><em>.</em></p><p>Logs? Phew! Just write something like:</p><p>print(&quot;Expected `foo` to be &#39;bar&#39;, got: \(foo)&quot;)</p><p>Later, when you debug an issue, the message in console could give you a hint for the solution. Simple enough. But it’s good only for a “Hello World” application.</p><p>Why?</p><p>1st of all, logs should be available when you need them desperately. A large number of users is a guarantee that every possible crash will happen including those under really strange circumstances. Unable to reproduce the issue, with only a stack trace in your hands you would clearly benefit from an additional source of information — application logs. Is it possible to attach logs to a crash report? Yes. For example, Crashlytics <a href="https://docs.fabric.io/apple/crashlytics/enhanced-reports.html#custom-logs">Custom Logs</a> can be used for that.</p><p>Let’s assume that in the 3rd party SDK, which you are using to collect crash reports there is a func cloudLog(_ msg: String) method (like CLSLogv method in Crashlytics). Say this method follows the Unix philosophy and the only thing it does is sending a provided message to the 3rd party no matter what the current build configuration is. But we would want to use this handy method only in production builds in order to avoid the noise from the 3rd party when debugging.</p><p>So it makes sense to introduce your own method like this:</p><pre>func myLog(_ msg: String) {<br>#if DEBUG<br>    print(msg)<br>#else<br>    cloudLog(msg)<br>#endif<br>}</pre><p>Hmm, didn’t we just start writing our own logging framework?</p><p>Our new myLog(_:) method works for most cases. But what if the application you are working on handles sensitive information? Banking, dating or health apps are good examples. If you are logging payloads of your network requests and responses (that would be handy, so probably you are) via previously introduced myLog method you might be sending sensitive information to the 3rd party. For some organisations that’s a huge no-no.</p><p>So what can we do about that? Something like this should work:</p><pre>func myLog(_ msg: String, isSensitive: Bool = true) {<br>#if DEBUG<br>    print(msg)<br>#else<br>    if !isSensitive {<br>        cloudLog(msg)<br>    }<br>#endif<br>}</pre><p>Hmm,myLog keeps growing…</p><p>2ndly, sometimes a desire to log is in itself an indication of something unexpected happening and usually an additional action should be taken in such a case.</p><p>To be more specific imagine your application is talking to your beloved server and suddenly a terrible thing happens — incompatible JSON is served. Shouldn’t we do the following?</p><p>myLog(&quot;Error, look how terrible that JSON is: \(json)&quot;)</p><p>Not bad. But the problem won’t be noticed unless your application crashes right after. A crash is better than nothing in such a case, but the user might benefit from an offline data or remaining parts of the working functionality. A much better option would be to silently report the problem, receive the report, fix the API, meanwhile showing “Something went wrong. Sorry! We are already working on it.” message to the user. Is there a ready solution to report such a problem? Yes. For example, Crashlytics <a href="https://docs.fabric.io/android/crashlytics/caught-exceptions.html?caught%20exceptions#caught-exceptions">Caught Exceptions</a> can be used for that. I will reference those as a non-fatals — issues that are not crashing the application but should be reported.</p><p>Let’s assume that there is a func reportNonFatalError(_ error: Error) method to report problems silently (like recordError in Crashlytics).</p><p>We don’t want to report the issue in debug builds (to avoid the noise), but we still want to log to the console a message describing the problem. As for reporting, reusing this log message looks like a good idea. So the straightforward way to do that is like this:</p><pre>let message = &quot;Error, look how terrible that JSON is: \(json)&quot;<br>#if DEBUG<br>myLog(message)<br>#else<br>reportNonFatalError(makeError(withMessage: message))<br>#endif</pre><p>The above code looks too cumbersome. Much better is to rewrite our myLog method like this:</p><pre>enum LogBehavior {<br>    case writeToLog(isSensitive: Bool)<br>    case reportAsNonFatal<br>}</pre><pre>struct WrappedStringError: Error {<br>    var value: String<br>    var localizedDescription: String {<br>        return value<br>    }<br>}</pre><pre>func myLog(_ msg: String, behavior: LogBehavior = .writeToLog(isSensitive: true)) {<br>#if DEBUG<br>    print(msg)<br>#else<br>    switch behavior {<br>    case let .writeToLog(isSensitive):<br>        if !isSensitive {<br>            cloudLog(msg) <br>        }<br>    case .reportAsNonFatal:<br>        reportNonFatalError(WrappedStringError(value: msg))<br>    }<br>#endif<br>}</pre><p>NowmyLog‘s signature seems too complicated and its implementation is like a trash bin full of 3rd party specific logic. Is there a better way? Yes!</p><p>We better use some logging framework. Something like <a href="https://github.com/CocoaLumberjack/CocoaLumberjack">CocoaLumberjack</a>. The idea behind it is really simple, but powerful. On the launch of the application you plug in some loggers (you can create your own or select from the predefined ones). When you log the message, it is broadcasted to every logger you plugged in. It’s up to a particular logger how to process the message. Given the lack of restriction on the number of loggers you can plug in you can untangle logging spaghetti using single responsibility principle.</p><p>But let’s give some examples.</p><p>The most simple but useful logger is a console logger which just prints a message to the console.</p><p>You might want to use file logger which persists logs on disk and rotates them. File logger is quite useful for feedback functionality — ask the user if he want to attach logs when composing email to customer support. A problem description with recent log is much more helpful than just the description. Of course you should not store any sensitive data in such logs.</p><p>Passing log messages to cloud log or reporting non-fatals is a good enough job for a separate logger as well.</p><p>Okay. Seems pretty straightforward. But how can we pass the information to a logger if the message is sensitive or if it should be reported as a non-fatal?</p><p>To answer that question let’s look at how we actually log. We do it the same way as we did before when using standard print, but now we have to flag the message as error, warning, info, debug or verbose one. Too many of them, isn’t it a complication? A bit it is, but a handy one. Of course those types impose specific semantics on messages and could be used to filter or color them appropriately in the console. But they can be also used to make a decision on what particular logger should do with a particular message. For example, non-fatals logger should ignore all the messages except for of error type.</p><p>No more words, just look at the following logging policy scheme:</p><pre>┌───────┬───────┬───────┬─────┬─────┬──────────────────────────────┐<br>│       │       │       │ Non │Cloud│                              │<br>│       │ Build │Console│Fatal│ Log │         When To Use          │<br>│       │       │       │     │     │                              │<br>├───────┼───────┼───────┼─────┼─────┼──────────────────────────────┤<br>│       │       │       │     │     │Something bad has happened    │<br>│       │       │       │     │     │that should never happen. App │<br>│       │ Debug │   ✓   │     │     │is not crashing but a part of │<br>│       │       │       │     │     │the functionality is not      │<br>│       │       │       │     │     │working. The situation        │<br>│       │       │       │     │     │should be reported.           │<br>│ Error ├───────┼───────┼─────┼─────┤                              │<br>│       │       │       │     │     │E.g., a wrong JSON format,    │<br>│       │       │       │     │     │parsing errors when a source  │<br>│       │Release│       │  ✓  │  ✓  │format is unsupported, some   │<br>│       │       │       │     │     │system service is not         │<br>│       │       │       │     │     │available but we expect it to │<br>│       │       │       │     │     │be.                           │<br>├───────┼───────┼───────┼─────┼─────┼──────────────────────────────┤<br>│       │       │       │     │     │Something bad has happened    │<br>│       │       │       │     │     │that can happen from time to  |│       │ Debug │   ✓   │     │     |time, it’s a normal           │<br>│       │       │       │     │     │behaviour. Messages of that   |<br>│       |       |       |     |     |level are useful to           |<br>│Warning├───────┼───────┼─────┼─────┤understand the reason for     │<br>│       |       |       |     |     |a crash or an unclear bug.    │<br>│       │       │       │     │     │                              │<br>│       │Release│       │     │  ✓  │E.g., a network request fails |<br>│       │       │       │     │     │due to the internet being off |<br>│       │       │       │     │     │or login token expired.       │<br>├───────┼───────┼───────┼─────┼─────┼──────────────────────────────┤<br>│       │       │       │     │     │Something expected happened.  │<br>│       │       │       │     │     │Messages of that level are    │<br>│       │ Debug │   ✓   │     │     │useful to understand the      │<br>│       │       │       │     │     │reason for a crash or an      |<br>│       │       │       │     │     │unclear bug.                  │<br>│       │       │       │     │     │                              │<br>│ Info  ├───────┼───────┼─────┼─────┤E.g., a view controller is    │<br>│       │       │       │     │     │presented or dismissed,       |│       │       │       │     │     │user interactions, some       │<br>│       │Release│       │     │  ✓  │service finished an           │<br>│       │       │       │     │     │initialization, a network     |<br>│       │       │       │     │     │request succeeded, a DB is    |<br>│       │       │       │     │     │saved successfully.           │<br>├───────┼───────┼───────┼─────┼─────┼──────────────────────────────┤<br>│       │       │       │     │     │Messages of that level are    |<br>│       │ Debug │   ✓   │     │     │useful to log sensitive       │<br>│       │       │       │     │     │information such as a network │<br>│ Debug ├───────┼───────┼─────┼─────┤response content if it’s      │<br>│       │       │       │     │     │really needed. Such log       │<br>│       │Release│       │     │     │messages should not litter    │<br>│       │       │       │     │     │the console.                  │<br>├───────┼───────┼───────┼─────┼─────┼──────────────────────────────┤<br>│       │ Debug │       │     │     │Messages that are littering   │<br>│Verbose├───────┼───────┼─────┼─────┤the console, should be        │<br>│       │Release│       │     │     │manually turned on.           │<br>└───────┴───────┴───────┴─────┴─────┴──────────────────────────────┘</pre><p>Important details:</p><ol><li>log sensitive information only on debug and lower levels (that’s only verbose);</li><li>report only error messages as non-fatals;</li></ol><p>That’s roughly the scheme we are using in <a href="https://transferwise.com/">TransferWise</a> for our iOS application. Of course some amount of work is needed to setup such an approach for an existing application, but it will make the maintenance simpler. For the applications that are suffocating from the tech debt it’s one of the first steps to do. But even if your application is pretty healthy, efforts made to keep a finger on the pulse are thousand times worth it!</p><p>DDLogInfo(“Happy logging!&quot;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f9b5538660d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>