2017-02-03 68 views
1

我試圖根據原理log4j2.xml文件在我的應用程序內部飛行中創建新的記錄器配置。該代碼會解釋自己:在飛行中創建新的滾動文件LOG4j2 2.6

XML文件

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration monitorInterval="5"> 
    <Appenders> 
     <Console name="STDOUT" target="SYSTEM_OUT"> 
      <PatternLayout pattern="%-d{yyyy MMM dd HH:mm:ss,SSS} %5p %c{1} - %m%n" /> 
     </Console> 

     <RollingFile name="file" fileName="./log/application.log" 
      filePattern="./log/devfix-%-d{yyyy-MM-dd}.log"> 
      <PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" /> 
      <Policies> 
       <SizeBasedTriggeringPolicy size="100MB" /> 
      </Policies> 
      <DefaultRolloverStrategy max="10" /> 
     </RollingFile> 


     <Async name="ASYNC" bufferSize="2048"> 
      <AppenderRef ref="file" /> 
      <AppenderRef ref="STDOUT" /> 
     </Async> 

     <JMS name="jmsQueue" destinationBindingName="logQueue" 
      factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory" 
      factoryBindingName="ConnectionFactory" providerURL="tcp://xxxx:xxxxx?jms.useAsyncSend=true"> 
      <PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" /> 
     </JMS> 

     <RollingFile name="fixfile" fileName="./log/fix.log" 
      filePattern="./log/fix-%-d{yyyy-MM-dd-HH}.log"> 
      <PatternLayout pattern="%-d{yyyy MMM dd HH:mm:ss,SSS} %c{1} - %m%n" /> 
      <Policies> 
       <SizeBasedTriggeringPolicy size="50MB" /> 
      </Policies> 
      <DefaultRolloverStrategy max="10" /> 
     </RollingFile> 

    </Appenders> 

    <Loggers> 
     <Logger name="jmsLogger" additivity="false" level="OFF"> 
      <AppenderRef ref="jmsQueue" /> 
     </Logger> 

     <Logger name="org.hibernate" level="WARN"> 
     </Logger> 
     <Logger name="FIX_CONFIG" level="INFO" additivity="false"> 
      <AppenderRef ref="fixfile" /> 
     </Logger> 
     <Root level="info"> 
      <AppenderRef ref="ASYNC" /> 
     </Root> 

    </Loggers> 
</Configuration> 

所以在那之後,在代碼中,我使用的方法來創建一個新的rolligFileAppender。

private static Logger getLogger(String platformName) throws IOException { 

    /*Using a commun configuration to find the new Path for the plaform log file*/ 
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false); 
    Configuration config = ctx.getConfiguration(); 
    RollingFileAppender fixAppender = (RollingFileAppender) config.getAppender("fixfile"); 
    if(null == fixAppender) { 
     Logger logger = LogManager.getLogger(platformName); 
     logger.error("Missing fixfile log4j appender in configuration"); 
     return logger; 
    } 
    String fileName = fixAppender.getFileName(); 
    if (null != fileName && fileName.toLowerCase().endsWith(".log")) 
     try { // remove empty fix.log file created by defaults 
      new File(fileName).delete(); 
     } catch (Exception e) { 
     } 

    /*construction of the new file name depending on the platform*/ 
    int slashIndex = null != fileName ? fileName.lastIndexOf("/") : -1; 
    fileName = slashIndex >= 0 ? fileName.substring(0, slashIndex + 1) + platformName.toLowerCase() + ".log" : platformName.toLowerCase() + ".log"; 

    /*construction of the new file Pattern depending on the platform*/ 
    String fileNamePattern = fixAppender.getFileName(); 
    int slashIndexPattern = null != fileNamePattern ? fileNamePattern.lastIndexOf("/") : -1; 
    fileNamePattern = slashIndex >= 0 ? fileNamePattern.substring(0, slashIndexPattern + 1) + platformName.toLowerCase()+"-%-d{yyyy-MM-dd-HH}.log" : platformName.toLowerCase() + "-%-d{yyyy-MM-dd-HH}.log"; 

    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); 
    builder.setStatusLevel(Level.INFO); 
    builder.setConfigurationName("RollingBuilder"); 


    // create a console appender 
    AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", 
     ConsoleAppender.Target.SYSTEM_OUT); 
    appenderBuilder.add(builder.newLayout("PatternLayout") 
     .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); 
    builder.add(appenderBuilder); 



    // create a rolling file appender 
    LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout") 
     .addAttribute("pattern", " %-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n"); 
    ComponentBuilder triggeringPolicy = builder.newComponent("Policies") 
     .addComponent(builder.newComponent("TimeBasedTriggeringPolicy")); 

     appenderBuilder = builder.newAppender("file2", "RollingFile") 
     .addAttribute("fileName", fileName) 
     .addAttribute("filePattern", fileNamePattern) 
     .add(layoutBuilder) 
     .addComponent(triggeringPolicy); 
    builder.add(appenderBuilder); 

    // create the new logger 
    builder.add(builder.newLogger(platformName.toLowerCase(), Level.INFO) 
     .add(builder.newAppenderRef("file2")) 
     .addAttribute("additivity", false)); 

    builder.add(builder.newRootLogger(Level.INFO) 
     .add(builder.newAppenderRef("file2"))); 


    LoggerContext newctx = Configurator.initialize(builder.build()); 
    return newctx.getLogger(platformName.toLowerCase()); 

} 

問題是我沒有能力創建這個新的記錄器。例如,我用平臺名稱參數=「test」調用這個函數。 ./log/test.log文件已正確創建,但我無法記錄任何內容。我注意到newctx指向我的log4j2.xml文件,所以用他所有的參數進行初始化,例如,當我嘗試去調試時,最後一條語句LoggerContext newctx = Configurator.initialize(builder.build()並忽略我的指令builer.Build()。

你知道爲什麼會發生這種情況嗎?

預先感謝您。

回答