Logging
suggest changeAny quality Android application will keep track of what it’s doing through application logs.
These logs allow easy debugging help for the developer to diagnose what’s going on with the application. Full Android Documentation can be found here, but a summary follows:
Basic Logging
The Log
class is the main source of writing developer logs, by specifying a tag
and a message
. The tag is what you can use to filter log messages by to identify which lines come from your particular Activity. Simply call
Log.v(String tag, String msg);
And the Android system will write a message to the logcat:
07-28 12:00:00.759 24812-24839/my.packagename V/MyAnimator: Some log messages
└ time stamp | app.package┘ | └ any tag |
process & thread ids ┘ log level┘ └ the log message
TIP: Notice the process id and the thread id. If they are the same - the log is coming from the main/UI thread!
Any tag can be used, but it is common to use the class name as a tag:
public static final String tag = MyAnimator.class.getSimpleName();
—
Log Levels
The Android logger has 6 different levels, each of which serve a certain purpose: - ERROR
: Log.e()
- Used to indicate critical failure, this is the level printed at when throwing an Exception
. - WARN
: Log.w()
- Used to indicate a warning, mainly for recoverable failures - INFO
: Log.i()
- Used to indicate higher-level information about the state of the application - DEBUG
: Log.d()
- Used to log information that would be useful to know when debugging the application, but would get in the way when running the application - VERBOSE
: Log.v()
- Used to log information that reflects the small details about the state of the application - ASSERT
: Log.wtf()
- Used to log information about a condition that should never happen.
- *wtf* stands for "What a Terrible Failure".
E/MyApplication: Process: com.example.myapplication, PID: 25788 com.example.SomeRandomException: Expected string, got ‘null’ instead ```
A quick glance at the logs and it is obvious that the file was empty.
—
Things To Considering When Logging:
Although logging is a powerful tool that allows Android developers to gain a greater insight into the inner working of their application, logging does have some drawbacks.
Log Readability:
It is common for Android Applications to have several logs running synchronously. As such, it is very important that each log is easily readable and only contains relevant, necessary information.
Performance:
Logging does require a small amount of system resources. In general, this does not warrant concern, however, if overused, logging may have a negative impact on application performance.
Security:
Recently, several Android Applications have been added to the Google Play marketplace that allow the user to view logs of all running applications. This unintended display of data may allow users to view confidential information. As a rule of thumb, always remove logs that contain on non-public data
before
publishing your application to the marketplace. —
Conclusion:
Logging is an essential part of an Android application, because of the power it gives to developers. The ability to create a useful log trace is one of the most challenging aspects of software development, but Android’s Log class helps to make it much easier.
For more documentation and examples visit Logging and using Logcat