Comprehensive SLF4J implementation providing enterprise-grade logging with flexible configuration, high performance, and extensive appender ecosystem for Java applications.
—
Output destinations for log events including console, file, network, and specialized appenders. Appenders are responsible for writing log events to their final destinations with appropriate formatting and filtering.
Base interface and functionality shared by all appenders.
/**
* Core appender interface for writing log events
*/
public interface Appender<E> extends LifeCycle, ContextAware, FilterAttachable<E> {
String getName();
void setName(String name);
void doAppend(E event);
}
/**
* Base implementation providing common appender functionality
*/
public abstract class AppenderBase<E> extends ContextAwareBase implements Appender<E> {
protected volatile boolean started = false;
private String name;
private FilterAttachableImpl<E> fai = new FilterAttachableImpl<E>();
public String getName();
public void setName(String name);
public void start();
public void stop();
public boolean isStarted();
// Template method for actual appending
protected abstract void append(E eventObject);
// Filter management
public void addFilter(Filter<E> newFilter);
public void clearAllFilters();
public List<Filter<E>> getCopyOfAttachedFiltersList();
public FilterReply getFilterChainDecision(E event);
}
/**
* Interface for appenders that can have other appenders attached
*/
public interface AppenderAttachable<E> {
void addAppender(Appender<E> newAppender);
Iterator<Appender<E>> iteratorForAppenders();
Appender<E> getAppender(String name);
boolean isAttached(Appender<E> appender);
void detachAndStopAllAppenders();
boolean detachAppender(Appender<E> appender);
boolean detachAppender(String name);
}Simple console output appender for development and simple deployments.
/**
* Appender that writes to System.out or System.err
*/
public class ConsoleAppender<E> extends OutputStreamAppender<E> {
public static final String SYSTEM_OUT = "System.out";
public static final String SYSTEM_ERR = "System.err";
private ConsoleTarget target = ConsoleTarget.SystemOut;
private boolean withJansi = false;
/**
* Set the console target (System.out or System.err)
*/
public void setTarget(String value);
public String getTarget();
/**
* Enable/disable JANSI for Windows console colors
*/
public boolean isWithJansi();
public void setWithJansi(boolean withJansi);
}
/**
* Console target enumeration
*/
public enum ConsoleTarget {
SystemOut("System.out"),
SystemErr("System.err");
public static ConsoleTarget findByName(String name);
}Usage Examples:
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
// Create console appender
ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
appender.setContext(loggerContext);
appender.setName("STDOUT");
appender.setTarget("System.out");
// Configure encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.start();
appender.setEncoder(encoder);
appender.start();High-performance asynchronous logging appender that processes events in background threads.
/**
* Asynchronous appender for high-performance logging
*/
public class AsyncAppender extends AsyncAppenderBase<ILoggingEvent> {
boolean includeCallerData = false;
/**
* Enable/disable caller data extraction (expensive operation)
*/
public boolean isIncludeCallerData();
public void setIncludeCallerData(boolean includeCallerData);
// Inherited from AsyncAppenderBase
public void setQueueSize(int queueSize);
public int getQueueSize();
public void setDiscardingThreshold(int discardingThreshold);
public int getDiscardingThreshold();
public void setMaxFlushTime(int maxFlushTime);
public int getMaxFlushTime();
public void setNeverBlock(boolean neverBlock);
public boolean isNeverBlock();
}
/**
* Base class for asynchronous appenders
*/
public abstract class AsyncAppenderBase<E> extends AppenderBase<E> implements AppenderAttachable<E> {
public static final int DEFAULT_QUEUE_SIZE = 256;
public static final int UNDEFINED = -1;
public static final int DEFAULT_MAX_FLUSH_TIME = 1000;
BlockingQueue<E> blockingQueue;
AppenderAttachableImpl<E> aai = new AppenderAttachableImpl<E>();
int queueSize = DEFAULT_QUEUE_SIZE;
int appenderCount = 0;
int discardingThreshold = UNDEFINED;
boolean neverBlock = false;
Worker worker = new Worker();
/**
* Set the maximum number of events in the queue
*/
public void setQueueSize(int queueSize);
public int getQueueSize();
/**
* Set threshold for discarding events when queue is full
*/
public void setDiscardingThreshold(int discardingThreshold);
public int getDiscardingThreshold();
/**
* Set maximum time to wait for queue flush on shutdown
*/
public void setMaxFlushTime(int maxFlushTime);
public int getMaxFlushTime();
/**
* Configure non-blocking behavior when queue is full
*/
public void setNeverBlock(boolean neverBlock);
public boolean isNeverBlock();
}Usage Examples:
import ch.qos.logback.classic.AsyncAppender;
// Create async appender
AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setContext(loggerContext);
asyncAppender.setName("ASYNC");
// Configure queue settings
asyncAppender.setQueueSize(512);
asyncAppender.setDiscardingThreshold(20);
asyncAppender.setMaxFlushTime(5000);
asyncAppender.setNeverBlock(false);
asyncAppender.setIncludeCallerData(false); // For performance
// Add delegate appender
asyncAppender.addAppender(consoleAppender);
asyncAppender.start();File-based appenders with rolling policies for log file management.
/**
* Base class for file-based appenders
*/
public class FileAppender<E> extends OutputStreamAppender<E> {
public static final long DEFAULT_BUFFER_SIZE = 8192;
protected String fileName = null;
private boolean append = true;
private boolean prudent = false;
/**
* Set the target file name
*/
public void setFile(String file);
public String getFile();
/**
* Configure append mode vs overwrite
*/
public void setAppend(boolean append);
public boolean isAppend();
/**
* Enable/disable prudent mode for multi-JVM safe writing
*/
public void setPrudent(boolean prudent);
public boolean isPrudent();
}
/**
* Rolling file appender with configurable rolling policies
*/
public class RollingFileAppender<E> extends FileAppender<E> {
File currentlyActiveFile;
TriggeringPolicy<E> triggeringPolicy;
RollingPolicy rollingPolicy;
/**
* Set the triggering policy for when to roll files
*/
public void setTriggeringPolicy(TriggeringPolicy<E> policy);
public TriggeringPolicy<E> getTriggeringPolicy();
/**
* Set the rolling policy for how to roll files
*/
public void setRollingPolicy(RollingPolicy policy);
public RollingPolicy getRollingPolicy();
}Base classes for stream-based output appenders.
/**
* Base class for appenders that write to output streams
*/
public class OutputStreamAppender<E> extends AppenderBase<E> {
private Encoder<E> encoder;
private OutputStream outputStream;
private boolean immediateFlush = true;
/**
* Set the encoder for formatting events
*/
public void setEncoder(Encoder<E> encoder);
public Encoder<E> getEncoder();
/**
* Configure immediate flushing behavior
*/
public void setImmediateFlush(boolean value);
public boolean isImmediateFlush();
/**
* Set the target output stream
*/
public void setOutputStream(OutputStream outputStream);
protected OutputStream getOutputStream();
// Stream management
protected void writeOut(E event) throws IOException;
protected final void streamWrite(E event, OutputStream out) throws IOException;
protected void closeOutputStream();
}
/**
* Appender that writes to a Writer
*/
public class WriterAppender<E> extends AppenderBase<E> {
protected Layout<E> layout;
protected Writer writer;
private boolean immediateFlush = true;
/**
* Set the layout for formatting events
*/
public void setLayout(Layout<E> layout);
public Layout<E> getLayout();
/**
* Configure immediate flushing
*/
public void setImmediateFlush(boolean value);
public boolean isImmediateFlush();
/**
* Set the target writer
*/
public void setWriter(Writer writer);
protected Writer getWriter();
}/**
* Interface for rolling policies
*/
public interface RollingPolicy extends LifeCycle {
void rollover() throws RolloverFailure;
String getActiveFileName();
CompressionMode getCompressionMode();
void setParent(FileAppender<?> appender);
}
/**
* Interface for triggering policies
*/
public interface TriggeringPolicy<E> extends LifeCycle {
boolean isTriggeringEvent(File activeFile, E event);
}
/**
* Size-based triggering policy
*/
public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {
public static final String DEFAULT_MAX_FILE_SIZE = "10MB";
FileSize maxFileSize = FileSize.valueOf(DEFAULT_MAX_FILE_SIZE);
public void setMaxFileSize(FileSize maxFileSize);
public FileSize getMaxFileSize();
}
/**
* Time-based rolling policy
*/
public class TimeBasedRollingPolicy<E> extends RollingPolicyBase implements TriggeringPolicy<E> {
static final String FNP_NOT_SET = "The FileNamePattern option must be set before using TimeBasedRollingPolicy";
FileNamePattern fileNamePattern;
RenameUtil renameUtil = new RenameUtil();
Compressor compressor;
String fileNamePatternStr;
int maxHistory = UNBOUNDED_HISTORY;
FileSize totalSizeCap = new FileSize(UNBOUNDED_TOTAL_SIZE_CAP);
boolean cleanHistoryOnStart = false;
public void setFileNamePattern(String fnp);
public String getFileNamePattern();
public void setMaxHistory(int maxHistory);
public int getMaxHistory();
public void setTotalSizeCap(FileSize totalSizeCap);
public FileSize getTotalSizeCap();
public void setCleanHistoryOnStart(boolean cleanHistoryOnStart);
public boolean isCleanHistoryOnStart();
}Usage Examples:
import ch.qos.logback.core.rolling.*;
// File appender with time-based rolling
RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>();
fileAppender.setContext(loggerContext);
fileAppender.setName("FILE");
fileAppender.setFile("logs/application.log");
// Time-based rolling policy
TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setFileNamePattern("logs/application.%d{yyyy-MM-dd}.%i.log.gz");
rollingPolicy.setMaxHistory(30);
rollingPolicy.setTotalSizeCap(FileSize.valueOf("1GB"));
rollingPolicy.setParent(fileAppender);
rollingPolicy.start();
// Size-based triggering
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
triggeringPolicy.setMaxFileSize(FileSize.valueOf("100MB"));
triggeringPolicy.start();
fileAppender.setRollingPolicy(rollingPolicy);
fileAppender.setTriggeringPolicy(triggeringPolicy);
// Configure encoder
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();Install with Tessl CLI
npx tessl i tessl/maven-ch-qos-logback--logback-classic