Chúng tôi giải quyết vấn đề này bằng cách chuyển hướng System.err trước khi tải cấu hình và kiểm tra nếu lỗi được đăng nhập vào dòng:
class ConfigurationLoader {
class Log4jConfigStderrStream extends ByteArrayOutputStream {
private int lineCount;
private StringBuilder output;
private PrintStream underlying;
public Log4jConfigStderrStream(PrintStream underlying) {
this.lineCount = 0;
this.output = new StringBuilder("");
this.underlying = underlying;
}
@Override
public void flush() throws IOException {
String[] buffer;
synchronized (this) {
buffer = this.toString().split("\n");
super.flush();
super.reset();
for (int i = 0; i < buffer.length; i++) {
String line = buffer[i].replace("\n", "").replace("\r",
"");
if (line.length() > 0) {
this.lineCount++;
this.output.append(line);
this.output.append("\n");
}
}
}
}
public String getOutput() {
return this.output.toString();
}
public PrintStream getUnderlying() {
return this.underlying;
}
public boolean hasErrors() {
return this.lineCount == 0 ? false : true;
}
}
private String output;
public void flushOutput() {
if (!"".equals(this.output))
System.err.print(this.output);
}
public boolean loadConfiguration(String filename) {
Log4jConfigStderrStream confErr;
confErr = new Log4jConfigStderrStream(System.err);
System.setErr(new PrintStream(confErr, true));
LogManager.resetConfiguration();
DOMConfigurator.configure(filename);
System.setErr(confErr.getUnderlying());
this.output = confErr.getOutput();
return !confErr.hasErrors();
}
}
Nguồn
2012-04-18 10:04:38
Vâng, đây là nguyền rủa gây phiền nhiễu, Tôi đồng ý. Tôi không nghĩ rằng bạn có thể làm điều này, mặc dù. – skaffman
Tôi đã làm một chút googling và không có gì bật lên. Tôi đã hy vọng tràn ngăn xếp có thể cho tôi một câu trả lời nhưng tôi đoán tôi sẽ chỉ phải dựa vào đầu vào người dùng tốt nếu họ quyết định thay đổi tập tin. Cám ơn phản hồi của bạn! :) – user459811