简体   繁体   中英

Logging data to different log files using log4j2?

I am currently working on an application which will generate 2 different log files for different purposes. Since I am new to log4j2, I am unable to achieve it. Here is my configuration file (log4j2.xml) :

<?xml version="1.0" encoding="UTF-8"?>
  <Configuration status="WARN">
    <Properties>
      <Property name="log-path">C:/Users/460681/Desktop/SourceFiles</Property>
    </Properties>
    <Appenders>
      <Console name="console-log" target="SYSTEM_OUT"> 
        <PatternLayout 
          pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
      </Console> 
      <RollingFile name="info-log" fileName="${log-path}/SplunkOADC.log"
         filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log">
         <PatternLayout>
           <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
           </pattern>
         </PatternLayout>
         <Policies>
           <TimeBasedTriggeringPolicy interval="1"
             modulate="true" />
         </Policies>
         <Filters>
           <ThresholdFilter level="error" onMatch="DENY"   onMismatch="NEUTRAL"/>
           <ThresholdFilter level="trace" onMatch="DENY"   onMismatch="NEUTRAL"/>
           <ThresholdFilter level="info" onMatch="ACCEPT"   onMismatch="DENY"/>
         </Filters>
       </RollingFile>
       <RollingFile name="error-log" fileName="${log-path}/SplunkOADC-error.log"
            filePattern="${log-path}/SplunkOADC-error-%d{yyyy-MM-dd}.log">
         <PatternLayout>
           <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
           </pattern>
         </PatternLayout>
         <Policies>
           <TimeBasedTriggeringPolicy interval="1"
              modulate="true" />
         </Policies>
         <Filters>
           <ThresholdFilter level="error" onMatch="ACCEPT"   onMismatch="DENY"/>
           <ThresholdFilter level="trace" onMatch="DENY"   onMismatch="NEUTRAL"/>
           <ThresholdFilter level="info" onMatch="DENY"   onMismatch="NEUTRAL"/>
         </Filters>
       </RollingFile>
       <RollingFile name="trace-log" fileName="${log-path}/SplunkOADC-trace.log"
          filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log">
          <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
          </PatternLayout>
          <Policies>
            <TimeBasedTriggeringPolicy interval="1"
              modulate="true" />
          </Policies>
          <Filters>
            <ThresholdFilter level="error" onMatch="DENY"   onMismatch="NEUTRAL"/>
            <ThresholdFilter level="trace" onMatch="ACCEPT"   onMismatch="DENY"/>
            <ThresholdFilter level="info" onMatch="DENY"   onMismatch="NEUTRAL"/>
          </Filters>
        </RollingFile>
     </Appenders>
     <Loggers>
       <Root level="debug">
         <AppenderRef ref="info-log" level="info"/>
         <AppenderRef ref="trace-log" level="trace"/>
       </Root>
     </Loggers>
  </Configuration>

I have tried filters but I'm not sure if it is the correct way. Here is my java method which is trying to log using log4j2

logger.entry("Enter The app");
String report_index_data =
      "select REPORT_MODE, INDEX_ID from TABLE_NAME";
ResultSet rs = db.selectQuery(report_index_data, conn);
while(rs.next()){
 logger.info("report_index_data =" +rs.getString("report_index_data"));
}
logger.exit();

Thanks!

Add a new logger to configuration.

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

    <logger name="logger2">
     <AppenderRef ref="trace-log" level="trace"/>
    </logger>

First, note that if your root logger level is debug, then it will not send trace-level log events to any of its appenders; it will only debug level or higher log events to its appenders.

Second, your code uses logger.entry() and logger.exit() . For these methods to be useful you need to use a pattern layout with location patterns like %location or %method in the pattern. (Eg [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}.%M - %msg%n ). This will display the name of the method you entered/exited. Be aware that calculating location information has some performance impact .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM