2010-09-27 45 views
18

我试图以编程方式设置Logback appender路径。 (RollingFileAppender与FixedWindowRollingPolicy是精确的)以编程方式设置Logback Appender路径

我这样做是因为我想使我的用户设置首选项对话框(Eclipse RCP的)

我已经试过这样的日志路径,但我不会改变从什么配置文件中定义的日志路径:

Logger logback_logger = (ch.qos.logback.classic.Logger)LoggerFactory 
    .getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); 
RollingFileAppender<ILoggingEvent> rfappender = 
    (RollingFileAppender<ILoggingEvent>)logback_logger.getAppender("FILE"); 
rfappender.setFile(newFile); 
FixedWindowRollingPolicy rollingPolicy = 
    (FixedWindowRollingPolicy)rfappender.getRollingPolicy(); 
rollingPolicy.setFileNamePattern(newPattern); 

回答

13

使用系统属性和重新加载配置文件似乎清洁:

变化logback.xml文件:

<file>${log_path:-}myfile.log</file> 
.... 
<FileNamePattern>${log_path:-}myfile.%i.log</FileNamePattern> 

这将设置默认位置的工作目录。 然后,使用:

System.setProperty("log_path", my_log_path); 

//Reload: 
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
ContextInitializer ci = new ContextInitializer(lc); 
lc.reset(); 
try { 
    //I prefer autoConfig() over JoranConfigurator.doConfigure() so I wouldn't need to find the file myself. 
    ci.autoConfig(); 
} catch (JoranException e) { 
    // StatusPrinter will try to log this 
    e.printStackTrace(); 
} 
StatusPrinter.printInCaseOfErrorsOrWarnings(lc); 
+2

使用ContextInitializer是非常不正确的。看到我的答案是正确的方法。 – Ceki 2011-10-19 17:07:46

+1

很酷的东西!我改变了一件事:我使用lc.putProperty(“log_path”,my_log_path)而不是System.setProperty。它看起来更好,因为使用0个全局变量。 – Sasha 2015-08-04 17:11:03

5

望着的logback代码,我已经找到了解决办法:

rollingPolicy.stop(); 
rfappender.stop(); 
rollingPolicy.start(); 
rfappender.start(); 

这会导致Logback使用新的定义。不过,它仍然像是一种解决方法。

26

一旦编程配置的appender,你需要调用它的start()方法。如果appender具有子组件,请首先在子组件上调用start()。然后,将appender添加到您选择的记录器中。

下面是一个例子:

import ch.qos.logback.classic.Logger; 
import ch.qos.logback.classic.encoder.PatternLayoutEncoder; 
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy; 
import ch.qos.logback.core.rolling.RollingFileAppender; 
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; 
import ch.qos.logback.core.util.StatusPrinter; 
import org.slf4j.LoggerFactory; 
import ch.qos.logback.classic.LoggerContext; 

public class Main { 
    public static void main(String[] args) { 
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); 

    RollingFileAppender rfAppender = new RollingFileAppender(); 
    rfAppender.setContext(loggerContext); 
    rfAppender.setFile("testFile.log"); 
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); 
    rollingPolicy.setContext(loggerContext); 
    // rolling policies need to know their parent 
    // it's one of the rare cases, where a sub-component knows about its parent 
    rollingPolicy.setParent(rfAppender); 
    rollingPolicy.setFileNamePattern("testFile.%i.log.zip"); 
    rollingPolicy.start(); 

    SizeBasedTriggeringPolicy triggeringPolicy = new ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy(); 
    triggeringPolicy.setMaxFileSize("5MB"); 
    triggeringPolicy.start(); 

    PatternLayoutEncoder encoder = new PatternLayoutEncoder(); 
    encoder.setContext(loggerContext); 
    encoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n"); 
    encoder.start(); 

    rfAppender.setEncoder(encoder); 
    rfAppender.setRollingPolicy(rollingPolicy); 
    rfAppender.setTriggeringPolicy(triggeringPolicy); 

    rfAppender.start(); 

    // attach the rolling file appender to the logger of your choice 
    Logger logbackLogger = loggerContext.getLogger("Main"); 
    logbackLogger.addAppender(rfAppender); 

    // OPTIONAL: print logback internal status messages 
    StatusPrinter.print(loggerContext); 

    // log something 
    logbackLogger.debug("hello"); 
    } 
} 

上面的代码是由所述的logback的XML配置器,即Joran,当解析RollingFixedWindow.xml文件所采取的步骤的方案表达。

+4

(这对于记录日志内容有点尴尬,但是)这不是我想要做的 - 我想用XML配置我的记录器,并且只通过代码更改位置。这样,高级用户可以控制细粒度的日志记录属性,新手用户可以使用该UI。通过代码重新启动appender;使用系统属性和ContextInitializer效果更好,硬编码更少,为什么这是不正确的? – yshalbar 2012-01-25 10:28:42

+0

这种方法在最近发布的Logback中仍然有效吗? – 2014-04-02 08:45:51

+0

“RollingFixedWindow.xml”的链接已损坏 – 2014-04-02 08:47:02