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

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>

Summary others (@basiljames, @daker, @Jay Taylor) answers here:

my log4j2.xml Configuration

my case:

  • log4j2 version: 2.13.0

  • log4j2.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FILEOUT" fileName="your_log_filename.log" append="false">
      <PatternLayout>
        <Pattern>%d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F%L - %m%n</Pattern>
      </PatternLayout>
    </File>

    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p %F:%L - %m%n"/>
    </Console>
  </Appenders>

  <Loggers>
    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
  </Loggers>
</Configuration>

Q: how to implement File and Console with different level ?

A: as official doc also mentioned, the core part:

    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>

can achieve:

based on:

  • root level is DEBUG

set:

  • File level to DEBUG
  • Console level to INFO

Q: @Stealth Rabbi: so what was the problem?

A: original main problem is:

the logger name syntax is wrong

that is name="filelogger" in:

    <logger name="filelogger" level="error">
        <appender-ref ref="MyFile"/>
    </logger>

for normally the logger name is your class name, like

    <Logger name="com.foo.bar" level="error">
        <AppenderRef ref="MyFile"/>
    </Logger>

another possible minor problem is:

log content level is below your file level error, so file created but empty

when set file level to error, but your log code is low level, such as

logger.debug("output something");

that is:

logger lever in code(debug) < file level(error)

so debug content will NOT output to log file, log file keep empty.

Q: @Bendemann is it possible to specify a path where I want to save the file if I am running a war file on a tomcat server?

A: yes. Just set the relative or absolute log file path to File's fileName.

just like:

  <Appenders>
    <File name="FILEOUT" fileName="/your/path/your_log_filename.log" append="false">
...

is ok.


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>

<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

Tags:

Xml

Log4J

Log4J2