tinylog – Lightweight, Simplified logging framework for Java. HelloWorld tutorial and Logging Level details

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » tinylog – Lightweight, Simplified logging framework for Java. HelloWorld tutorial and Logging Level details
Tinylog HelloWorld Complete Tutorial by crunchify.com

What is tinylog?

Tinylog is a lightweight and Simplified logging framework for so many JVM languages. It has static logger. No need to create any other logger instance per class.

Like any other standard logging framework (log4j) it also supports 5 different logging levels.

  1. TRACE
  2. DEBUG
  3. INFO – Default
  4. WARNING
  5. ERROR

Have your logging level to trace to enable all logging.

What all frameworks tinylog supports:

  • Java
  • Kotlin
  • Scala
  • other JVM language

What are the main advantages of tinylog logging framework?

  • It’s lightweight (~170kb lib)
  • It’s faster
  • It’s very simple to implement.
  • It follows standard log4j patterns
  • It’s opensource

Let’s get started on coding and sample HelloWorld Example

Step-1

Tinylog HelloWorld Tutorial - add pom.xml dependencies

Open your production project’s pom.xml file and add below two dependencies.

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

Rebuild the project.

Step-2

  • Add tinylog.properties under /resources folder.
  • File: tinylog.properties
Tinylog HelloWorld Tutorial - add tinylog.properties file under resources folder
# logs to Console
writerCrunchifyConsole        = console
writerCrunchifyConsole.format = {date: HH:mm:ss.SSS} {level}: {message}
writerCrunchifyConsole.level  = trace

# logs to File
writerCrunchifyFile    = file
writerCrunchifyFile.file  = crunchifyLog.txt
writerCrunchifyFile.level = trace

Here we have two tinylog adaptors.

  1. tinylog console adaptor
  2. tinylog file adaptor
    • This prints log in crunchifyLog.txt file
Tinylog HelloWorld Tutorial - add tinylog.properties file content

Step-3

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

import org.tinylog.Logger;

/**
 * @author crunchify.com
 * Program: tinylog Hello World Tutorial with log levels and properties
 *
 */

public class CrunchifyTinyLogFirstTutorial {

    public static void main(String[] args) {

        // Logs a message at INFO level.
        Logger.info("Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level");

        // Logs a message at TRACE level.
        Logger.trace("Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - TRACE Log Level!");

        // Logs a message at DEBUG level.
        Logger.debug("Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - DEBUG Log Level!");

        // Logs a message at WARN level.
        Logger.warn("Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - WARN Log Level!");

        // Logs a message at ERROR level.
        Logger.error("Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - ERROR Log Level!");


        String crunchifyString = "Cruncher";

        Logger.info("Howdy {}! This is Tinylog tutorial by {}.", crunchifyString, "crunchify.com");

        Logger.error("Oh.. This is not {} but it's {}", "Meta.com", "crunchify.com");

    }

}

Copy above code and save file.

Step-4

Run above program as Java Application and you will see result as below.

/Users/app/Library/Java/JavaVirtualMachines/openjdk-17.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60075:/Applications/ crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial

13:21:52.998 INFO: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level
13:21:53.001 TRACE: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - TRACE Log Level!
13:21:53.002 DEBUG: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - DEBUG Log Level!
13:21:53.002 INFO: Howdy Cruncher! This is Tinylog tutorial by crunchify.com.
13:21:53.002 WARN: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - WARN Log Level!
13:21:53.002 ERROR: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - ERROR Log Level!
13:21:53.002 ERROR: Oh.. This is not Meta.com but it's crunchify.com

Process finished with exit code 0

What all are the logging format you can use with tinylog?

All the logging format you can use with tinylog - Crunchify Tips
NameDescription
CLASSFully-qualified class name where the logging request is issued
CLASS_NAMEClass name (without package) where the logging request is issued
CONTEXTAll set values from logging context (new in tinylog 1.1)
DATEDate and time of the logging request
EXCEPTIONThrown exception (null, if none has been thrown)
FILEFilename of the Java source file from where the logging request is issued
LEVELLogging level of the created log entry
LINELine number from where the logging request is issued
MESSAGEAssociated message of the created log entry
METHODMethod name from where the logging request is issued
PACKAGEPackage where the logging request is issued
PROCESS_IDProcess ID of the application
RENDERED_LOG_ENTRYFinal rendered log entry as it would be used for text based outputs
THREAD_IDID of the current thread
THREAD_NAMEName of the current thread

How many writers you could use with tinylog?

WriterNameDescription
ConsoleWriterconsoleWrites log entries to the console
FileWriterfileWrites log entries to a defined file
JdbcWriterjdbcStores log entries in a SQL database
LogcatWriterlogcatForwards log entries to Android’s native logging system
RollingFileWriterrollingfileLike FileWriter but uses multiple files by rotating them
SharedFileWritersharedfileSupports writing of multiple instances of a program to the same file
nullnullDiscards all log entries

Log without tinylog.properties file

For reference, here is a log if you don’t have any logging framework enabled for your application.

<meta charset="utf-8"/>/Users/app/Library/Java/JavaVirtualMachines/openjdk-17.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60075:/Applications/ crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial

2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
INFO: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - INFO Log Level
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
TRACE: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - TRACE Log Level!
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
DEBUG: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - DEBUG Log Level!
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
INFO: Howdy Cruncher! This is Tinylog tutorial by crunchify.com.
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
WARN: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - WARN Log Level!
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
ERROR: Howdy Cruncher! This is App Shah and welcome to Tinylog Tutorial! - ERROR Log Level!
2021-11-08 13:28:30 [main] crunchify.com.java.tutorials.CrunchifyTinyLogFirstTutorial.main()
ERROR: Oh.. This is not Meta.com but it's crunchify.com

Process finished with exit code 0

And you are all set. Let me know if you face any issue running this tinylog tutorial.

Leave a Comment