1. Installation

Add the SLF4J API and logback-android to your module's build.gradle:

dependencies {
  implementation 'org.slf4j:slf4j-api:2.0.7'
  implementation 'com.github.tony19:logback-android:3.0.0'
}

logback-android is published as JDK-specific artifacts — pick the one that matches your minimum SDK:

ArtifactBytecodeminSdk
com.github.tony19:logback-androidJava 1126 (Android 8.0)
com.github.tony19:logback-android-jdk8Java 821 (Android 5.0)
Unit tests: if you use logback-android in local unit tests, either run them with Robolectric, or exclude logback-android from the test configuration and substitute ch.qos.logback:logback-classic. See the README for the exact snippet.

2. Configuration

On startup, logback-android looks for logback.xml in your app's assets directory (app/src/main/assets/logback.xml). If no configuration is found, logging is silently disabled — so this file is not optional. A minimal config that logs to logcat:

<configuration
  xmlns="https://tony19.github.io/logback-android/xml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://tony19.github.io/logback-android/xml https://cdn.jsdelivr.net/gh/tony19/logback-android/logback.xsd"
>
  <appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
    <tagEncoder>
      <pattern>%logger{12}</pattern>
    </tagEncoder>
    <encoder>
      <pattern>[%-20thread] %msg</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="logcat" />
  </root>
</configuration>

Prefer not to write XML by hand? The Config Editor builds this file for you interactively. Programmatic (in-code) configuration is also supported.

Appender reference

AppenderClassPurpose
Logcat ch.qos.logback.classic.android.LogcatAppender Writes to Android logcat with a configurable tag and message pattern.
File ch.qos.logback.core.FileAppender Appends to a single file on the device.
Rolling file ch.qos.logback.core.rolling.RollingFileAppender Rotates log files by time and/or size, pruning old files automatically.
SQLite ch.qos.logback.classic.android.SQLiteAppender Stores events in a SQLite database with automatic expiry (maxHistory).
Socket ch.qos.logback.classic.net.SocketAppender Streams serialized events to a remote log server.
Syslog ch.qos.logback.classic.net.SyslogAppender Sends events to a syslog daemon over UDP.
SMTP (email) ch.qos.logback.classic.net.SMTPAppender Emails a batch of recent events when an error occurs.

Special properties

These variables are resolved at runtime and can be used anywhere in your config:

PropertyResolves to
${DATA_DIR}The app's data directory (Context.getFilesDir())
${LOG_DIR}The app's log directory
${EXT_DIR}External storage directory (if available)
${PACKAGE_NAME}The application package name
${VERSION_NAME}The app's version name
${VERSION_CODE}The app's version code

Providing the Android context

Resolving the special properties requires an Android Context. By default the framework uses a reflection-based workaround, but Google's restrictions on non-SDK interfaces may break it in the future. The recommended approach is to provide the context explicitly, before any logging occurs:

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    // Assuming no logging occurs before this
    AndroidContextUtil.setApplicationContext(this);
  }
}

3. Reference material

README

The repository README: quick start, JDK-specific artifacts, unit-test setup, and providing the Android context.

Javadoc

API documentation for logback-android classes. Older versions are archived under doc/<version>.

logback manual

The upstream manual — architecture, pattern layout syntax, filters, and rolling policies all apply to logback-android.

Issue tracker

Search existing questions and answers, report bugs, or request features.

XML schema

The logback.xsd schema enables IDE validation and autocompletion for your logback.xml.

Releases

Release notes for each version, including what changed and migration notes.