TinyLog – Lightweight Java Logging Framework Tutorial

Filed Under: Java

TinyLog is a simple and lightweight logging framework for Java. We can use tinylog with Java, Kotlin, Scala, and Android applications.

Benefits of TinyLog Logging Framework

  1. Avoids boiler-plate code of Logger initialization. The Logger class in tinylog is static and used directly to log messages.
  2. Support for other popular JVM languages such as Kotlin and Scala.
  3. Support for android applications too using logcat.
  4. The tinylog jars are lightweight and small in size. The tinylog 2.x version API jar is 48 kb and implementation jar is 72 kb only.
  5. The output can be sent to Console, File, database using JDBC and DataSource.
  6. The configuration file is very simple. For simple console based logging, we don’t need any configuration file.
  7. It’s free and open source. The complete project code is hosted on GitHub.
  8. According to their own benchmarking, they are much faster than Log4J logging framework.
  9. There is no dependency on any other API and framework.
  10. Support for creating tags to categorize the log messages. It’s very useful in filtering log messages from a huge log file.
  11. Lazy Logging support to defer expensive computation only if the logging is required.

Getting Started with the TinyLog Logging

We have to include following jars into our project classpath. These are the latest versions of the tinylog framework as of writing this tutorial.

  • tinylog-api-2.0.0-RC1.jar
  • tinylog-impl-2.0.0-RC1.jar

Most of the time, we use Maven or Gradle for our project build and dependencies management.

Maven Dependencies for tinylog


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

Gradle Dependencies for tinylog


implementation 'org.tinylog:tinylog-api:2.0.0-RC1'
implementation 'org.tinylog:tinylog-impl:2.0.0-RC1'
The above implementation jar is for JVM based applications. There are separate implementation jars for Kotlin, Scala, Android, SLF4J, and JCL. You have to include the implementation JAR based on the type of your project.

Using tinylog Logger

Once the required jars are added to the project classpath, we can use its Logger class to write logging messages.

Below image shows my Eclipse project structure.

Tinylog Example Project

TinyLog Example Project

Here are the classes where I am using tinylog Logger for logging.

Employee.java: A simple model class with some properties and their getter-setter methods.


package com.journaldev.tinylog;

import org.tinylog.Logger;

public class Employee {

	private int id;
	private String name;

	public String getName() {
		Logger.debug("Returning employee name " + this.name);
		return name;
	}

	public void setName(String name) {
		Logger.info("Setting employee name " + name);
		this.name = name;
	}

	public int getId() {
		Logger.debug("Returning employee id " + this.id);
		return id;
	}

	public void setId(int id) {
		Logger.info("Setting employee id " + id);
		this.id = id;
	}
}

EmployeeService.java: A simple service class that provides a method to create an Employee object.


package com.journaldev.tinylog;

import org.tinylog.Logger;

public class EmployeeService {

	public Employee createEmployee(int i, String n) {
		Logger.info("Creating Employee with ID = " + i + " and Name = " + n);
		Employee emp = new Employee();
		emp.setId(i);
		emp.setName(n);
		return emp;
	}
	
}

EmployeeMain.java: The main class to run our simple project.


package com.journaldev.tinylog;

import org.tinylog.Logger;

public class EmployeeMain {

	public static void main(String[] args) {		
		Logger.debug("Program Started");
		
		EmployeeService empService = new EmployeeService();

		Employee emp = empService.createEmployee(10, "Pankaj");

		System.out.println("Employee ID = " + emp.getId());
		System.out.println("Employee Name = " + emp.getName());

		Logger.debug("Program Finished");
	}
}

When we run the above program, it generates the following output. Note that we haven’t created the tinylog configuration file at this stage.

Tinylog Logging Example Output Console

TinyLog Logging Example Output Console

TinyLog Logging Levels

Logger supports five logging levels – trace, debug, info, warn, and error. The default and the lowest priority level is “trace”. The highest priority level is “error”. We can set the logging level in the configuration file. The log messages of the level set and the higher priority will get logged.

TinyLog Configuration File

TinyLog looks for the tinylog.properties file in the classpath to configure logging options. We usually create this file in the resources directory. Let’s create our first tinylog configuration file.

tinylog.properties:


writer        = console
writer.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

When we run the main program again, the output is:


12:22:47.073  DEBUG: Program Started
12:22:47.076  INFO: Creating Employee with ID = 10 and Name = Pankaj
12:22:47.076  INFO: Setting employee id 10
12:22:47.077  INFO: Setting employee name Pankaj
12:22:47.077  DEBUG: Returning employee id 10
Employee ID = 10
12:22:47.077  DEBUG: Returning employee name Pankaj
Employee Name = Pankaj
12:22:47.077  DEBUG: Program Finished

Notice that the log messages are following the format mentioned in the configuration file.

TinyLog Tags

We can set Logger tag in the code using below code.


Logger.tag("MAIN").debug("Program Started");

Logger.tag("MAIN").debug("Program Finished");

The updated log messages will contain the following lines.


12:26:12.831 MAIN DEBUG: Program Started
...
12:26:12.835 MAIN DEBUG: Program Finished

We can set a writer tag to log messages having that tag, the other messages will be ignored by the writer.


writer     = console
writer.tag = SYSTEM

We can ignore all the tags and only log untagged messages by setting the tag as “-“.


writer     = console
writer.tag = -

TinyLog Configuration to log messages into a log file

Here is a simple example to log messages into tinylog.txt file.


writer       =  file
writer.file  = tinylog.txt
writer.level = info
writer.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

TinyLog supports reading environment variables too. For example, we can specify the log file to be generated at home directory using the following format.


writer.file = ${HOME}/tinylog.txt

We can also read system properties in the TinyLog configuration using the format #{key}.


writer.format = #{user.name}: {message}

Rolling File Writer Configuration


writer       = rolling file
writer.file  = tinylog{count}.txt
writer.level = info
writer.format = {date: HH:mm:ss.SSS} {tag} {level}: {message}

Extending TingLog

TinyLog is extendable. We can implement org.tinylog.writers.Writer interface and register it as a service. Similarly, we can implement org.tinylog.policies.Policy interface to create custom policies and register it. You can get more details about it here.

You can checkout the final project from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages