logging springboot

Setup Logging using Log42 + Maven + Eclipse in Spring Boot with Example (Step by Step Guide)

Logging is one of the essential features for any kind of development to identify the bugs and for tracking the flows. Spring boot makes everything east and configurable, Even with logging. Let us get into that deeply.

Spring boots do everything with a configuration of simple artifacts id in starter dependency named spring-boot-starter-logging. This dependency supports multiple logging mechanisms.

  1. Java Util Logging
  2. Log4J2
  3. Logback

The Default Logging mechanism of Spring Boot is Logback, So if we do not mention explicitly any logging. It internally uses Logback as a default logging provider and works for you.

Since Spring Boot 2.0, the spring boot provides everything for us. This means spring-boot-starter-web depends only on spring-boot-starter-logging which will pull all the necessary logging jars for us.

  1. Logging Class
  2. Main Java Class
  3. Log42.xml
  4. application.properties

Below the Example shows the way to use Logging using Log42 + Maven + Eclipse

hierarchy logging spring boot

Log4J2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Property name="APP_LOG_ROOT">d:/temp</Property>
    </Properties>
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>
  
        <RollingFile name="file"
            fileName="${APP_LOG_ROOT}/application.log"
            filePattern="${APP_LOG_ROOT}/application-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10000KB" />
            </Policies>
            <DefaultRolloverStrategy max="1" />
        </RollingFile>
  
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console" />
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>

Pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Application .xml

package com.example.springboot;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

	 private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
	
	public static void main(String[] args) {
		LOGGER.info("Spring Boot Application is Running Successfully");
		LOGGER.error("THIS IS ERROR BLOCK.....");
		SpringApplication.run(Application.class, args);
		
	}

	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		return args -> {

			System.out.println("Let's inspect the beans provided by Spring Boot:");

			String[] beanNames = ctx.getBeanDefinitionNames();
			Arrays.sort(beanNames);
			for (String beanName : beanNames) {
				System.out.println("Bean Names : "+beanName);
			}

		};
	}

}

HelloController.java

package com.example.springboot;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	
	@GetMapping("/home")
	public String indexHome() {
		return "Greetings from Spring Boot Home!";
	}

}

Conclusion :

Spring Boot is very easy to do the configuration for logging in terms of setup, apart from Log4J2, there are other ways such as Util Logging, Log Back available as default features.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *