Log4j 2.x with multiple files depending on the level

Asked

Viewed 602 times

1

I am a log4j beginner. I want to create a different file for each level of log (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’s 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 saves the level error in the file ripper_error.log. warn it does not save to any file.

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

1 answer

1


I arrived at 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.");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.