Kotlin SpringBoot configure Apache Log4j2

Apache Log4j2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture. The tutorial will introduce way to configure Apache Log4j2 with Kotlin SpringBoot.

I. Technologies

– Java 1.8
– Maven 3.6.1
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot – 1.5.9.RELEASE
– Kotlin

II. Practice

We use SpringToolSuite to create a Kotlin SpringBoot project as below structure:

Kotlin SpringBoot configure Apache Log4j2 - project structure

Step to do:
– Configure SpringBoot Log4j2 dependencies
– Create a Simple Controller
– Configure Log4j2.xml
– Build & Run

1. Configure SpringBoot Log4j2 dependencies

– Exclude logback from default log dependency of SpringBoot:


	org.springframework.boot
	spring-boot-starter
	
		
			org.springframework.boot
			spring-boot-starter-logging
		
	

– Add Spring Web MVC dependency:


	org.springframework.boot
	spring-boot-starter-web

– Add Log4j2 dependency


	org.springframework.boot
	spring-boot-starter-log4j2

2. Create a Simple Controller

We define a logger which is ready for showing messages with 4 mode: Debug, Info, Warn, Error

package com.javasampleapproach.log4j2.controller

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class SimpleController {
	
    private val logger = LoggerFactory.getLogger(SimpleController::class.java)
	
    @RequestMapping("/")
    fun hello() : String{
        logger.debug("Debug message");
        logger.info("Info message");
        logger.warn("Warn message");
        logger.error("Error message");
        return "Done";
    }
}
3. Configure Log4j2.xml

– Create a log4j2.xml file for configuration log4j2 (Spring will understand that the “log4j2.xml” file is a configuration for log4j2).

– Create 3 Loggers:

...

        
            
            
        
        
            
            
         
        
            
        

...

– With 3 Appenders:

...

 
    
	
		
			
				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
			
			>
		
	
 
    
	
		
			
				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
			
		
	
 
   
	
		
			
				[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
			
		
	

...

– Full Details:



	
		logs
	
	
		
			
				
					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
				
				>
			
		
		
			
				
					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
				
			
		
		
			
				
					[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
				
			
		
	
	
		
			
			
		
		
			
			
		
		
			
		
	

4. Build & Run

Run the SpringBoot project as a commandline mvn spring-boot:run.
Then make a request http://localhost:8080/:
-> Console logs:

...

[INFO ] 2017-12-06 20:30:50.289 [http-nio-8080-exec-1] SimpleController - Info message
[WARN ] 2017-12-06 20:30:50.301 [http-nio-8080-exec-1] SimpleController - Warn message
[ERROR] 2017-12-06 20:30:50.301 [http-nio-8080-exec-1] SimpleController - Error message

Note: Because levellog = INFO so DEBUG log is missed.

-> Log files:

Kotlin SpringBoot configure Apache Log4j2 - results

  • app_log.log
  • [INFO ] 2017-12-06 20:30:50.289 [http-nio-8080-exec-1] SimpleController - Info message
    [WARN ] 2017-12-06 20:30:50.301 [http-nio-8080-exec-1] SimpleController - Warn message
    [ERROR] 2017-12-06 20:30:50.301 [http-nio-8080-exec-1] SimpleController - Error message
    
  • springboot_log.log
  • [INFO ] 2017-12-06 20:30:35.035 [localhost-startStop-1] ContextLoader - Root WebApplicationContext: initialization completed in 1279 ms
    [INFO ] 2017-12-06 20:30:35.452 [main] RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6cd3a7ac: startup date [Wed Dec 06 20:30:33 ICT 2017]; root of context hierarchy
    [INFO ] 2017-12-06 20:30:35.528 [main] RequestMappingHandlerMapping - Mapped "{[/]}" onto public java.lang.String com.javasampleapproach.log4j2.controller.SimpleController.hello()
    [INFO ] 2017-12-06 20:30:35.531 [main] RequestMappingHandlerMapping - Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    [INFO ] 2017-12-06 20:30:35.532 [main] RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    [INFO ] 2017-12-06 20:30:35.555 [main] SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    [INFO ] 2017-12-06 20:30:35.556 [main] SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    [INFO ] 2017-12-06 20:30:35.585 [main] SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    [INFO ] 2017-12-06 20:30:50.216 [http-nio-8080-exec-1] DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
    [INFO ] 2017-12-06 20:30:50.239 [http-nio-8080-exec-1] DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 23 ms
    

    III. Sourcecode

    SpringBootKotlinLog4J2



    By grokonez | December 6, 2017.


    Related Posts


    Got Something To Say:

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

    *