Java tinylog Rolling File Writer
Tags: tinylog
In this Java tinylog tutorial we show how to configure rolling file write with tinylog library. With different examples tinylog.properties configurations you will learn how to configure tinylog for your Java applications effectively.
Table of contents
- Add tinylog dependencies to Java project
- How to log entries using tinylog
- Configure Rolling File Writer in tinylog.properties file
- Configure Rolling File to zip the log files
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 log entries using tinylog
At this step we implement a Java application to log a lot of entries using tinylog API as below.
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.info("Simple Solution");
}
}
}
Configure Rolling File Writer in tinylog.properties file
To configure rolling file writer, we create a new file named tinylog.properties under src/main/resources/ folder in Java project.
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.charset = UTF-8
writer.policies = size: 5mb
Execute the Java application we have the log files be written as below.
Configure Rolling File to zip the log files
In the following tinylog.properties example we show you another configuration which allow different file name for latest log file and also zip the log file with 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.policies = size: 5mb
writer.convert = gzip
Execute the Java application we have the log files be written and zip as screenshot below.
Happy Coding 😊
Related Articles
Java tinylog with Multiple Writers