Search in sources :

Example 1 with RollingPolicyBase

use of ch.qos.logback.core.rolling.RollingPolicyBase 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)

Aggregations

PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)1 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)1 TriggeringPolicy (ch.qos.logback.core.rolling.TriggeringPolicy)1 File (java.io.File)1