3

I want to have my string added as prefix to every logging message in app. I mean, I have several java apps, and I want one to have some prefix in message like:

DATE, INFO(or other), [sample] (-->this is my added string) , rest of standard message

I have seen a several tutorials, but none of them worked properly for me. I have already included dependency in pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</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>

Do you have any trick or simple solution for that? To write some kind of wrapper, or configuration class?

xross
  • 597
  • 4
  • 9
  • 25

1 Answers1

2

Mapped Diagnostic Context (MDC) logging allows you to set attributes associated with current thread, so that SLF4J (via your logger implementation library) can log those attributes with each logging statement without it having to be specified. So what you need is to put your prefix text in MDC. If you're using Spring MVC then a way is to implement a HandlerInterceptor.

import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoggingInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MDC.put("PREFIX", "your text");
        return true;
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MDC.clear();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        MDC.clear();
    }
}

import com.tigerit.kai.education.util.LoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class BeanConfiguration {

   @Bean
   public LoggingInterceptor loggingHandlerInterceptor() {
      return new LoggingInterceptor();
   }

   @Bean
   public WebMvcConfigurerAdapter webConfigurer() {
      return new WebMvcConfigurerAdapter() {
         @Override
         public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(loggingHandlerInterceptor());
         }
      };
   }
}

And finally add following in application.properties

logging.pattern.console=%d %-4r [%thread] %-5level PREFIX=%X{PREFIX} - %msg%n
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
  • I dont know why, but it shows `"PREFIX="` (I have changed preHandle method) – xross Dec 11 '17 at 08:02
  • I suppose something is wrong with line in application.properties. I am trying to modify it – xross Dec 11 '17 at 08:14
  • If I wrote only this line in app.properties then it is looking quite well. I dont know if it should be that way? `logging.pattern.console=%d %-4r [%thread] %-5level HMAC - %msg%n` – xross Dec 11 '17 at 08:47