2012-03-01 116 views
1

在變量替換期間logback是否讀取環境變量?我在應用程序啓動過程中在控制檯上獲得了REM_HOME_IS_UNDEFINED。雖然變量存在。手冊說env變量在食物鏈中最後被查找。 http://logback.qos.ch/manual/configuration.html#variableSubstitution無法讀取logback中的env變量

下面是我在做什麼:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
    <file>${REM_HOME}/reac/logs/reac.log</file> 

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 
     <fileNamePattern>${REM_HOME}/reac/logs/archives/reac-%i.log.zip</fileNamePattern> 
     <minIndex>1</minIndex> 
     <maxIndex>3</maxIndex> 
    </rollingPolicy> 

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 
     <maxFileSize>5MB</maxFileSize> 
    </triggeringPolicy> 

    <encoder> 
     <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} %-5level - %msg%n</pattern> 
    </encoder>  
</appender> 

回答

2

這裏是我最後做:

我讀的環境變量並調用System.setProperty將值設置爲系統變量,重置loggercontext。

System.setProperty(REM_HOME, remHome); 

     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); 

     try { 
      JoranConfigurator configurator = new JoranConfigurator(); 
      configurator.setContext(context); 

      // Call context.reset() to clear any previous configuration, e.g. default 
      // configuration. For multi-step configuration, omit calling context.reset() 
      context.reset(); 
      configurator.doConfigure(getClass().getClassLoader().getResourceAsStream(LOG_CONF_FILE)); 
     } 
     catch (JoranException je) { 
      // StatusPrinter will handle this 
      je.printStackTrace(); 
     } 
     StatusPrinter.printInCaseOfErrorsOrWarnings(context); 
0

顯然,這個錯誤說,你的java程序不能老是明白這個變量(REM_HOME),所以你有兩個解決方案添加新的環境變量稱爲REM_HOME或作出新的屬性文件,並告訴的logback cofig文件來使用它(添加下面一行到你的代碼)

<property file="path_to_your_File/YourFileName.properties"/> 

,然後在屬性文件中定義要使用就像波紋管的vriable:

REM_HOME=Path_to_your_location