|
| 1 | +# Support for Log4J2 in Android |
| 2 | +Yet another one. But this one works. |
| 3 | + |
| 4 | +The example section shows the usage of SLF4J facade on the lib side and the log control using Log4J2 on the app side. |
| 5 | + |
| 6 | + |
| 7 | +# Already works |
| 8 | +* File-based configuration, except for XML format (because of defect in Android) |
| 9 | +* Integration with SLF4J |
| 10 | +* Configuration overriding in JUnit/Android tests |
| 11 | + |
| 12 | +# TODO |
| 13 | +* JUnit extension for initialization instead of @BeforeAll |
| 14 | +* Registration of the plugins using annotation processor or PluginUtil |
| 15 | + |
| 16 | +# Examples |
| 17 | + |
| 18 | +It shows a classic example of a library The Lib consumed by an application The App. The lib does some logging, which is handy for its developers but might not be |
| 19 | +so for the devs of the App, who are more interested in their own logs during the development. And for the release, the noise in logs must be reduced to the minimum. |
| 20 | + |
| 21 | +## The Lib |
| 22 | +The library does not use any specific logging library but rather a logging facade, in this case SLF4J. The choice (and control over) of the concrete logging |
| 23 | +implementation is on the consumer of the library. |
| 24 | + |
| 25 | +However, the developers might want to use Log4J2 for the testing and debugging. They can do it by having a separate configuration file for the tests. |
| 26 | + |
| 27 | +## The App |
| 28 | +The app has to say which concrete logging engine is used. Library `android-log4j2` offers seamless and easy integration with Log4J2 engine. |
| 29 | + |
| 30 | + |
| 31 | +# File-based configuration of the logging level |
| 32 | + |
| 33 | +## Location of config files |
| 34 | +The Log4J2 configuration file is called `log4j2.properties` and is found in the following locations: |
| 35 | +1) for unit tests, nothing changed. It is where we always had it - in `src/test/resources` |
| 36 | +2) for Android tests, it is in `src/androidTest/assets` |
| 37 | +3) for Android app, it is depending on the build variant: |
| 38 | + * for debug builds: in `src/debug/assets` |
| 39 | + * for release builds: in `src/release/assets` |
| 40 | + |
| 41 | +If you do not separate your debug and release source sets, you can keep it in `src/main/assets`; just don't forget to adjust the log levels before the release. |
| 42 | + |
| 43 | +In Android tests, the configuration from `src/androidTest/assets` overrides the config from the app. |
| 44 | + |
| 45 | +## Example config file |
| 46 | +Example of a typical `log4j2.properties` during the development: |
| 47 | +```properties |
| 48 | +# Root Logger |
| 49 | +rootLogger = ALL, LOGCAT |
| 50 | + |
| 51 | +# Direct log messages to LOGCAT |
| 52 | +appender.logcat.type = Logcat |
| 53 | +appender.logcat.name = LOGCAT |
| 54 | +appender.logcat.layout.type = PatternLayout |
| 55 | +appender.logcat.layout.pattern = %m%n |
| 56 | +appender.logcat.stack-trace-rendering = logcat |
| 57 | + |
| 58 | + |
| 59 | +logger.lib.name = com.github.neboskreb.lib |
| 60 | +logger.lib.level = warn |
| 61 | + |
| 62 | +logger.app.name = com.github.neboskreb.app |
| 63 | +logger.app.level = all |
| 64 | +``` |
| 65 | + |
| 66 | +## LogcatAppender attributes |
| 67 | + |
| 68 | +### stack-trace-rendering |
| 69 | +Select which engine to use for rendering the stack trace when logging the exceptions. Some Android tools might get confused with Log4J's stack traces. |
| 70 | + |
| 71 | +Options: `logcat` (default), `log4j2` |
| 72 | + |
| 73 | + |
| 74 | +# Credits |
| 75 | +This work is based on the [example app](https://github.com/loune/log4j2-android) by Loune. |
| 76 | + |
| 77 | +# Contribution |
| 78 | +Pull requests are welcome! If you plan to open one, please first create an issue where you describe the problem/gap your contribution closes, and tag the keeper(s) of this repo so they could get back to you with help. |
0 commit comments