我使用的logback,和我想我的Java程序(类似于Setting Logback Appender path programmatically)内编程设置日志文件名,我努力去适应这种解决办法如下:的logback - 集日志文件名编程
在的logback-的test.xml:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/${log_file_name}.log</file>
...
在我的Java程序
然后又说:
String logFileName = "" + System.currentTimeMillis(); // just for example
System.setProperty("log_file_name", logFileName);
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);
然而结果是两个记录,一个完整的,命名为我想要的,例如“131904 1145343.log“,另一个为空,并命名为”log_file_name_IS_UNDEFINED.log“。如何停止创建其他空白日志文件?
你的代码的唯一问题似乎是你正在设置'System.setProperty(“log_file_name”,logFileName)''太迟了。在Logback autoconfig执行之前执行它,你有你想要的。 – Robert
它实际上可以做得比在你的代码片段中容易得多:http://stackoverflow.com/a/21886071/709537 –