Logging in Android with Logback

Wageesha Erangi
4 min readMar 16, 2021

Logback is one of the widely used logging framework with android development because of its faster implementation, flexibility and configurable capabilities.

In this article we will look in to how to use logback with android applications, dependency types, how to use them, basic logging configuration with logback, write logs to the external storage and how to read configurations from the external storage.

Dependency types and how to use them

SLF4J

Simple Logging Façade for Java can be added as a dependency to the developed android application and this helps to plug-in the logging implementation at the deployment time.

Logback

Simple and standard logging framework which is easily configurable. This also provides a faster and more flexible implementation than some of the logging frameworks.

logback-core and logback-classic

The logback-core which provides the core functionalities of the logging framework and logback-classic which adds more features to the core functionalities , together they provides all the logging functionalities we need with our android application.

When you use these two dependencies, first add them to the “app/build.gradle” file with the latest version. As of now the latest versions are 1.2.3 for logback-core and 1.2.3 logback-classic.

dependencies {
implementation 'org.slf4j:slf4j-api:1.7.30'
implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'ch.qos.logback:logback-core:1.2.3'
}

When using the logback-core and logback-classic for your application you need to create the logback.xml file in the path “app/src/main/resources/logback.xml”.

logback-android

logback-android is an android implementation of logback which has additional features such as logcatAppender and SQLiteAppender.

To use the logback-android with your android application add the following dependencies to the “app/build.gradle” file with its latest version.

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

When using the logback-android for your application you need to create the logback.xml file in the “app/src/main/assets/logback.xml” path.

Basic logging configuration with logback.xml

The basic structure of a logback.xml file is given below. As it in the example, there are different appenders and layouts we can use when we want to change the logging configuration of the application.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="Logcat"
class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<Pattern>%date %level %msg%n</Pattern>
</encoder>
</appender>

<appender name="File" class="ch.qos.logback.core.FileAppender">
<file>./log/test.log</file>
<encoder>
<pattern>%date %level %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="Logcat" />
<appender-ref ref="File"/>
</root>

</configuration>

Write log files to external storage

In order to write the log files inside the device, you must first check whether you have granted the application permissions in the android device to write into the internal/external storage. After granting the permission you will be able to access the device internal/external storage.

The following example shows how to write in to the external storage of your device. Using the “${EXTERNAL_STORAGE}”, the file path to the external storage can be obtained. As given in the file appender, the path can be configured as “${log_path}/log/app.log”. Even if the targeted log directory has not been already created, it will automatically get created at the application run time. Hence you do not have to manually create the log directory within your device external storage.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<property name="log_path" value="${EXTERNAL_STORAGE}"/>

<appender name="Logcat"
class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<Pattern>%date %level %msg%n<</Pattern>
</encoder>
</appender>

<appender name="File" class="ch.qos.logback.core.FileAppender">
<file>${log_path}/log/app.log</file>
<encoder>
<pattern>%date %level %msg%n<</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="Logcat" />
<appender-ref ref="File"/>
</root>

</configuration>

Read logging configuration from external storage

There are multiple ways to read the logging configuration properties from the external storage.

  1. Read multiple properties
  2. Read single properties

The following logback file example shows how to read multiple properties and single properties from two separate files. In the logging configuration we have externalized the configuration of logcat_pattern, file_pattern and the file_name properties.

To add the default configuration, we have defined those three properties within the logback.xml file as below, using the property tag.

The include tag is used to read the external config file from the target location in the device. In this example I have included two files into the logback.xml file.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<property name="log_path" value="${EXTERNAL_STORAGE}"/>

<property name="logcat_pattern" value="%d %thread %msg%n"/>
<property name="file_pattern" value="%d %thread %msg%n"/>
<property name="file_name" value="test"/>

<include file="${log_path}/log/multiple_properties.xml"/>
<include file="${log_path}/log/single_property.xml"/>

<appender name="Logcat"
class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<Pattern>${logcat_pattern}</Pattern>
</encoder>
</appender>

<appender name="File" class="ch.qos.logback.core.FileAppender">
<file>${log_path}/log/${file_name}.log</file>
<encoder>
<pattern>${file_pattern}</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="Logcat" />
<appender-ref ref="File"/>
</root>

</configuration>

When we want to import a single property from the external storage, the file can be created as follows. Inside the property tag the required property name and the value can be defined.

./log/single_property.xml

<property name="file_name" value="app"/>

In the second file multiple_properties.xml, multiple properties are defined using the included tag. Inside the included tag, using the property tag, the required properties can be defined.

./log/multiple_properties.xml

<included>
<property name="logcat_pattern" value="%d{ISO8601} %msg%n" />
<property name="file_pattern" value="%d{ISO8601} %line %msg%n"/>
</included>

We have to be cautions when we define the file we need to include within the logback.xml file. The default values must be defined before the include tag as given in the example. If not the properties defined in the external files will get overridden by the default values.

--

--

Wageesha Erangi

Computer Science & Engineering undergraduate student