log4j2 xml configuration - Log to file and console (with different levels) log4j2 xml configuration - Log to file and console (with different levels) xml xml

log4j2 xml configuration - Log to file and console (with different levels)


I figured it out! The <Logger> tag shouldn't be used in this case, see Gaurang Patel's answer for details.

<?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">        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>    </File>               </appenders>  <loggers>         <root level="debug">      <appender-ref ref="Console" level="info"/>      <appender-ref ref="MyFile" level="error"/>    </root>      </loggers></configuration>


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,

Perhaps it is desired to eliminate all the TRACE output from everything except com.foo.Bar. Simply changing the log level would not accomplish the task. Instead, the solution is to add a new logger definition to the configuration:

<Loggers>  <Logger name="com.foo.Bar" level="TRACE"/>   <Root level="ERROR">      <AppenderRef ref="STDOUT">   </Root>  ...</Loggers>


<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