Search in sources :

Example 11 with RollingFileAppender

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;
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender)

Example 12 with RollingFileAppender

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");
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 13 with RollingFileAppender

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");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 14 with RollingFileAppender

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);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 15 with RollingFileAppender

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 };
}
Also used : FilenameFilter(java.io.FilenameFilter) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) File(java.io.File) Comparator(java.util.Comparator)

Aggregations

RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)19 LoggerContext (ch.qos.logback.classic.LoggerContext)11 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)10 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)9 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)6 Test (org.junit.Test)6 Logger (ch.qos.logback.classic.Logger)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)4 FileAppender (ch.qos.logback.core.FileAppender)3 FileSize (ch.qos.logback.core.util.FileSize)3 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)2 Field (java.lang.reflect.Field)2 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 SizeAndTimeBasedFNATP (ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP)1 GelfAppender (de.appelgriepsch.logback.GelfAppender)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 SimpleDateFormat (java.text.SimpleDateFormat)1