use of ch.qos.logback.core.rolling.RollingFileAppender in project jmxtrans by jmxtrans.
the class TimeBasedRollingKeyOutWriter method buildAppender.
private RollingFileAppender buildAppender(Encoder encoder, RollingPolicy rollingPolicy) {
RollingFileAppender appender = new RollingFileAppender();
appender.setEncoder(encoder);
appender.setAppend(true);
appender.setContext(loggerContext);
appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender);
return appender;
}
use of ch.qos.logback.core.rolling.RollingFileAppender in project sonarqube by SonarSource.
the class LogbackHelperTest method createRollingPolicy_time.
@Test
public void createRollingPolicy_time() {
props.set("sonar.log.rollingPolicy", "time:yyyy-MM");
props.set("sonar.log.maxFiles", "20");
LoggerContext ctx = underTest.getRootContext();
LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
RollingFileAppender appender = (RollingFileAppender) policy.createAppender("SONAR_FILE");
// max 5 monthly files
TimeBasedRollingPolicy triggeringPolicy = (TimeBasedRollingPolicy) appender.getTriggeringPolicy();
assertThat(triggeringPolicy.getMaxHistory()).isEqualTo(20);
assertThat(triggeringPolicy.getFileNamePattern()).endsWith("sonar.%d{yyyy-MM}.log");
}
use of ch.qos.logback.core.rolling.RollingFileAppender in project sonarqube by SonarSource.
the class LogbackHelperTest method createRollingPolicy_size.
@Test
public void createRollingPolicy_size() {
props.set("sonar.log.rollingPolicy", "size:1MB");
props.set("sonar.log.maxFiles", "20");
LoggerContext ctx = underTest.getRootContext();
LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
Appender appender = policy.createAppender("SONAR_FILE");
assertThat(appender).isInstanceOf(RollingFileAppender.class);
// max 20 files of 1Mb
RollingFileAppender fileAppender = (RollingFileAppender) appender;
FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) fileAppender.getRollingPolicy();
assertThat(rollingPolicy.getMaxIndex()).isEqualTo(20);
assertThat(rollingPolicy.getFileNamePattern()).endsWith("sonar.%i.log");
SizeBasedTriggeringPolicy triggeringPolicy = (SizeBasedTriggeringPolicy) fileAppender.getTriggeringPolicy();
assertThat(triggeringPolicy.getMaxFileSize()).isEqualTo("1MB");
}
use of ch.qos.logback.core.rolling.RollingFileAppender in project dubbo by alibaba.
the class LogbackContainer method doInitializer.
/**
* Initializer logback
*
* @param file
* @param level
* @param maxHistory
*/
private void doInitializer(String file, String level, int maxHistory) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAndStopAllAppenders();
// appender
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
fileAppender.setContext(loggerContext);
fileAppender.setName("application");
fileAppender.setFile(file);
fileAppender.setAppend(true);
// policy
TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
policy.setContext(loggerContext);
policy.setMaxHistory(maxHistory);
policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
policy.setParent(fileAppender);
policy.start();
fileAppender.setRollingPolicy(policy);
// encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
rootLogger.addAppender(fileAppender);
rootLogger.setLevel(Level.toLevel(level));
rootLogger.setAdditive(false);
}
use of ch.qos.logback.core.rolling.RollingFileAppender in project sling by apache.
the class SlingConfigurationPrinter method getRotatedFiles.
/**
* @param app appender instance
* @param maxOldFileCount -1 if all files need to be included. Otherwise max
* old files to include
* @return sorted array of files generated by passed appender
*/
private File[] getRotatedFiles(FileAppender app, int maxOldFileCount) {
final File file = new File(app.getFile());
//are used in Logback
if (app instanceof RollingFileAppender) {
final File dir = file.getParentFile();
final String baseName = file.getName();
File[] result = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(baseName);
}
});
//Sort the files in reverse
Arrays.sort(result, Collections.reverseOrder(new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
long o1t = o1.lastModified();
long o2t = o2.lastModified();
return o1t < o2t ? -1 : (o1t == o2t ? 0 : 1);
}
}));
if (maxOldFileCount > 0) {
int maxCount = Math.min(getMaxOldFileCount(), result.length);
if (maxCount < result.length) {
File[] resultCopy = new File[maxCount];
System.arraycopy(result, 0, resultCopy, 0, maxCount);
return resultCopy;
}
}
return result;
}
//Not a RollingFileAppender then just return the actual file
return new File[] { file };
}
Aggregations