Java tinylog Tutorial

Tags: tinylog

This Java tinylog tutorial to show you how to use the lightweight logging framework tinylog in Java applications.

Table of contents

  1. Add tinylog dependencies to Java project
  2. How to use tinylog to log messages in Java application
  3. Log messages with arguments using tinylog
  4. How to log the objects with tinylog
  5. How to log Exception objects with tinylog
  6. How to use configuration with tinylog.properties file
  7. How to write log messages to file using tinylog
  8. Rolling File Writer with tinylog
  9. How to log messages with tags in tinylog

Add tinylog dependencies to Java project

To use the tinylog library in the Gradle build project, add the following tinylog dependencies into the build.gradle file.

implementation group: 'org.tinylog', name: 'tinylog-api', version: '2.4.1'
implementation group: 'org.tinylog', name: 'tinylog-impl', version: '2.4.1'

To use the tinylog library in the Maven build project, add the following tinylog dependencies into the pom.xml file.

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

To have more information about the tinylog library you can visit the project home page at tinylog.org

How to use tinylog to log messages in Java application

With tinylog we can use the provided static methods in org.tinylog.Logger class to log messages.

There are 5 logging levels with the static method names as below.

Logger.trace("Simple Solution");
Logger.debug("Simple Solution");
Logger.info("Simple Solution");
Logger.warn("Simple Solution");
Logger.error("Simple Solution");

The Java program below to show you how the entries be logged to the console.

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        Logger.trace("Trace message");
        Logger.debug("Debug message");
        Logger.info("Information message");
        Logger.warn("Warning message");
        Logger.error("Error message");
    }
}
The output as below.
2022-04-06 22:09:12 [main] TinyLogExample.main()
TRACE: Trace message
2022-04-06 22:09:12 [main] TinyLogExample.main()
DEBUG: Debug message
2022-04-06 22:09:12 [main] TinyLogExample.main()
INFO: Information message
2022-04-06 22:09:12 [main] TinyLogExample.main()
WARN: Warning message
2022-04-06 22:09:12 [main] TinyLogExample.main()
ERROR: Error message

Log messages with arguments using tinylog

With tinylog we can use the placeholder “{}” to log the arguments as the following Java example.

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        int value1 = 10;
        int value2 = 5;

        Logger.trace("First value is {}, second value is {}", value1, value2);
        Logger.debug("First value is {}, second value is {}", value1, value2);
        Logger.info("First value is {}, second value is {}", value1, value2);
        Logger.warn("First value is {}, second value is {}", value1, value2);
        Logger.error("First value is {}, second value is {}", value1, value2);
    }
}
The output as below.
2022-04-06 22:22:32 [main] TinyLogExample.main()
TRACE: First value is 10, second value is 5
2022-04-06 22:22:32 [main] TinyLogExample.main()
DEBUG: First value is 10, second value is 5
2022-04-06 22:22:32 [main] TinyLogExample.main()
INFO: First value is 10, second value is 5
2022-04-06 22:22:32 [main] TinyLogExample.main()
WARN: First value is 10, second value is 5
2022-04-06 22:22:32 [main] TinyLogExample.main()
ERROR: First value is 10, second value is 5

How to log the objects with tinylog

The following Java example program to show how to log the Java objects using tinylog library.

TinyLogExample.java

import org.tinylog.Logger;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class TinyLogExample {
    public static void main(String... args) {
        List list = new ArrayList();
        list.add("Simple Solution");
        list.add("Java Tutorials");
        LocalDate date = LocalDate.now();
        BigDecimal bigDecimal = new BigDecimal(999.123);

        Logger.trace(list);
        Logger.debug(date);
        Logger.info(bigDecimal);
    }
}
The output as below.
2022-04-06 22:29:03 [main] TinyLogExample.main()
TRACE: [Simple Solution, Java Tutorials]
2022-04-06 22:29:03 [main] TinyLogExample.main()
DEBUG: 2022-04-06
2022-04-06 22:29:03 [main] TinyLogExample.main()
INFO: 999.12300000000004729372449219226837158203125

How to log Exception objects with tinylog

The following Java example to show how the Exception or Throwable objects be logged with tinylog.

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        int a = 1;
        int b = 0;
        try {
            int x = a / b;
        }catch (Exception ex) {
            Logger.error(ex);
        }
    }
}
The output as below.
2022-04-06 22:56:55 [main] TinyLogExample.main()
ERROR: java.lang.ArithmeticException: / by zero
	at TinyLogExample.main(TinyLogExample.java:8)

