2016년 7월 21일 목요일

Adding logbackValve to embedded tomcat application

You can add logbackValve to embedded tomcat application.

- Structure













- pom.xml



    4.0.0

    org.blog.test
    logback-valve-project
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.6.RELEASE
         
    

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

        
            ch.qos.logback
            logback-access
            1.1.7
        


    




- logback-access.xml


    
        
            %fullRequest%n%n%fullResponse
        
    

    



- LogBackValveApplication.java

package org.blog.test;

import ch.qos.logback.access.tomcat.LogbackValve;
import org.apache.catalina.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class LogBackValveApplication {

    public static void main(String[] args) {
        SpringApplication.run(LogBackValveApplication.class, args);
    }

    @RequestMapping("/server/info")
    public String getServerInfo() {
        return "server-info";
    }
}


- TomcatConfig.java

package org.blog.test;

import ch.qos.logback.access.tomcat.LogbackValve;
import org.apache.catalina.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

@Configuration
public class TomcatConfig {

    @Autowired
    ResourceLoader resourceLoader;

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    ((TomcatEmbeddedServletContainerFactory) container).addContextCustomizers(new TomcatContextCustomizer() {
                        @Override
                        public void customize(Context context) {
                            LogbackValve valve = new LogbackValve();
                            valve.setFilename(resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + "logback-access.xml").getFilename());
                            context.getPipeline().addValve(valve);
                        }
                    });
                }
            }
        };
    }
}


- result

You can find the full request and response in console log.

댓글 없음 :

댓글 쓰기