Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

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
Name Description
CLASS Fully-qualified class name where the logging request is issued
CLASS_NAME Class name (without package) where the logging request is issued
CONTEXT All set values from logging context (new in tinylog 1.1)
DATE Date and time of the logging request
EXCEPTION Thrown exception (null, if none has been thrown)
FILE Filename of the Java source file from where the logging request is issued
LEVEL Logging level of the created log entry
LINE Line number from where the logging request is issued
MESSAGE Associated message of the created log entry
METHOD Method name from where the logging request is issued
PACKAGE Package where the logging request is issued
PROCESS_ID Process ID of the application
RENDERED_LOG_ENTRY Final rendered log entry as it would be used for text based outputs
THREAD_ID ID of the current thread
THREAD_NAME Name of the current thread

How many writers you could use with tinylog?

Writer Name Description
ConsoleWriter console Writes log entries to the console
FileWriter file Writes log entries to a defined file
JdbcWriter jdbc Stores log entries in a SQL database
LogcatWriter logcat Forwards log entries to Android’s native logging system
RollingFileWriter rollingfile Like FileWriter but uses multiple files by rotating them
SharedFileWriter sharedfile Supports writing of multiple instances of a program to the same file
null null Discards 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.



/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.

The post tinylog – Lightweight, Simplified logging framework for Java. HelloWorld tutorial and Logging Level details appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires