1. Overview

tinylog is a lightweight logging framework for Java applications and Android apps. In this tutorial, we’ll see how to issue log entries with tinylog and how to configure the logging framework. Some approaches of tinylog are very different from Log4j and Logback, as we’ll see.

2. Get Started

First, we need to add the dependencies for the tinylog API and tinylog implementation to the pom.xml file:

<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>tinylog-api</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>tinylog-impl</artifactId>
    <version>2.5.0</version>
</dependency>

Now, let’s create a new Java application and add a log statement:

package com.baeldung.tinylog;

import org.tinylog.Logger;

public class Application {

    public static void main(String[] args) {
        Logger.info("Hello World!");
    }
}

Unlike other logging frameworks, tinylog has a static logger class. Therefore, we don’t have to create a logger instance for each class in which we want to use logging. This saves us some boilerplate code. Nevertheless, tinylog can output log messages along with source code location information.

For example, if we execute our application without any configuration, the log message will be output to the console by default:

2023-01-01 14:17:42 [main] com.baeldung.tinylog.Application.main()
INFO: Hello World!

3. Logging

Usually, we want to log more than just static text, unlike what we did in the previous example. tinylog has various logging methods for different use cases. *Each logging method exists for all five supported severity levels: trace(), debug(), info(), warn(), and error()*. For the sake of readability, we’ll always use only one of them in the examples.

3.1. Placeholders

We can use text with placeholders to assemble the log message at runtime:

Logger.info("Hello {}!", "Alice"); // Hello Alice!

When logging numbers, we can format them exactly the way we want. For example, let’s output π with two decimal places:

Logger.info("π = {0.00}", Math.PI); // π = 3.14

tinylog supports lazy logging by using lambdas. If a log message or a placeholder argument is passed as lambda, it will only be resolved if the severity level is enabled. Lazy logging using lambdas can significantly improve performance if a severity level is known to be disabled in production:

Logger.debug("Expensive computation: {}", () -> compute());

3.2. Exceptions

In tinylog, exceptions are always the first argument when issuing log entries to prevent exceptions from being mixed with placeholder arguments:

try {
    int i = a / b;
} catch (Exception ex) {
    Logger.error(ex, "Cannot divide {} by {}", a, b);
}

Nevertheless, it’s also perfectly legal to log an exception without any explicit log message. In this case, tinylog automatically uses the message from the exception:

try {
    int i = a / b;
} catch (Exception ex) {
    Logger.error(ex);
}

4. Configuration

The recommended way to configure tinylog is to use a properties file. At startup, tinylog automatically looks for tinylog.properties in the default package and loads the configuration from this file. The correct location for tinylog.properties is normally src/main/resources. However, this may depend on the build tool used.

4.1. Console

In Java applications, tinylog outputs log entries to the console by default. However, we can also explicitly configure a console writer and customize the output.

Let’s enable the console writer for all log entries with the severity level info and higher. This means that trace and debug log entries aren’t output:

writer       = console
writer.level = info

As we’ve seen earlier, tinylog outputs all log entries as two-liners by default. So, let’s define a custom format pattern that can output log entries in a single line:

writer        = console
writer.format = {date: HH:mm:ss.SSS} [{level}] {class}: {message}

The {class} placeholder outputs the fully-qualified name, the {level} placeholder the severity level of the log entry, the {date} placeholder the date and/or time, and the {message} placeholder the log message (and exception if present).

*For formatting dates and times, tinylog uses SimpleDateFormat or DateTimeFormatter, depending on the Java version.* So, we can use all date and time patterns that these Java classes support.

When we execute our application from earlier now, we’ll see log output on the console:

14:17:42.452 [INFO] com.baeldung.tinylog.Application: Hello World!

4.2. Log Files

tinylog has four different file writers. In practice, the rolling file writer is usually the one used in real applications. Therefore, this tutorial focuses on this writer.

