Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

tinylog – Rolling File Writer Tutorial

tinylog - Rolling File Writer Tutorial

What is rolling file adaptor for tinylog?

The rolling file writer or adaptor is a tinylog’s writer which writes logs to rolling files. It keeps rotating files and you will have latest log files as per your configuration.

Example:

Crunchify's rolling file logs - tinylog properties

Let’s get started:

Step-1. Make sure you follow tinylog’s HelloWorld tutorial

Here is a link:

Step-2. Modify tinylog.properties file

writerCrunchifyRollingFile          = rolling file
writerCrunchifyRollingFile.level    = info
writerCrunchifyRollingFile.format   = {date: HH:mm:ss.SSS} {level}: {message}
writerCrunchifyRollingFile.file     = logs/crunchifyLog_{count}.log
writerCrunchifyRollingFile.latest   = logs/crunchifyLog.log
writerCrunchifyRollingFile.charset  = UTF-8
writerCrunchifyRollingFile.buffered = false
writerCrunchifyRollingFile.policies = startup, daily: 00:00, size: 1mb
writerCrunchifyRollingFile.backups  = 100
writerCrunchifyRollingFile.convert  = gzip

Here are all tinylog’s placeholders:

Placeholder Description
{count} Consecutive number, starting with “0”
{date} Current date and time. Optionally there can be a custom date format pattern such as “{date: HH:mm:ss.SSS}”. The date format pattern is compatible with SimpleDateFormat and on Java 9 (or higher), also with DateTimeFormatter that supports milliseconds and nanoseconds. The default date format pattern is “yyyy-MM-dd_HH-mm-ss”.
{pid} Process ID of the application

Here are all tinylog’s policies:

Policies Property Value Description
Daily Policy daily Starts a new log file every day. The time can be defined in 24-hour clock time format, such as: daily: 03:00. The default is at midnight (00:00). Since tinylog 2.4, the time zone can be set explicitly (for example daily: 03:00@UTC).
Monthly Policy monthly Starts a new log file on the first day of each month. The time can be defined in 24-hour clock time format, such as: monthly: 03:00. The default is at midnight (00:00). This policy is available for tinylog 2.3 and later. Since tinylog 2.4, the time zone can be set explicitly (for example monthly: 03:00@UTC).
Size Policy size Starts a new log file when the current file exceeds the defined maximum file size. The definition of the file size is mandatory. For example, size: 10mb would start a new log file when the current exceeds 10 MB.
Startup Policy startup Starts a new log file at every startup of the application.

Step-3. Write program to generate logs

  • Create class CrunchifyTinyLogRollingFileWriter.java
package crunchify.com.java.tutorials;

import org.tinylog.Logger;

/**
 * @author Crunchify.com
 * Program: tinylog Rolling File Writer example
 *
 */
public class CrunchifyTinyLogRollingFileWriter {
    public static void main(String[] args) {

        // create hello x 3 per line
        String crunchifyRecord = "Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level! ";
        crunchifyRecord = crunchifyRecord + crunchifyRecord;

        System.out.println(crunchifyRecord);

        int crunchifyCounter = 0;
        while (crunchifyCounter < 50000) {
            Logger.info(crunchifyRecord + crunchifyCounter);
            crunchifyCounter++;
        }

        System.out.println("Completed");
    }
}

Step-4. Run above program to generate logs

Eclipse / IntelliJ IDEA console result.

Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level! Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level! 
Completed

Process finished with exit code 0

Step-5. Verify rolling file logs

  • Go to your project main folder
  • Go to logs folder
  • You will see crunchifyLog.log file and all zip files

Let me know if you face any issue running code.

The post tinylog – Rolling File Writer Tutorial appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires