A lightweight and efficient SLF4J binding for Android that routes all SLF4J logging calls to Android's native Logcat system.
This library provides a simple way to use the popular SLF4J (Simple Logging Facade for Java) logging API in Android applications. It routes all SLF4J logging calls to Android's native Logcat system with appropriate log levels.
- Lightweight implementation with minimal overhead
- Supports all SLF4J log levels (TRACE, DEBUG, INFO, WARN, ERROR)
- Maps SLF4J log levels to appropriate Android Logcat priorities
- Supports parameterized logging for improved performance
- Handles exception logging with full stack traces
- Configurable minimum log level
- Compatible with Android API 16+
Add the dependency to your app's build.gradle.kts
file:
dependencies {
implementation("eu.torvian.logging:slf4j-android-logcat-binding:1.0.0")
// SLF4J API is required
implementation("org.slf4j:slf4j-api:2.0.7")
}
- Create a logger instance in your class:
private val logger = LoggerFactory.getLogger(YourClass::class.java)
- Use the logger to log messages:
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warn("This is a warning message")
logger.error("This is an error message")
You can set the minimum log level to control which messages are displayed:
// In your Application class
AndroidLogcatServiceProvider.setMinimumLoggingLevel(Level.DEBUG)
SLF4J supports parameterized logging for better performance:
// Instead of concatenating strings:
logger.info("User " + username + " logged in from " + ipAddress)
// Use parameterized logging:
logger.info("User {} logged in from {}", username, ipAddress)
Log exceptions with full stack traces:
try {
// Some code that might throw an exception
} catch (e: Exception) {
logger.error("An error occurred", e)
}
A sample application is included in the repository to demonstrate the usage of the library. See the sample-app directory for more details.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- SLF4J - The Simple Logging Facade for Java
- Android Logcat - Android's logging system