Search in sources :

Example 6 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project midpoint by Evolveum.

the class TestLoggingConfiguration method test110SetMaxHistory.

// MID-5674
@Test
public void test110SetMaxHistory() throws Exception {
    // GIVEN
    Task task = taskManager.createTaskInstance(TestLoggingConfiguration.class.getName() + ".test101EnableBasicAudit");
    OperationResult result = task.getResult();
    // Setup
    PrismObject<SystemConfigurationType> systemConfiguration = PrismTestUtil.parseObject(AbstractInitializedModelIntegrationTest.SYSTEM_CONFIGURATION_FILE);
    LoggingConfigurationType logging = systemConfiguration.asObjectable().getLogging();
    applyTestLoggingConfig(logging);
    ((FileAppenderConfigurationType) logging.getAppender().get(0)).setMaxHistory(100);
    Collection<ObjectDelta<? extends ObjectType>> deltas = cast(prismContext.deltaFor(SystemConfigurationType.class).item(SystemConfigurationType.F_LOGGING).replace(logging.asPrismContainerValue().clone()).asObjectDeltas(AbstractInitializedModelIntegrationTest.SYSTEM_CONFIGURATION_OID));
    // WHEN
    modelService.executeChanges(deltas, null, task, result);
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    assertNotNull("No logger", logger);
    Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
    RollingFileAppender<ILoggingEvent> fileAppender = null;
    while (appenderIterator.hasNext()) {
        Appender<ILoggingEvent> appender = appenderIterator.next();
        System.out.println("Appender: " + appender);
        if (appender instanceof RollingFileAppender) {
            fileAppender = (RollingFileAppender<ILoggingEvent>) appender;
            break;
        }
    }
    assertNotNull("No file appender", fileAppender);
    RollingPolicy rollingPolicy = fileAppender.getRollingPolicy();
    System.out.println("Rolling policy = " + rollingPolicy);
    assertTrue("Wrong type of rolling policy", rollingPolicy instanceof TimeBasedRollingPolicy);
    TimeBasedRollingPolicy<?> timeBasedRollingPolicy = (TimeBasedRollingPolicy<?>) rollingPolicy;
    assertEquals("Wrong maxHistory", 100, timeBasedRollingPolicy.getMaxHistory());
}
Also used : Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Task(com.evolveum.midpoint.task.api.Task) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.testng.annotations.Test)

Example 7 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project sonarqube by SonarSource.

the class LogbackHelperTest method createRollingPolicy_defaults.

@Test
public void createRollingPolicy_defaults() {
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    FileAppender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    // max 5 daily files
    RollingFileAppender fileAppender = (RollingFileAppender) appender;
    TimeBasedRollingPolicy triggeringPolicy = (TimeBasedRollingPolicy) fileAppender.getTriggeringPolicy();
    assertThat(triggeringPolicy.getMaxHistory()).isEqualTo(7);
    assertThat(triggeringPolicy.getFileNamePattern()).endsWith("sonar.%d{yyyy-MM-dd}.log");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) 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 8 with TimeBasedRollingPolicy

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

the class TimeBasedRollingKeyOutWriter method initRollingPolicy.

