on 2018 Oct 04 11:00 AM
Hi
Version: Hybris 6.6
I'm facing a problem that my new Log4j consoleAppender doesn't even log anything in console. I implemented a filter to log the sessionId on every request. But for some reason it doesn't log anything, even if it goes through the filter.
I did these configs in local.properties:
log4j.rootLogger = INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{ISO8601}] %X{sessionId} %p %c - %m%n
Even if I put these configs in project.properties, it doesn't work.
Here's the code of my filter:
public class LogSessionIdFilter extends OncePerRequestFilter {
private static final Logger LOG = Logger.getLogger(LogSessionIdFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
try {
MDC.put("sessionId", request.getSession().getId());
LOG.warn(MDC.get("sessionId"));
chain.doFilter(request, response);
} finally {
MDC.remove("sessionId");
}
}
}
The implentend "LOG.warn("sessionId");" works and I see an entry in console. But I want to the pattern configured in properties file and that's the small thing what doesn't work.
Can someone help?
Request clarification before answering.
After I updated everything to log4j2, it logged as I wanted to. To map the values, ThreadContext is used and not MDC.
Code:
import org.apache.logging.log4j.ThreadContext;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LogSessionIdFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
ThreadContext.put("sessionId", request.getSession().getId());
chain.doFilter(request, response);
} finally {
ThreadContext.clearAll();
}
}
}
And the properties file:
log4j2.console.type = Console
log4j2.appender.console.layout.type = PatternLayout
log4j2.appender.console.layout.pattern = %highlight{[%d] [%-5p] [%X{sessionId}] [%X{RemoteAddr}] [%X{Tenant}] [%X{CronJob}] [%c{1}] - %m%n}{STYLE=Logback}
log4j2.rootLogger = STDOUT
log4j2.rootLogger.level = INFO
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Miroslav,
I think it's a case that you are missing log4j.appender.CONSOLE.Target=System.out
The documentation Logging With Log4J has the following configuration:
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p (%X{Tenant}) (%X{CronJob}) [%c{1}] %m%n
Or perhaps to prefix the properties with log4j2
Regards,
Luke
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
FYI, hybris uses Log4J*2* since 6.0.
so you probably have to override / change the default logj42 console appender pattern:
log4j2.appender.console.layout.pattern = <your custom pattern here>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.