All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bootique.logback.policy.TimeBasedPolicyFactory Maven / Gradle / Ivy

There is a newer version: 3.0.M2
Show newest version
package io.bootique.logback.policy;

import com.fasterxml.jackson.annotation.JsonTypeName;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.TriggeringPolicy;
import ch.qos.logback.core.util.FileSize;
import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;

/**
 * A factory what defines rules for creation time-based rolling policy.
 *
 * It is not needed to add any triggering policy to appender
 * @see Logback documentation
 *
 * @since 0.10
 */
@JsonTypeName("time")
@BQConfig
public class TimeBasedPolicyFactory extends RollingPolicyFactory {

    private String totalSize;

    /**
     * Sets a maximum size of all log files combined. Equivalent to Logback
     * 'totalSizeCap' property.
     *
     * @param totalSize
     *            maximum size of all log files combined expressed in bytes,
     *            kilobytes, megabytes or gigabytes by suffixing a numeric value
     *            with KB, MB and respectively GB. For example: 5000000, 5000KB,
     *            5MB and 2GB.
     */
    @BQConfigProperty
    public void setTotalSize(String totalSize) {
        this.totalSize = totalSize;
    }

    @Override
    protected TimeBasedRollingPolicy instantiatePolicy(LoggerContext context) {
		TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy<>();
        setupBasePolicySettings(policy);
		policy.setContext(context);
		return policy;
    }

    @Override
    public TriggeringPolicy createTriggeringPolicy(LoggerContext context) {
        return null; // There is no triggering policy
    }

    @Override
    protected FileNamePatternValidator getFileNamePatternValidator(LoggerContext context) {
        return new FileNamePatternValidator(context, getFileNamePattern(), TimeBasedRollingPolicy.class.getSimpleName()) {
            @Override
            protected void validate() {
                checkPattern(true, false);
            }
        };
    }

    protected void setupBasePolicySettings(TimeBasedRollingPolicy policy) {
        policy.setFileNamePattern(getFileNamePattern());
        if (getHistorySize() > 0) {
            policy.setMaxHistory(getHistorySize());
            policy.setCleanHistoryOnStart(true);
        }
        if (totalSize != null && totalSize.length() > 0) {
            policy.setTotalSizeCap(FileSize.valueOf(totalSize));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy