DEV Community

Arvind Choudhary
Arvind Choudhary

Posted on

Why to use Loggers?

Loggers

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

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


Different Loggers

  1. Log4j: Apache Log4j is an open-source Java-based logging utility.
  2. 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.
  3. Logback: It is an open-source project designed as a successor to Log4j version 1 before Log4j version 2 was released.
  4. 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:

Logger 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>
Enter fullscreen mode Exit fullscreen mode

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

<configuration>

    <!-- Console Appender -->
    <appender name="console-appender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


    <!-- File Appender -->
    <appender name="file-appender" class="ch.qos.logback.core.FileAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <file>trace.log</file>
        <append>false</append>
    </appender>

    <root level="ALL">
        <appender-ref ref="console-appender" />
        <appender-ref ref="file-appender" />
    </root>

</configuration>
Enter fullscreen mode Exit fullscreen mode

MainClass.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainClass {
    public static void main(String[] args) {
        // initializing logger
        Logger logger = LoggerFactory.getLogger(MainClass.class);
        logger.trace("trace-log");
        logger.debug("debug-log");
        logger.info("info-log");
        logger.warn("warn-log");
        logger.error("error-log");
    }
}
Enter fullscreen mode Exit fullscreen mode

In our case we are using 2 Appenders:
Console Appender : To print logs onto console.
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)