我很想用语法配置logback来使用SizeBasedTriggeringPolicy。
我正在尝试使用10KB的日志文件和3个文件进行测试。
由于某种原因它失败了。
我做错了什么?
这是我的代码:
class LogbackLogger implements ILogger {
private String componentName;
private Logger logger;
public LogbackLogger(String name) {
componentName = "[" + name + "]";
this.logger = ((Logger) LoggerFactory.getLogger(componentName));
}
public void info(String msg) {
logger.info(msg);
}
public void debug(String msg) {
logger.debug(msg);
}
public void warning(String msg) {
logger.warn(msg);
}
public void error(String msg) {
logger.error(msg);
}
public void error(String msg, Throwable t) {
logger.error(msg, t);
}
public void dispose() {
// Stop all appenders, threads etc. This command is take from the
// "Stopping logback-classic" section in here:
// http://logback.qos.ch/manual/configuration.html
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.stop();
}
public void setLevel(Level level)
{
this.logger.setLevel(level);
}
public void removeConsoleAppender() {
this.logger.detachAppender("Console");
}
public void addConsoleAppender() {
if (this.logger.getAppender("Console") != null) {
return;
}
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>();
consoleAppender.setName("Console");
consoleAppender.setTarget("System.out");
consoleAppender.setEncoder(getEncoder(this.logger.getLoggerContext()));
consoleAppender.setContext(this.logger.getLoggerContext());
consoleAppender.start();
this.logger.addAppender(consoleAppender);
}
public void addFileAppender(String fileName, long logLimitInKB, int fileCount) {
if (this.logger.getAppender("File") != null) {
log("FileAppender already exists");
return;
}
log("About to create FileAppender. fileName:" + fileName);
FileAppender<ILoggingEvent> fileAppender = createFileAppender(fileCount, logLimitInKB, fileName);
fileAppender.setEncoder(getEncoder(this.logger.getLoggerContext()));
fileAppender.start();
this.logger.addAppender(fileAppender);
}
private Encoder<ILoggingEvent> getEncoder(LoggerContext loggerContext) {
final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date{ISO8601} %-5level [%thread] %logger{35} %msg%n");
encoder.setOutputPatternAsHeader(false);
encoder.start();
return encoder;
}
private FileAppender<ILoggingEvent> createFileAppender(int fileCount, long logLimitInKB, String fileName) {
if (fileCount <= 1) {
log("Creating FileAppender");
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setName("File");
fileAppender.setFile(fileName);
fileAppender.setAppend(false);
fileAppender.setPrudent(false);
fileAppender.setContext(this.logger.getLoggerContext());
return fileAppender;
}
log("Creating RollingFileAppender");
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(this.logger.getLoggerContext());
fileAppender.setName("File");
fileAppender.setFile(fileName);
fileAppender.setAppend(false);
fileAppender.setPrudent(false);
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(this.logger.getLoggerContext());
rollingPolicy.setParent(fileAppender);
rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(fileCount - 1);
rollingPolicy.setFileNamePattern(fileName + "%i.log");
fileAppender.setRollingPolicy(rollingPolicy);
TriggeringPolicy<ILoggingEvent> triggerPolicy = new SizeBasedTriggeringPolicy(logLimitInKB +" kb");
fileAppender.setTriggeringPolicy(triggerPolicy);
triggerPolicy.start();
rollingPolicy.start();
return fileAppender;
}
private void log(String msg)
{
System.out.println("#############################################################");
System.out.println(msg);
System.out.println("#############################################################");
}
}
我将非常感谢你的帮助。
答案 0 :(得分:0)
最后,我的问题与配置有关。事实证明,我的问题是由于我的代码中存在错误,每个记录器都创建了一个RollingFileAppender的新实例。
当存在多个RollingFileAppenders时看同一个文件,它们都有文件的打开句柄,因此无法重命名它(因为其他的appender都有一个打开的句柄)。
我能够通过从logback打印状态信息来跟踪查找我的问题。 这是我用来打印它的代码:
private void printLogbackInternalState(String prefix){
StringBuilder sb = new StringBuilder();
sb.append("<"+ prefix+"> Internal state:" + System.lineSeparator());
for(Status status : this.logger.getLoggerContext().getStatusManager().getCopyOfStatusList()){
sb.append(status.getOrigin() + " - " + status.getMessage()+ System.lineSeparator());
}
System.out.println(sb.toString());
}
我已经在创建了一个appender的代码之后使用它然后我能够注意到我有几个实例&amp;一条错误消息,指示它无法重命名该文件。
希望这会对任何人有所帮助:)。