The rolling file writer can start new log files at defined events. For example, let’s configure a writer that starts a new log file at startup and every day at 6 am:

writer          = rolling file
writer.file     = logs/myapp_{date: yyyy-MM-dd}_{count}.log
writer.policies = startup, daily: 06:00
writer.format   = {class} [{thread}] {level}: {message}

For the log file path, we should use placeholders to avoid overwriting existing log files. In the above configuration, we use a placeholder for the date and another for a sequential count number. For example, if we start the application multiple times on January 1, 2023, the first log file will be logs/myapp_2023-01-01_0.log, the second one will be logs/myapp_2023-01-01_1.log, and so on.

The policies are the defined events at which a new log file should be started. The format pattern for log entries can be freely configured in the same way as for the console writer.

For saving space on the file system, we can also compress log files and limit the number of log files to be stored:

writer         = rolling file
writer.file    = logs/myapp_{date: yyyy-MM-dd}_{count}.log
writer.convert = gzip
writer.backups = 100

When gzip compression is enabled, the current log file is still a plain uncompressed text file. However, as soon as tinylog starts a new log file, the logging framework compresses the previous log file with gzip and adds .gz as the file extension. If there are more than 100 compressed backup files, tinylog starts deleting the oldest to ensure that there are never more than 100 files present.

4.3. Logcat

In Android apps, tinylog outputs log entries via Logcat by default. However, we can also explicitly configure a Logcat writer and customize the output.

First, let’s create a main activity that issues a log entry that we can use as a basis for future configuration:

package com.baeldung.tinylog;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import org.tinylog.Logger;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Logger.info("Hello World!");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Now, we can enable the Logcat writer, for example, for all log entries with the severity level debug and higher. This means that trace log entries aren’t output:

writer       = logcat
writer.level = debug

tinylog can automatically compute the tag, which we’d normally have to pass as the first parameter when using Android’s Log class. By default, the logging framework uses the simple class name without the package name as the tag. For example, tinylog uses MainActivity as the tag for all log entries issued in the class com.baeldung.tinylog.MainActivity.

However, the tag computation is freely configurable. For example, if we use a tagged logger instead of the static logger, we can use the tinylog tag also as the Logcat tag:

public class MainActivity extends AppCompatActivity {

    private final TaggedLogger logger = Logger.tag("UI");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        logger.info("Hello World!");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Now, we can reconfigure our Logcat writer to use the UI tag of the tagged logger:

writer         = logcat
writer.tagname = {tag}

4.4. Multiple Writers

We can use multiple writers at the same time in tinylog. One way we can configure multiple writers is to number them consecutively:

writer1       = console
writer1.level = debug

writer2       = rolling file
writer2.file  = myapp_{count}.log

However, we can also name our writers meaningfully:

writerConsole       = console
writerConsole.level = debug

writerFile          = rolling file
writerFile.file     = myapp_{count}.log

tinylog recognizes every property name that starts with writer. However, every writer must have a unique property name to comply with the Java standard for properties files.

4.5. Asynchronous Output

To avoid blocking the application when log entries are output, tinylog can output log entries asynchronously. This is especially recommended when using file-based writers.

We only need to enable the writing thread in our tinylog configuration to benefit from asynchronous output:

writingthread = true

With this configuration, tinylog uses a separate thread for outputting log entries.

Additionally, we can also further improve the performance of rolling file writers by enabling buffered output. By default, file-based writers write each log entry to the log file separately. However, with buffered output, tinylog can output multiple log entries to the log file in a single IO operation:

writer          = rolling file
writer.file     = myapp_{count}.log
writer.buffered = true

5. Conclusion

In this article, we’ve discussed the fundamental logging methods and configuration parameters of tinylog 2. We focused on features that are most commonly used in real applications. Furthermore, tinylog offers many more features, which we can discover in the official documentation.

As always, the examples in this tutorial are available over on GitHub.