I want to do two things:
- Log to console with a certain log-level
- Log to file with another log-level
Console logging seems to work just fine but the log file keeps beeing empty.
This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="logs/app.log" immediateFlush="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</appenders>
<loggers>
<logger name="filelogger" level="error">
<appender-ref ref="MyFile"/>
</logger>
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
What might be wrong?
<logger name="filelogger" level="error" >
This should be the problem. The name of the logger usually is your package name (unless you have specifically named it
filelogger
).Try
<logger name="com.yourpackage" level="error" additivity="true">
Refer Log4j2 Doc
Although Daker had put the corrected configuration file but he didn't explain it. I would like to add explanation here. As quoted in Log4j2 Documentation here, usage of <Logger> tag was not required for the given requirement. Further when you should use <Logger> tag? Read below explanation form the documentation,
I figured it out! The
<Logger>
tag shouldn't be used in this case, see Gaurang Patel's answer for details.I use
<ThresholdFilter />
and<AppenderRef level="">
to do this