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

io.bootique.logback.appender.AppenderFactory Maven / Gradle / Ivy

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

import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AsyncAppenderBase;
import ch.qos.logback.core.Context;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;
import io.bootique.config.PolymorphicConfiguration;

@BQConfig("Appender of a given type.")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = ConsoleAppenderFactory.class)
public abstract class AppenderFactory implements PolymorphicConfiguration {

    private String logFormat;

    public AppenderFactory() {
        this.logFormat = "%-5p [%d{ISO8601,UTC}] %thread %c{20}: %m%n%rEx";
    }

    @BQConfigProperty("Log format specification compatible with Logback framework. The default is " +
            "'%-5p [%d{ISO8601,UTC}] %thread %c{20}: %m%n%rEx'")
    public void setLogFormat(String logFormat) {
        this.logFormat = logFormat;
    }

    /**
     * @since 0.12
     * @return configured log format
     */
    public String getLogFormat() {
        return logFormat;
    }

    public abstract Appender createAppender(LoggerContext context);

    protected PatternLayout createLayout(LoggerContext context) {
        PatternLayout layout = new PatternLayout();
        layout.setPattern(logFormat);
        layout.setContext(context);

        layout.start();
        return layout;
    }

    protected Appender asAsync(Appender appender) {
        return asAsync(appender, appender.getContext());
    }

    protected Appender asAsync(Appender appender, Context context) {
        AsyncAppender asyncAppender = new AsyncAppender();
        asyncAppender.setIncludeCallerData(false);
        asyncAppender.setQueueSize(AsyncAppenderBase.DEFAULT_QUEUE_SIZE);
        asyncAppender.setDiscardingThreshold(-1);
        asyncAppender.setContext(context);
        asyncAppender.setName(appender.getName());
        asyncAppender.addAppender(appender);
        asyncAppender.start();
        return asyncAppender;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy