Log4j 2.x with multiple files depending on the level

1

I'm new to log4j. I want to create a different file for each log level (warn, error and etc).

I have the following xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" strict="true" name="XMLConfigTest" packages="org.apache.logging.log4j">
  <Properties>
    <Property name="ErrorFileName">c:/ripper/ripper_error.log</Property>
     <Property name="WarningFileName">c:/ripper/ripper_warning.log</Property>
  </Properties>
 
  <Appenders>
    <Appender type="Console" name="STDOUT">
      <Layout type="PatternLayout" pattern="%m MDC%X%n"/>
      <Filters>
        <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
        <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/>
      </Filters>
    </Appender>
    
    <Appender type="File" name="File" fileName="${ErrorFileName}">
      <Layout type="PatternLayout">
        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
      </Layout>
    </Appender>
    
    <Appender type="File" name="FileWarning" fileName="${WarningFileName}">
      <Layout type="PatternLayout">
        <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
      </Layout>
    </Appender>
    
  </Appenders>
 
  <Loggers>
    <Logger name="warn" level="warn">
        <AppenderRef ref="FileWarning" />
    </Logger>
    <Root>
      <AppenderRef ref="File"/>
    </Root>
  </Loggers>
</Configuration>

In my class it looks like this:

public class HashMapFactory {
final static Logger logger = LogManager.getLogger(HashMapFactory.class);

//...

logger.error("Erro! A chaveEL "+chaveEL+" está duplicada ou não há terminal cadastrado na TotalGirosLinhaEstacao.");
logger.warn("teste");
}

It only writes the error level in the file ripper_erro.log. The warn does not write to any file.

How do I have to record in ripper_warning.log the warn level events?

    
asked by anonymous 04.01.2016 / 19:34

1 answer

1

I came up with this solution:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" strict="true" name="XMLConfigTest" packages="org.apache.logging.log4j">
  <Properties>
    <Property name="Dir">/log/ripper</Property> <!-- linux -->
    <Property name="ErrorFileName">${Dir}/log/ripper_error.log</Property>
    <Property name="WarnFileName">${Dir}/log/ripper_warn.log</Property>
    <Property name="InfoFileName">${Dir}/log/ripper_info.log</Property>
    
    <Property name="pattern">%d %p %C{1.} [%t] %m%n</Property>
    <Property name="name">ripper</Property>
  </Properties>
 
  <Appenders>
    <Appender type="File" name="FileError" fileName="${ErrorFileName}">
      <Layout type="PatternLayout">
        <Pattern>${pattern}</Pattern>
      </Layout>
      <Filters>
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" />
        </Filters>
    </Appender>
    
    <Appender type="File" name="FileWarn" fileName="${WarnFileName}">
      <Layout type="PatternLayout">
        <Pattern>${pattern}</Pattern>
      </Layout>
      <Filters>
            <ThresholdFilter level="WARN" onMatch="ACCEPT" />
        </Filters>
    </Appender>
    <Appender type="File" name="FileInfo" fileName="${InfoFileName}">
      <Layout type="PatternLayout">
        <Pattern>${pattern}</Pattern>
      </Layout>
      <Filters>
            <ThresholdFilter level="INFO" onMatch="ACCEPT" />
        </Filters>
    </Appender>
  </Appenders>
 
  <Loggers>
      
    <Logger name="warnLog" level="WARN">
      <AppenderRef ref="FileWarn" level="WARN"/>
    </Logger>
    
    <Logger name="errorLog" level="ERROR">
      <AppenderRef ref="FileWarn" level="ERROR"/>
    </Logger>
    
    <Logger name="infoLog" level="INFO">
      <AppenderRef ref="FileInfo" level="INFO"/>
    </Logger>
    
    <Root>
      <AppenderRef ref="FileError" level="ERROR"/>
    </Root>
    
  </Loggers>
</Configuration>

And in my class:

final static Logger logger = LogManager.getLogger(HashMapFactory.class);
final static Logger loggerWarn = LogManager.getLogger("warnLog");

...

loggerWarn.warn("Estação não encontrada para a linha "+linha+" operada pela empresa "+empresa+" na ConsolidacaoLinhasPorEstacao.");
logger.error("Erro! Não existe movimento para a ERN neste dia no rateio exclusivo.");
    
05.01.2016 / 20:10