2013-05-29 71 views
1

我想在appender中設置logback.xml文件路徑(以及文件名) 我需要使用哪個類。請幫忙。以下是我的appender如何在appender中的logback.xml中設置文件路徑

<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> 
    <discriminator class="com.hotwaxmedia.logger.ThreadNameBasedDiscriminator"/> 
    <sift> 
     <appender class="ch.qos.logback.core.FileAppender"> 
      <file>runtime/logs/app-${threadName}.html</file> 
      <layout class="ch.qos.logback.classic.PatternLayout"> 
       <pattern>%p %d ${threadName} [%logger{0} : %-5level] %msg%n%rEx</pattern> 
      </layout> 
     </appender> 
    </sift> 
</appender> 
+1

和您遇到了什麼問題? – fGo

+0

我只想在我的java代碼中獲取文件路徑(即運行時/日誌/應用程序 - $ {threadName} .html) 。那麼是否有返回文件路徑的類或方法? – user2404847

回答

0

你可以加載附加器編程

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); 
for (Logger logger : context.getLoggerList()) { 
    for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders();  index.hasNext();) { 
    Appender<ILoggingEvent> appender = index.next(); 
    } 
} 

,那麼你可以驗證是appenderFileAppender和呼叫getFile()。看看文檔http://logback.qos.ch/apidocs/ch/qos/logback/core/FileAppender.html

0

該問題與LOGBACK-833相關,後者在logback版本1.0.12中修復。因此,您需要升級到logback版本1.0.12或更高版本。鑑別器實例必須將「threadName」定義爲要導出的密鑰。否則,$ {threadName}無法解析。下面是修改的鑑別:

<discriminator class="com.hotwaxmedia.logger.ThreadNameBasedDiscriminator"> 
    <!-- the key to export needs to be defined --> 
    <key>threadName</key> 
</discriminator> 
0

必須在 「fileNamePattern」 標記,例如用戶路徑:

<Discriminator> 

     <Key>userId</Key> 

     <DefaultValue>general</DefaultValue> 

    </Discriminator> 



    <sift> 

     <appender name="FILE-MODULENAME-${userId}" class="ch.qos.logback.core.rolling.RollingFileAppender"> 

       <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
        <!-- rollover daily --> 
        <fileNamePattern>/opt/webligic12/user_projects/domains/myDomain/servers/AdminServer/logs/%d{yyyy-MM-dd}/byUser/modulename/${userId}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern> 
        <timeBasedFileNamingAndTriggeringPolicy 
         class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 
        <!-- or whenever the file size reaches maxFileSize --> 
        <maxFileSize>2MG</maxFileSize> 
        </timeBasedFileNamingAndTriggeringPolicy> 
       </rollingPolicy> 


      <Append>true</Append> 

      <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 

       <Pattern>%d %X{user} %level %msg %n</Pattern> 

      </encoder> 

     </appender> 

    </sift> 

</appender> 
相關問題