📜 ⬆️ ⬇️

Logging in Android applications

I am sure that all developers of applications on the Android platform are familiar with the Log class, which allows you to log various events. For various reasons, the logging format for each project may differ quite strongly - from “AAA”, “111111” and “I was here” to more or less intelligible - “Opening HTTP connection to habrahabr.ru”. Under the cat you will find an example of a function that will help bring order to the logs.
This topic does not claim originality and versatility. And therefore, if in your project there already exists a certain standard of event logging, then feel free to pass by - the topic is more focused on novice developers.

As a rule, you begin to understand the value of logs only when a customer swears and sends a log to the post office and asks to fix the fix after 5 minutes. And if the log consists of messages of an inaudible nature, then at least, the analysis of this log will take much longer than we would like.

We are trying to restore order


Logs exist so that the developer can understand what happened where and when. Finding the answer to the question “when it happened” is quite simple - Androyd records the time of the event in the logs. Finding the answer to the question “what happened” also does not cause great difficulties if the message to the log was written with meaning, for example: “Opening file ...”. The question "where happened" is the most difficult. If the project is large, then you have to spend time finding the right place for the code, even if the log was written with meaning.

If the event is logged with the indication Throwable (more often Exception), for example, public static int d (String tag, String msg, Throwable tr) method public static int d (String tag, String msg, Throwable tr) , then a message stack will be displayed in the message console that will help you quickly identify the location of the logging. But the use of this method without special need to disgrace overload the log with unnecessary information.
')
If just text is logged, then when logging, you can explicitly specify the place of the call. For example:
 Log.v("My Project", "[ImageLoader:loadFile]: Opening file..."); 

However, writing this every time is a tedious and ungrateful business.

Below is an example of the Log class that does this automatically.
 public final class Log { ... public static void v(String msg) { android.util.Log.v(TAG, getLocation() + msg); } private static String getLocation() { final String className = Log.class.getName(); final StackTraceElement[] traces = Thread.currentThread().getStackTrace(); boolean found = false; for (int i = 0; i < traces.length; i++) { StackTraceElement trace = traces[i]; try { if (found) { if (!trace.getClassName().startsWith(className)) { Class<?> clazz = Class.forName(trace.getClassName()); return "[" + getClassName(clazz) + ":" + trace.getMethodName() + ":" + trace.getLineNumber() + "]: "; } } else if (trace.getClassName().startsWith(className)) { found = true; continue; } } catch (ClassNotFoundException e) { } } return "[]: "; } private static String getClassName(Class<?> clazz) { if (clazz != null) { if (!TextUtils.isEmpty(clazz.getSimpleName())) { return clazz.getSimpleName(); } return getClassName(clazz.getEnclosingClass()); } return ""; } } 


Using the class is very simple:
 Log.v("Opening file..."); 


The result of logging in this way will be approximately the following lines:
 03-28 22:51:23.239: VERBOSE/TestApp(16390): [MainActivity:onResume:124]: Opening file... 03-28 22:51:23.341: VERBOSE/TestApp(16390): [MainActivity:run:198]: Closing file... 


Note:
For obvious reasons, this method is not very suitable for applications "missed" through obfuscator.

In general, everything.
I apologize if this article seemed too trivial for Habr.

Source: https://habr.com/ru/post/116376/


All Articles