Logger library created for simple integration of logger capability to android applications providing APIs to write logs with different level of verbosity and avoiding to write boilerplate code like declaring TAG for each class or use static functions to get the logger instance every time as implemented in the most logger library on the market. This library makes the logger process simple to use, pretty and powerful.
The library is distributed as Android Studio project named loggerlib that depends on the module lintrules.
The order in terms of verbosity, from the least to the most is: ASSERT, ERROR, WARN, INFO, DEBUG and VERBOSE. Verbose and Debug should never be used in production. Assert should be used only to log error that should never happens (eg. App crash).
- Minimum performance impact
- Logcat integration
- Log file writing
- File rotation
- CSV format output
- Editable current timestamp
- Crash system detection
- Compile time correctness detection
- Log level supports
- String format args
- Customizable Layout and Appenders
The library is fully thread-safe, it can be configured for creating a separate process for the logger and communicate with it over AIDL, each library function returns immediately to the caller and the file writing is managed in a separate process with a MIN PRIORITY daemon thread.
Repository available on jCenter and Bintray
In your build.gradle, add:
implementation 'com.ech0s7r.android:loggerlib:1.2.11'
If you wish to enable the text report, add the following in your app module build.gradle:
lintOptions {
textReport true
textOutput 'stdout'
}
-
Add loggerlib library into the project
-
Initialize the logger using the LoggerConfigurator class
LoggerConfigurator.init( Logger.Level.WARN, /* Minimum level to log */ 123, /* Application ID */ "MyApp", /* Application name */ BuildConfig.VERSION_NAME, /* Application version */ getDeviceId(), /* Device ID */ "MyAppAndroid.log"); /* File name prefix */
-
Add Layouts with Appenders
LoggerConfigurator.addAppender(new LogcatAppender(new LogcatLayout())); LoggerConfigurator.addAppender(new FileAppender(getApplicationContext(), new CsvLayout()));
The supported appenders, for now, are writing on Logcat and writing on File with Csv layout and Logcat layout.
The Logger usage is pretty simple, all that you need is to call the right static method from the Logger class, as following:
Logger.v("verbose");
Logger.d("debug");
Logger.i("info");
Logger.w("warning");
Logger.e("error");
Logger.wtf("What a Terrible Failure");
Logger.w("Hello %s %d", "world", 1);
try {
...
} catch (Exception e){
Logger.e(e);
// or
Logger.e("description", e);
}
The library implements 4 kinds of custom Lint rules to avoid common error and promote the usage of this library instead of the android built-in logger library. These rules are checked at compile time and if not respected an error will be generated during the build process. The Lint scanning tools, in case of build error, generate an HTML file that can be controlled to see where the following rules are not observed.
-
All the usages of the System.out.print/ln() or System.err.print/ln() are forbidden.
-
The usage of Throwable.printStackTrace() is forbidden, see How to log Exception.
-
The usage of android.utils.Log is forbidden
-
Every Exception should be logged
New Appenders and Layouts can be added by developers, extending the LogAppender and LogLayout classes.
New Lint rules are more than welcome and can be added in the lintrules module.
Each line logged on Android logcat contains the following elements:
- Date and Time
- Process ID
- App package
- Log level
- Thread ID
- Class.Method (File name : code line)
- Message
The log files are stored into the sd-card in a new directory with the same name of the application, into the folder “logs”. The file name is generated adding the current date (format yyyyMMdd) to file prefix used during the logger configuration.
Example: If the application name is MyApp and the file prefix is MyAppAndroid.log, the generated files are:
…
/sdcard/logs/MyApp/MyAppAndroid.log20170803
/sdcard/logs/MyApp/MyAppAndroid.log20170804
…
Copyright 2012 ech0s7r
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.