Why to use Loggers?

Logger

In Java, logging is an important feature that helps developers to trace out the errors. Java is the programming language that comes with the logging approach. It provides a Logging API that was introduced in Java 1.4 version. It provides the ability to capture the log file.

Components of Loggers

  1. Logger — recorder.
  2. Handlers or Appender — Where to send logs(console, file, etc.)
  3. Formatters or Layouts — How to display logs.

Different Loggers

  • Log4j: Apache Log4j is an open-source Java-based logging utility.
  • SLF4J: It stands for Simple Logging Facade for Java (SLF4J). It is an abstraction layer for multiple logging frameworks such as Log4j, Logback, and java.util.logging.
  • Logback: It is an open-source project designed as a successor to Log4j version 1 before Log4j version 2 was released.
  • tinylog(tinylog): It is a light weighted and open-source logger.

Why Loggers

While building your application/framework, you would often face errors which have to be debugged. So, with the help of logs, we can easily get information about what is happening within our app or framework with a record of errors and unusual circumstances. Now, it might strike your mind that, why not use the System.out.print() statement in Java. Well, the problem with these statements is that log messages will be printed only on the console. So, once you close console, automatically, all the logs will be lost. Therefore, logs will be not be stored permanently, and are displayed one by one, as it is a single-threaded environment.

In this post we would be using Logback logger library for our demo.

Logger Level Description:

level description

Maven dependency:

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<scope>test</scope>
</dependency>

logback-test.xml ( convention is to keep it in resources folder)

logback-test.xml

MainClass.java

MainClass.java

In our case we are using 2 Appenders:

  1. Console Appender : To print logs onto console.
  2. File Appender : To send logs to file.

kindly refer to logback-test.xml where we have mentioned 2 different appenders and log format.

Console print:

21:21:32.722 [main] TRACE MainClass - trace-log
21:21:32.722 [main] DEBUG MainClass - debug-log
21:21:32.722 [main] INFO MainClass - info-log
21:21:32.727 [main] WARN MainClass - warn-log
21:21:32.727 [main] ERROR MainClass - error-log

trace.log

21:21:32.722 [main] TRACE MainClass - trace-log
21:21:32.722 [main] DEBUG MainClass - debug-log
21:21:32.722 [main] INFO MainClass - info-log
21:21:32.727 [main] WARN MainClass - warn-log
21:21:32.727 [main] ERROR MainClass - error-log

--

--

--

Automation Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

5 reasons why should a beginner must learn PHP in 2022

Electronic Card Component Control System Project

Tailwind CSS autocomplete in VSCode with Ruby on Rails

Persistence Caching in Apache Spark

Remove Duplicate Element From an Array in Ruby

Export images from Jupyter Notebook with a single command

How We Maintain Separation of Concerns Using Tailwind CSS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arvind Choudhary

Arvind Choudhary

Automation Engineer

More from Medium

Important steps to get the first job as a QA

Maven Failsafe plugin vs Surefire plugin

How to do a Bug Smash

WebElementFacade In Serenity and WebElement In Selenium

WebElementFacade In Serenity selenium, Serenity vs WebElement In Selenium