Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

Monday, 5 July 2021

SLF4J and log4j: hello world application

Step 1: Create new maven project ‘slf4j-log4j-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>slf4j-log4j-demo</artifactId>
    <version>1</version>

    <dependencies>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.31</version>
        </dependency>
        
    </dependencies>
</project>

 

Step 3: Create log4j.properties file under src/main/resources folder.

 

log4j.properties

log4j.rootLogger=DEBUG,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

 

Step 4: Define HelloWorld.java application.

 

HelloWorld.java

package com.sample.app;

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

public class HelloWorld {

    public static void main(String args[]) {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);

        logger.info("Welcome to SLF4J logging");
    }

}

 

Output

INFO    2021-07-05 11:08:27,768 0   com.sample.app.HelloWorld   [main]  Welcome to SLF4J logging

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/slf4j/slf4j-log4j-demo


 

 

Previous                                                    Next                                                    Home

Introduction to SLF4J

 

SLF4J stands for Simple Logging Facade for Java and it provides an abstraction to all the Java logging frameworks.

 

Using SLF4J, you can work with any of the logging libraries like Logback, java.util.logging and log4j etc., Since SLF4J provides an abstraction on logging libraries, you can switch from one library to other seamlessly.

 

Core elements of SLF4j

There are 3 top level elements in SLF4J.

a.   Logger: It is the main user entry point of SLF4J API. It captures the message along with the metadata.

b.   Formatter: Formats the log message

c.    Handler: Handle the dispatched message. For example, a file handler write the message to a file and a console handler writes the message to system console.

 

SLF4J Severity levels

Below table summarize the severity levels of log message.

 

Severity level

Description

FATAL

Used to log messages that cause application to terminate

ERROR

Used to log application error messages

WARNING

Used to log warning messages

INFO

Used to log information message

DEBUG

Used while debugging the application

TRACE

Used to get details flow analysis of the system.

 

SLF4J vs log4j

SLF4j is not an implementation of logging framework. It provides an abstraction on top of existing logging frameworks. If you use slf4j, then you can easily switch the core logging framework in future.

Previous                                                    Next                                                    Home