I need to redirect Jersey request/response log to my log4j2.
I have Jersey logging enabled by using this code on my ApplicationJAXRS extends Application
:
@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>() {{
add(LoggingFilter.class);
}};
}
It seems that Jersey uses JUL (Java Logging) internally and the default output is STDOUT. At this moment I can see the STDOUT on Eclipse Console.
The Log4j2 documentation have a section about JDK Logging Adapter. It says
To use the JDK Logging Adapter, you must set the system property java.util.logging.manager to org.apache.logging.log4j.jul.LogManager
This must be done either through the command line (i.e., using the -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager argument) or by using System.setProperty() before any calls are made to LogManager or Logger.
To call the System.setProperty(*)
before any Logger call I've tried to put it on a @PostConstruct
in my Aplication
class.
@PostConstruct
public void init() {
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
}
I could get it to log on my logging files.
This is my log4j2.xml:
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/${name}.log"
filePattern="${log-path}/${date:yyyy-MM}/${name}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d{dd-MM-yy HH:mm:ss,SSS} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="debug"/>
<AppenderRef ref="RollingFile" level="debug"/>
</Root>
</Loggers>
</Configuration>