0

Below is my log4j2.xml file:

<?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="log" fileName="logs/log.txt">
        <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="log" level="info"/>
    </root>    
  </loggers>
</configuration>

I would like to print whatever I see on the console into my logs.txt file as well.
What am I doing wrong?

kodazys
  • 129
  • 1
  • 1
  • 9
  • Does this answer your question? [log4j2 xml configuration - Log to file and console (with different levels) ](https://stackoverflow.com/questions/17418494/log4j2-xml-configuration-log-to-file-and-console-with-different-levels) – Joy Jan 12 '20 at 06:37
  • The real question is what do you see happening? Your configuration should route everything that goes to the console to the file. However, you have the root logger level configured with debug but then have each of the appender-refs configured with info. For that you could just set the root level to info and not specify the level on the appender references. You only need the level if one appender-ref will have a different level than another. – rgoers Jan 12 '20 at 08:32

1 Answers1

0

I think You are missing the levelrangefilter tag .

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">



<Appenders>

    <Console name="Console" target="SYSTEM_OUT">
  <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

    <RollingFile name="log" fileName="logs/log.txt"
        filePattern="logs/log.txt.%i" >
        <LevelRangeFilter minLevel="FATAL" maxLevel="ALL" onMatch="ACCEPT" onMismatch="DENY"/>

    </RollingFile>


</Appenders>


   <loggers>     
    <root level="debug">
      <appender-ref ref="Console" level="info"/>
      <appender-ref ref="log"/>
    </root>    
  </loggers>

</Configuration>