Recently, when I was working on a log transformation for an Android APP, I wanted to meet the following requirements:
Based on this requirement, I searched for "Android log framework", and most netizens recommended logger, timber, xLog, etc., which looked good. However, based on my years of experience and habits in backend development, I learned more and found that the familiar log4j and logback have also been adapted on Android, so I finally decided to use slf4j + logback to achieve a consistent experience in front-end and back-end development. Students who have done Java backend development are generally familiar with the combination of slf4j + logback, but students who have done Android development may not have heard of them. Therefore, this article will start from scratch and record how to integrate slf4j + logback as a logging framework in Android APP and use Lombok annotations to generate log objects. Integrate slf4j + logbacklogback-android Project address: https://github.com/tony19/logback-android 1. Add dependencies in the build.gradle file of the project/module: If it is a single-module project, you can add it directly in the app/build.gradle file. If it is a multi-module project, you can add it in the build.gradle file of a common module. Remember to change the implementation of slf4j-api to api so that it can be referenced by other modules. 2. Create a log configuration file app/src/main/assets/logback.xml: The above configuration means that logs of DEBUG and above levels are output to the console, and logs of INFO and above levels are output to files. Files are segmented by date, and logs of up to 15 days are retained. You can configure as needed, for example, you can limit the size of a single file, customize the format of log output, etc. In the project's Wiki, it is mentioned that one thing that Android developers are more concerned about is that the log has a save path. You can specify an absolute path or use a variable, for example:
3. You can start using slf4j's API to print logs: Run the app and you can see that the logs are output to logcat and files in the corresponding locations. When you have questions about the configuration and need to debug, you can change debug="false" in the above configuration file to debug="true", so that logback will output detailed information to help us locate the problem. Generate log objects using Lombok annotationsIn step 3 of the previous section, in each class that needs to use the logger, you need to manually declare the logger, such as Logger log = LoggerFactory.getLogger(MainActivity.class);, which is not very convenient. Here we can use Lombok annotations to simplify this step and automatically generate logger objects. Lombok officially provides integration instructions for the Android platform: https://projectlombok.org/setup/android. Based on the Android Studio environment, there are actually only two steps to do. 1. Install the Lombok plugin: Settings -> Plugins -> Search for Lombok -> Install Note: For Android Studio versions 2020.3.1 - 2022.3.1, the JetBrains official plugin market cannot search for compatible versions of the Lombok plugin. You can refer to https://gitee.com/sgpublic/lombok-plugin-repository for a solution. 2. Add the following content to the build.gradle file of the module you need to use: Then, you can use the @Slf4j annotation to automatically generate a logger object. Now the usage is simplified to this: summaryWell, the above is the record of integrating slf4j + logback in Android. So far, I have "unified" the usage of Java backend and Android client to print logs, and made a small step forward in avoiding the "schizophrenia" caused by multi-project maintenance. The code samples listed in this article have been uploaded to GitHub, address: https://github.com/mzlogin/AndroidPractices/tree/master/android-studio/LogbackDemo |
>>: How much do you know about Android AOP?
In the first quarter of 2017, Tesla's revenue...
Rabbits in the world are classified into eight ge...
During the hot summer, many people like to go to ...
1. Wrong concept of love: thinking that only mone...
October 22, 2023 Academician of the Chinese Acade...
Ionic 1.0.0 released, codenamed "uranium-uni...
Arm pain, neck soreness... I'm sure you'v...
With over 100 million monthly active users and 10...
In the past few months, Douyin has been imposing ...
A few months ago, Android 12L was released as a B...
4K TV is a TV with a physical screen resolution o...
Carrots are a nutritious vegetable. The carotenoi...
Xpeng Motors released its fourth quarter and full...
Do you know the answers to these questions about ...
Recently, the news that the "giant panda in ...