Or we can log Exception object with a message.

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        int a = 1;
        int b = 0;
        try {
            int x = a / b;
        }catch (Exception ex) {
            Logger.error(ex, "Cannot divide {} by {}", a, b);
        }
    }
}
The output as below.
2022-04-06 22:58:26 [main] TinyLogExample.main()
ERROR: Cannot divide 1 by 0: java.lang.ArithmeticException: / by zero
	at TinyLogExample.main(TinyLogExample.java:8)

How to use configuration with tinylog.properties file

With tinylog library we can configure the writers, severity level, log message format, etc. via tinylog.properties file in classpath resources folder.

For example we have resources/tinylog.properties file as below to configure writer message format , and logging level as info (log info severity level and above it).

src/main/resources/tinylog.properties

writer        = console
writer.format = {date: yyyy-MM-dd HH:mm:ss.SSS}{class}.{method}() {message}
level         = info

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        Logger.trace("Trace message");
        Logger.debug("Debug message");
        Logger.info("Information message");
        Logger.warn("Warning message");
        Logger.error("Error message");
    }
}
The output as below.
2022-04-06 23:44:45.045TinyLogExample.main() Information message
2022-04-06 23:44:45.046TinyLogExample.main() Warning message
2022-04-06 23:44:45.046TinyLogExample.main() Error message

How to write log messages to file using tinylog

At this step we show you how to configure tinylog library to log entries into file system.

To log entries to file we need to configure writer as file value in resources/tinylog.properties file, more detail in the following example code.

src/main/resources/tinylog.properties

writer        = file
writer.file   = application.log
writer.format = {date: yyyy-MM-dd HH:mm:ss.SSS}{class}.{method}() {message}

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        Logger.trace("Trace message");
        Logger.debug("Debug message");
        Logger.info("Information message");
        Logger.warn("Warning message");
        Logger.error("Error message");
    }
}
Execute the above application we have the application.log file with following content.

application.log

2022-04-06 23:50:33.775TinyLogExample.main() Trace message
2022-04-06 23:50:33.776TinyLogExample.main() Debug message
2022-04-06 23:50:33.776TinyLogExample.main() Information message
2022-04-06 23:50:33.776TinyLogExample.main() Warning message
2022-04-06 23:50:33.776TinyLogExample.main() Error message

Rolling File Writer with tinylog

In the following Java example program, we show how to configure rolling file writer in tinylog to allow the log entries to be written and zip to multiple files based on the policies config such as maximum 10MB of log file to zip to the gzip format.

src/main/resources/tinylog.properties

writer        = rolling file
writer.format = {date: yyyy-MM-dd HH:mm:ss.SSS}{class}.{method}() {message}
writer.file   = application_{count}.log
writer.latest = application_latest.log
writer.charset  = UTF-8 
writer.backups  = 100
writer.buffered = true
writer.policies = startup, daily: 00:00, size: 10mb
writer.convert  = gzip

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        for(long i = 0; i <= Long.MAX_VALUE; i++) {
            Logger.trace("Trace message");
            Logger.debug("Debug message");
            Logger.info("Information message");
            Logger.warn("Warning message");
            Logger.error("Error message");
        }
    }
}

Execute the application above we have the log file be written as following screenshot.

Rolling File Writer with tinylog

How to log messages with tags in tinylog

tinylog library also support tags the log entries to allow category the log entries.

For example, we can use tag to separate log entries to different log files as below.

src/main/resources/tinylog.properties

writer1        = file
writer1.tag    = web
writer1.file   = web.log
writer1.format = {date: yyyy-MM-dd HH:mm:ss.SSS}{class}.{method}() {message}

writer2        = file
writer2.tag    = api
writer2.file   = api.log
writer2.format = {date: yyyy-MM-dd HH:mm:ss.SSS}{class}.{method}() {message}

TinyLogExample.java

import org.tinylog.Logger;

public class TinyLogExample {
    public static void main(String... args) {
        Logger.tag("web").info("Information message");
        Logger.tag("api").info("Information message");
    }
}

Execute the application above we have 2 log files be written as screenshot below.

How to log messages with tags in tinylog

Happy Coding 😊

Java tinylog Rolling File Writer

Java tinylog with Multiple Writers

Java tinylog Tags to Different Log Files

Spring Boot logging with tinylog