Skip to content

rwachters/slf4j-android-logcat-binding

Repository files navigation

SLF4J Android Logcat Binding

A lightweight and efficient SLF4J binding for Android that routes all SLF4J logging calls to Android's native Logcat system.

License: MIT API

Overview

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.

Features

  • 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+

Installation

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")
}

Usage

Basic Setup

  1. Create a logger instance in your class:
private val logger = LoggerFactory.getLogger(YourClass::class.java)
  1. 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")

Setting Minimum Log Level

You can set the minimum log level to control which messages are displayed:

// In your Application class
AndroidLogcatServiceProvider.setMinimumLoggingLevel(Level.DEBUG)

Parameterized Logging

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)

Exception Logging

Log exceptions with full stack traces:

try {
    // Some code that might throw an exception
} catch (e: Exception) {
    logger.error("An error occurred", e)
}

Sample Application

A sample application is included in the repository to demonstrate the usage of the library. See the sample-app directory for more details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Credits

About

A lightweight SLF4J binding for Android that routes logging calls to Android's native Logcat system

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages