Use log4j2 API within your Android projects instead of Android's Log API. A nice side effect is that log4j2 logger in dependencies also work seamlessly on Android as it does in non-android projects.
runtime
or its equivalentlog4j-core
library is excluded. log4j2-android is a functional replacement for log4j-core, including both might create unexpected behavior.TLDR? - I distilled the above points in the following build.gradle snippet and use it in my Android project so that I am able to automatically switch between log4j-core for logging during junit testing, and log4j2-android for logging on Android devices (testing or production) without modifying the build.gradle.
configurations.all {
config -> {
if (config.name.startsWith("test") || config.name.contains("Test") && !config.name.contains("Android")) {
exclude group: "com.celeral", module: "log4j2-android"
} else {
exclude group:"org.apache.logging.log4j", module:"log4j-core"
}
}
resolutionStrategy {
force "org.apache.logging.log4j:log4j-api:2.17.2"
}
}
dependencies {
implementation "org.apache.logging.log4j:log4j-api:2.17.2"
testRuntimeOnly "org.apache.logging.log4j:log4j-core:2.17.2"
runtimeOnly "com.celeral:log4j2-android:1.0.0"
}
Functionality of this package is contained in Java package com.celeral.android.logging.log4j2
To use the package, you need to use the following gradle dependency. Note the scope being runtime
since it's a runtime dependency:
dependencies {
runtimeOnly 'com.celeral:log4j2-android:1.0.0'
}
To use the package, you need to use following Maven dependency. Note the scope being runtime
since it's a runtime dependency:
<properties>
...
<!-- Use the latest version whenever possible. -->
<celeral.version.log4j2.android>1.0.0</celeral.version.log4j2.android>
...
</properties>
<dependencies>
...
<dependency>
<groupId>com.celeral</groupId>
<artifactId>log4j2-android</artifactId>
<version>${celeral.version.log4j2.android}</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
or download jars from Central Maven repository.
For non-Maven use cases, you download jars from Central Maven repository.