Java

Java Installation

You can follow the installation guide to install this library.

However, if you are not running on an Android application, you will need to selectively install the module and not install the forest-android module. For example, in gradle, you should just install the forest module:

dependencies {
    implementation 'com.github.sukhai.forest:forest:1.3.0'
}

Java Usage

All of the code snippets shown in this documentation use Kotlin. Unless specify, all of the classes and methods are supported in Java as well with a few workarounds:

1. You will need to use Forest.Global to access the global Forest methods. For example,

Forest.plant(DebugTree())
Forest.d("a message")

Equivalent Java code:

Forest.Global.plant(new DebugTree());
Forest.Global.d("a message");

2. You can create a Forest through the Forest.Global:

val forest = getForest(MyClass::class.java)

// With configuration
val forest = getForest(MyClass::class.java) { config ->
    config.level = Forest.Level.INFO
    config.preProcessLog { ... }
}

Equivalent Java code:

Forest forest = Forest.Global.getForest(MyClass.class);

// With configuration
Forest forest = Forest.Global.getForest(MyClass.class, config -> {
    config.setLevel(Forest.Level.INFO);
    config.setPreProcessLog(logEntry -> { ... });
});

3. You can use the same methods to log a message:

forest.w("a message")
forest.w("a message", Throwable("an exception"))
forest.w("a message", Throwable("an exception"), mapOf("key1" to "value 1", "key2" to 2))

Equivalent Java code:

forest.w("a message");
forest.w("a message", new Throwable("an exception"));

Map<String, Object> attributes = new HashMap<>();
attributes.put("key1", "value 1");
attributes.put("key2", 2);
forest.w("a message", new Throwable("an exception"), attributes);