Search in sources :

Example 1 with TriggeringPolicy

use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.

the class AppenderUtilsTest method testCreateRollingFileAppenderNonDailyRolling.

@Test
public void testCreateRollingFileAppenderNonDailyRolling() {
    when(_settings.isRollDaily()).thenReturn(Boolean.FALSE);
    when(_settings.isCompressOldFiles()).thenReturn(Boolean.FALSE);
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);
    assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());
    RollingPolicy rollingPolicy = appender.getRollingPolicy();
    final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
    assertTrue("Unexpected rolling policy", condition2);
    rollingPolicy = ((RollingPolicyDecorator) rollingPolicy).getDecorated();
    final boolean condition1 = rollingPolicy instanceof AppenderUtils.SimpleRollingPolicy;
    assertTrue("Unexpected decorated rolling policy", condition1);
    assertEquals("Unexpected max history", (long) MAX_HISTORY, (long) ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getMaxIndex());
    assertEquals("Unexpected file name pattern", _testLogFileName + ".%i", ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getFileNamePattern());
    assertEquals("Unexpected compression mode", CompressionMode.NONE, rollingPolicy.getCompressionMode());
    TriggeringPolicy triggeringPolicy = appender.getTriggeringPolicy();
    assertEquals("Unexpected triggering policy", String.valueOf(MAX_FILE_SIZE) + " MB", ((AppenderUtils.SizeTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());
    Encoder encoder = appender.getEncoder();
    final boolean condition = encoder instanceof PatternLayoutEncoder;
    assertTrue("Unexpected encoder", condition);
    assertEquals("Unexpected layout pattern", LAYOUT, ((PatternLayoutEncoder) encoder).getPattern());
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) Encoder(ch.qos.logback.core.encoder.Encoder) Test(org.junit.Test)

Example 2 with TriggeringPolicy

use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.

the class AppenderUtilsTest method testCreateRollingFileAppenderDailyRolling.

@Test
public void testCreateRollingFileAppenderDailyRolling() {
    final RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);
    assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());
    RollingPolicy rollingPolicy = appender.getRollingPolicy();
    final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
    assertTrue("Unexpected rolling policy", condition2);
    rollingPolicy = ((RollingPolicyDecorator) rollingPolicy).getDecorated();
    final boolean condition1 = rollingPolicy instanceof TimeBasedRollingPolicy;
    assertTrue("Unexpected decorated rolling policy", condition1);
    assertEquals("Unexpected max history", (long) MAX_HISTORY, (long) ((TimeBasedRollingPolicy) rollingPolicy).getMaxHistory());
    assertEquals("Unexpected file name pattern", _testLogFileName + ".%d{yyyy-MM-dd}.%i.gz", ((TimeBasedRollingPolicy) rollingPolicy).getFileNamePattern());
    assertEquals("Unexpected compression mode", CompressionMode.GZ, rollingPolicy.getCompressionMode());
    TriggeringPolicy triggeringPolicy = ((TimeBasedRollingPolicy) rollingPolicy).getTimeBasedFileNamingAndTriggeringPolicy();
    final boolean condition = triggeringPolicy instanceof AppenderUtils.DailyTriggeringPolicy;
    assertTrue("Unexpected triggering policy", condition);
    assertEquals("Unexpected triggering policy", String.valueOf(MAX_FILE_SIZE) + " MB", ((AppenderUtils.DailyTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());
    assertEquals("Unexpected layout", LAYOUT, ((PatternLayoutEncoder) appender.getEncoder()).getPattern());
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 3 with TriggeringPolicy

use of ch.qos.logback.core.rolling.TriggeringPolicy in project qpid-broker-j by apache.

the class AppenderUtils method configureRollingFileAppender.

public static void configureRollingFileAppender(FileLoggerSettings fileLoggerSettings, Context loggerContext, RollingFileAppender<ILoggingEvent> appender) {
    String fileName = fileLoggerSettings.getFileName();
    File file = new File(fileName);
    if (file.getParentFile() != null) {
        file.getParentFile().mkdirs();
    }
    validateLogFilePermissions(file);
    validateMaxFileSize(fileLoggerSettings.getMaxFileSize());
    appender.setFile(fileName);
    appender.setAppend(true);
    appender.setContext(loggerContext);
    TriggeringPolicy triggeringPolicy;
    RollingPolicyBase rollingPolicy;
    final String maxFileSizeAsString = String.valueOf(fileLoggerSettings.getMaxFileSize()) + "MB";
    if (fileLoggerSettings.isRollDaily()) {
        DailyTriggeringPolicy dailyTriggeringPolicy = new DailyTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
        dailyTriggeringPolicy.setContext(loggerContext);
        TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<>();
        timeBasedRollingPolicy.setMaxHistory(fileLoggerSettings.getMaxHistory());
        timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(dailyTriggeringPolicy);
        timeBasedRollingPolicy.setFileNamePattern(fileName + ".%d{yyyy-MM-dd}.%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
        rollingPolicy = timeBasedRollingPolicy;
        triggeringPolicy = dailyTriggeringPolicy;
    } else {
        SizeTriggeringPolicy sizeTriggeringPolicy = new SizeTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
        sizeTriggeringPolicy.setContext(loggerContext);
        SimpleRollingPolicy simpleRollingPolicy = new SimpleRollingPolicy(fileLoggerSettings.getMaxHistory());
        simpleRollingPolicy.setFileNamePattern(fileName + ".%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
        rollingPolicy = simpleRollingPolicy;
        triggeringPolicy = sizeTriggeringPolicy;
    }
    rollingPolicy.setContext(loggerContext);
    RollingPolicyDecorator decorator = new RollingPolicyDecorator(rollingPolicy, fileLoggerSettings.getRolloverListener(), fileLoggerSettings.getExecutorService());
    decorator.setParent(appender);
    appender.setRollingPolicy(decorator);
    appender.setTriggeringPolicy(triggeringPolicy);
    decorator.start();
    triggeringPolicy.start();
    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(fileLoggerSettings.getLayout());
    encoder.setContext(loggerContext);
    encoder.start();
    appender.setEncoder(encoder);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) File(java.io.File) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) RollingPolicyBase(ch.qos.logback.core.rolling.RollingPolicyBase) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 4 with TriggeringPolicy

use of ch.qos.logback.core.rolling.TriggeringPolicy in project jmxtrans by jmxtrans.

the class KeyOutWriter method buildAppender.

protected Appender buildAppender(String loggerName, String fileStr) {
    final RollingFileAppender appender = new RollingFileAppender();
    appender.setName(loggerName + "RollingFile");
    appender.setContext(loggerContext);
    appender.setImmediateFlush(true);
    appender.setBufferSize(new FileSize(LOG_IO_BUFFER_SIZE_BYTES));
    appender.setFile(fileStr);
    appender.setEncoder(buildEncoder());
    TriggeringPolicy triggeringPolicy = buildTriggeringPolicy();
    if (triggeringPolicy != null) {
        appender.setTriggeringPolicy(triggeringPolicy);
    }
    appender.setRollingPolicy(buildRollingPolicy(appender, fileStr));
    appender.start();
    return appender;
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) TriggeringPolicy(ch.qos.logback.core.rolling.TriggeringPolicy) FileSize(ch.qos.logback.core.util.FileSize)

Aggregations

TriggeringPolicy (ch.qos.logback.core.rolling.TriggeringPolicy)4 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)3 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)3 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)3 LoggerContext (ch.qos.logback.classic.LoggerContext)2 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)2 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)2 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 Test (org.junit.Test)2 Encoder (ch.qos.logback.core.encoder.Encoder)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1 FileSize (ch.qos.logback.core.util.FileSize)1 File (java.io.File)1