protected RollingPolicy initRollingPolicy(String fileName, int maxBackupFiles, String maxFileSize) {
    SizeAndTimeBasedFNATP sizeTimeBasedPolicy = new SizeAndTimeBasedFNATP();
    // the max file size before rolling to a new file
    sizeTimeBasedPolicy.setMaxFileSize(maxFileSize);
    sizeTimeBasedPolicy.setContext(loggerContext);
    TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
    // set the filename pattern
    policy.setFileNamePattern(fileName);
    // the maximum number of backup files to keep around
    policy.setMaxHistory(maxBackupFiles);
    policy.setTimeBasedFileNamingAndTriggeringPolicy(sizeTimeBasedPolicy);
    policy.setContext(loggerContext);
    return policy;
}
Also used : SizeAndTimeBasedFNATP(ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 9 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project sling by apache.

the class LogWriter method createAppender.

public Appender<ILoggingEvent> createAppender(final Context context, final Encoder<ILoggingEvent> encoder) {
    SlingContextUtil ctxUtil = new SlingContextUtil(context, this);
    OutputStreamAppender<ILoggingEvent> appender;
    if (FILE_NAME_CONSOLE.equals(fileName)) {
        appender = new ConsoleAppender<ILoggingEvent>();
        appender.setName(FILE_NAME_CONSOLE);
    } else {
        ctxUtil.addInfo("Configuring appender " + getFileName());
        SlingRollingFileAppender<ILoggingEvent> rollingAppender = new SlingRollingFileAppender<ILoggingEvent>();
        rollingAppender.setAppend(true);
        rollingAppender.setFile(getFileName());
        Matcher sizeMatcher = SIZE_SPEC.matcher(getLogRotation());
        if (sizeMatcher.matches()) {
            // group 1 is the base size and is an integer number
            final long baseSize = Long.parseLong(sizeMatcher.group(1));
            // this will take the final size value
            final long maxSize;
            // group 2 is optional and is the size spec. If not null it is
            // at least one character long and the first character is enough
            // for use to know (the second is of no use here)
            final String factorString = sizeMatcher.group(2);
            if (factorString == null) {
                // no factor define, hence no multiplication
                maxSize = baseSize;
            } else {
                switch(factorString.charAt(0)) {
                    case 'k':
                    case 'K':
                        maxSize = baseSize * FACTOR_KB;
                        break;
                    case 'm':
                    case 'M':
                        maxSize = baseSize * FACTOR_MB;
                        break;
                    case 'g':
                    case 'G':
                        maxSize = baseSize * FACTOR_GB;
                        break;
                    default:
                        // we don't really expect this according to the
                        // pattern
                        maxSize = baseSize;
                }
            }
            SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
            triggeringPolicy.setMaxFileSize(String.valueOf(maxSize));
            triggeringPolicy.setContext(context);
            triggeringPolicy.start();
            rollingAppender.setTriggeringPolicy(triggeringPolicy);
            FixedWindowRollingPolicy pol = new FixedWindowRollingPolicy();
            pol.setMinIndex(1);
            pol.setMaxIndex(getLogNumber());
            pol.setFileNamePattern(getFileName() + "%i");
            pol.setContext(context);
            pol.setParent(rollingAppender);
            pol.start();
            rollingAppender.setRollingPolicy(pol);
        } else {
            TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
            String fileNamePattern = createFileNamePattern(getFileName(), getLogRotation());
            policy.setFileNamePattern(fileNamePattern);
            policy.setMaxHistory(getLogNumber());
            policy.setContext(context);
            policy.setParent(rollingAppender);
            policy.start();
            rollingAppender.setTriggeringPolicy(policy);
            ctxUtil.addInfo("Configured TimeBasedRollingPolicy with pattern " + fileNamePattern);
        }
        rollingAppender.setLogWriter(this);
        rollingAppender.setName(getAppenderName());
        appender = rollingAppender;
    }
    if (bufferedLogging && encoder instanceof LayoutWrappingEncoder) {
        ((LayoutWrappingEncoder) encoder).setImmediateFlush(false);
        ctxUtil.addInfo("Setting immediateFlush to false");
    } else {
        ctxUtil.addInfo("immediateFlush property not modified. Defaults to true");
    }
    appender.setContext(context);
    appender.setEncoder(encoder);
    appender.start();
    ctxUtil.addInfo("Completed configuring appender with name " + getFileName());
    return appender;
}
Also used : FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) Matcher(java.util.regex.Matcher) SlingContextUtil(org.apache.sling.commons.log.logback.internal.util.SlingContextUtil) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) SlingRollingFileAppender(org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 10 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project coffeenet-starter by coffeenet.

the class CoffeeNetLoggingAutoConfiguration method rollingFileAppender.

private RollingFileAppender<ILoggingEvent> rollingFileAppender(LoggerContext loggerContext) {
    RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
    rfAppender.setName(COFFEENET_FILE_APPENDER);
    rfAppender.setContext(loggerContext);
    rfAppender.setFile(coffeeNetLoggingFileProperties.getFile());
    // TimeBasedRollingPolicy
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(rfAppender);
    rollingPolicy.setFileNamePattern(coffeeNetLoggingFileProperties.getFileNamePattern());
    rollingPolicy.setMaxHistory(coffeeNetLoggingFileProperties.getMaxHistory());
    rollingPolicy.start();
    rfAppender.setRollingPolicy(rollingPolicy);
    // PatternLayoutEncoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(coffeeNetLoggingFileProperties.getPattern());
    encoder.start();
    rfAppender.setEncoder(encoder);
    rfAppender.start();
    return rfAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Aggregations

TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)19 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)15 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)13 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)11 LoggerContext (ch.qos.logback.classic.LoggerContext)10 Logger (ch.qos.logback.classic.Logger)7 Test (org.junit.Test)5 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 TriggeringPolicy (ch.qos.logback.core.rolling.TriggeringPolicy)2 GelfAppender (de.appelgriepsch.logback.GelfAppender)2 File (java.io.File)2 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 LogcatAppender (ch.qos.logback.classic.android.LogcatAppender)1 Appender (ch.qos.logback.core.Appender)1 FileAppender (ch.qos.logback.core.FileAppender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1