2016-11-12 84 views
2

我試圖將log4j2日誌保存到用戶主文件夾,因爲用戶沒有權限編寫應用程序的安裝位置。Log4j2登錄到用戶主文件夾

這是我的log4j設置的相關部分。不幸的是,它不能解析user.home,所以它會創建運行應用程序的文件夾$ user.home。

<?xml version="1.0" encoding="UTF-8"?> 
<configuration name="defaultConfiguration" status="warn" strict="true" monitorInterval="270"> 
    <properties>   
     <property name="patternlayout">%d{dd/MM/yyyy HH:mm:ss} %5p %c{1}:%L - %m%n 
     </property> 
     <property name="filename">${user.home}/niro/logs/niroDesk.log</property> 
     <property name="filenamePattern">${user.home}/niro/logs/niroDesk.log-%d{yyyy-MM-dd}.log.gz 
     </property> 
    </properties> 

    <appenders> 
     <appender name="Console" type="Console" target="SYSTEM_OUT"> 
      <layout type="PatternLayout" pattern="${patternlayout}" /> 
     </appender> 

     <!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files --> 
     <RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true"> 
      <PatternLayout pattern="${patternlayout}" /> 
      <Policies> 
       <TimeBasedTriggeringPolicy /> 
      </Policies> 
      <DefaultRolloverStrategy> 
       <Delete basePath="${user.home}/niro/logs" maxDepth="2"> 
        <IfFileName glob="niroDesk*.log.gz" /> 
        <IfLastModified age="7d" /> 
       </Delete> 
      </DefaultRolloverStrategy> 
     </RollingFile> 

     <appender name="AsyncFile" type="async" blocking="true" bufferSize="128"> 
      <appender-ref ref="File" /> 
     </appender> 
    </appenders> 
    <loggers> 
     <root level="error"> 
      <appender-ref ref="Console" /> 
      <appender-ref ref="AsyncFile" /> 
     </root> 
     <logger level="warn" name="org.springframework" additivity="false"> 
      <appender-ref ref="Console" /> 
      <appender-ref ref="AsyncFile" /> 
     </logger> 
    </loggers> 
</configuration> 


%d {DD/MM/YYYY HH:MM:SS}%5P%C {1}:%L - %米%N $ {的user.home} /尼魯/logs/niroDesk.log $ {}的user.home {/niro/logs/niroDesk.log-%d YYYY-MM-DD} .log.gz

<appenders> 
    <appender name="Console" type="Console" target="SYSTEM_OUT"> 
     <layout type="PatternLayout" pattern="${patternlayout}" /> 
    </appender> 

    <!-- Log su file ogni giorno, e mantenimento degli ultimi 5 files --> 
    <RollingFile name="File" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true" append="true"> 
     <PatternLayout pattern="${patternlayout}" /> 
     <Policies> 
      <TimeBasedTriggeringPolicy /> 
     </Policies> 
     <DefaultRolloverStrategy> 
      <Delete basePath="${user.home}/niro/logs" maxDepth="2"> 
       <IfFileName glob="niroDesk*.log.gz" /> 
       <IfLastModified age="7d" /> 
      </Delete> 
     </DefaultRolloverStrategy> 
    </RollingFile> 

    <appender name="AsyncFile" type="async" blocking="true" bufferSize="128"> 
     <appender-ref ref="File" /> 
    </appender> 
</appenders> 
<loggers> 
    <root level="error"> 
     <appender-ref ref="Console" /> 
     <appender-ref ref="AsyncFile" /> 
    </root> 
    <logger level="warn" name="org.springframework" additivity="false"> 
     <appender-ref ref="Console" /> 
     <appender-ref ref="AsyncFile" /> 
    </logger> 
</loggers> 

有一種方式解決這個問題?

回答

4

我解決了這個問題。您應該使用${sys:user.home}而不是${user.home}