How to set logs effectively in Android

Tram Ho

Log setting can sometimes be considered as a tool to be used as a last resort. You write a lot of log lines like “being here”, “user = 23939”, etc. and delete everything as soon as you understand the root cause of the error. And next time you start over.

I find that logging can be useful on a daily basis if done properly. Here are some tips for better logging on your Android apps.

How do I set the log

You can of course use the built-in Log class and its convenient static methods, d() , e() , i()

But this can lack some flexibility.

One option is to create your own wrapper for this class and include it in your classes. This will allow you to easily disable the production build logs, redirect them to another output, or decorate lines if needed.

Timber is a popular library that will allow you to do exactly that.

Use log levels

You should try not to flood the most important log levels. For example, you could set goals per log level:

Place meaningful tags

Although it is common to use the class name as the log tag, I do not recommend using the class name for your tag.

First, it’s the leaked implementation details.

It also makes filtering more difficult based on this tag, as your feature will often be implemented with multiple classes. It can also result in tag lengths greater than 23, truncated in output.

You can use a pattern like [PREFIX]. [FEATURE]

For a My Good App (MGA) example, you would define:

You can add a module.kt to the root of the feature package that contains a const like

const val TAG = "mga.user"

This file may also contain other definitions for modules, such as the dependency injection definition.

What to log

Important lifecycle events

Lots of Android app bugs and misconduct are related to the Android lifecycle. You should always write down this type of information. If you have a Fragment base inherited by all Fragments, use it to log these events.

This also has the advantage of leaving a trace of all navigation actions and allowing easy access to the current Fragment name. This can save a lot of time for newbies to learn the application and its code base.

Most of the user actions

All user actions will result in something being printed to the log. This doesn’t mean that all click handlers in the UI layer will print something, but the action triggered in your layer domain will print something with some parameters. relate to.

App version, BuildFlavor

Use the application’s Application class to record some basic information about your variant and version. This way, you will be able to associate the log with a specific version without any ambiguity, even if some date or month has passed.

App Configuration

In addition to the previous points, also record the dynamic configuration on startup and when it is changed. Since this can affect the performance of your application, it is important to have this information in order to interpret the log.

Remote logging solution

Sometimes the problem is only happening on a client’s device, and you might need contextual information to find out what’s going on.

Remote logging is your solution in this situation.

Firebase Crashlytics provides a way to record Non-fatal exceptions .

It’s enough to have more context for the inconsistent state that you can’t reproduce.

If you need more information in context, you can use the remote logging feature provided by Bugfender , which basically provides Logcat-like output (i.e. all traces) as if you were plugged in. USB debugging cable on client device!

Do not use Unicode symbols

Log content must be simple and reliable and not intended to be amusing or eye-catching.

  • You may have to copy it from different terminals, text editors and websites may not support UTF-8 well (or completely).
  • You’ll have to search in it, and it seems like finding a sample of text instead of a random emoji will be more efficient.

In other words, keep logs simple.

Conclude

In short, it’s pretty easy to keep an eye on your Application Log and it can be of great help during the critical moments and also help you and your teammates be more efficient on a daily basis.

Ref: https://blog.bam.tech/developer-news/android-logging-with-style

Share the news now

Source : Viblo