Core infrastructure and basic components for the Logback logging framework, providing appenders, encoders, layouts, filters, and event processing pipeline
—
XML-based configuration processing through the Joran framework, supporting property substitution, conditional configuration, and extensible action handlers. The configuration system enables declarative setup of logging components with advanced features like variable substitution and conditional logic.
Base class for XML-based configuration with multiple input source support.
/**
* Base XML configuration processor supporting multiple input sources.
*/
public abstract class GenericXMLConfigurator extends ContextAwareBase {
/**
* Configure from URL.
* @param url configuration file URL
* @throws JoranException if configuration fails
*/
public void doConfigure(URL url) throws JoranException;
/**
* Configure from file.
* @param file configuration file
* @throws JoranException if configuration fails
*/
public void doConfigure(File file) throws JoranException;
/**
* Configure from input stream.
* @param inputStream configuration input stream
* @throws JoranException if configuration fails
*/
public void doConfigure(InputStream inputStream) throws JoranException;
/**
* Configure from input source.
* @param inputSource SAX input source
* @throws JoranException if configuration fails
*/
public void doConfigure(InputSource inputSource) throws JoranException;
}Joran-based XML configurator with SAX event recording and playback.
/**
* Joran-based XML configurator with advanced processing capabilities.
*/
public class JoranConfigurator extends GenericXMLConfigurator {
/**
* Configure context from URL.
* @param context target context
* @param url configuration URL
* @throws JoranException if configuration fails
*/
public void configure(Context context, URL url) throws JoranException;
/**
* Record SAX events from input stream for later processing.
* @param inputStream configuration input stream
* @return list of recorded SAX events
* @throws JoranException if recording fails
*/
public List<SaxEvent> recordEvents(InputStream inputStream) throws JoranException;
}Central configuration and service container for all logback components.
/**
* Central context providing shared services and configuration.
*/
public interface Context extends PropertyContainer {
/**
* Get the status manager for diagnostic messages.
* @return status manager
*/
StatusManager getStatusManager();
/**
* Store an object by key.
* @param key object key
* @param value object to store
*/
void putObject(String key, Object value);
/**
* Retrieve stored object by key.
* @param key object key
* @return stored object or null
*/
Object getObject(String key);
/**
* Get context name.
* @return context name
*/
String getName();
/**
* Set context name.
* @param name context name
*/
void setName(String name);
/**
* Get context birth time.
* @return creation timestamp
*/
long getBirthTime();
/**
* Get configuration lock for thread safety.
* @return reentrant lock
*/
ReentrantLock getConfigurationLock();
/**
* Get scheduled executor service.
* @return scheduled executor
*/
ScheduledExecutorService getScheduledExecutorService();
/**
* Get general executor service.
* @return executor service
*/
ExecutorService getExecutorService();
/**
* Get alternate executor service.
* @return alternate executor
*/
ExecutorService getAlternateExecutorService();
/**
* Register a lifecycle component for management.
* @param component component to register
*/
void register(LifeCycle component);
/**
* Add a scheduled future for tracking.
* @param scheduledFuture future to track
*/
void addScheduledFuture(ScheduledFuture<?> scheduledFuture);
/**
* Get sequence number generator.
* @return sequence generator
*/
SequenceNumberGenerator getSequenceNumberGenerator();
}Base implementation of Context interface with complete service management.
/**
* Base context implementation with service management and lifecycle support.
*/
public class ContextBase implements Context {
/**
* Complete implementation of Context interface.
* Provides property storage, object registry, executor services, and lifecycle management.
*/
@Override
public StatusManager getStatusManager();
@Override
public void putObject(String key, Object value);
@Override
public Object getObject(String key);
@Override
public String getProperty(String key);
@Override
public void putProperty(String key, String value);
@Override
public Map<String, String> getCopyOfPropertyMap();
// ... additional Context methods
}Interface for property storage and retrieval with variable substitution support.
/**
* Interface for property storage and retrieval.
*/
public interface PropertyContainer {
/**
* Get property value with variable substitution.
* @param key property key
* @return property value with variables substituted
*/
String getProperty(String key);
/**
* Set property value.
* @param key property key
* @param value property value
*/
void putProperty(String key, String value);
/**
* Get copy of all properties.
* @return map of all properties
*/
Map<String, String> getCopyOfPropertyMap();
}Interface for dynamic property definition and computation.
/**
* Interface for defining properties dynamically.
*/
public interface PropertyDefiner extends ContextAware {
/**
* Get the computed property value.
* @return property value
*/
String getPropertyValue();
}Property definer that checks for resource existence.
/**
* Property definer that returns "true" if a resource exists.
*/
public class ResourceExistsPropertyDefiner extends PropertyDefinerBase {
/**
* Set the resource path to check.
* @param resource resource path (classpath or file)
*/
public void setResource(String resource);
/**
* Get the resource path.
* @return resource path
*/
public String getResource();
/**
* Returns "true" if resource exists, "false" otherwise.
* @return "true" or "false"
*/
@Override
public String getPropertyValue();
}Property definer that checks for file existence.
/**
* Property definer that returns "true" if a file exists.
*/
public class FileExistsPropertyDefiner extends PropertyDefinerBase {
/**
* Set the file path to check.
* @param path file path
*/
public void setPath(String path);
/**
* Get the file path.
* @return file path
*/
public String getPath();
/**
* Returns "true" if file exists, "false" otherwise.
* @return "true" or "false"
*/
@Override
public String getPropertyValue();
}Property definer that provides the canonical hostname.
/**
* Property definer that returns the canonical hostname.
*/
public class CanonicalHostNamePropertyDefiner extends PropertyDefinerBase {
/**
* Returns the canonical hostname of the local machine.
* @return canonical hostname
*/
@Override
public String getPropertyValue();
}Base class for configuration actions that process XML elements.
/**
* Base class for configuration actions that process XML elements.
*/
public abstract class Action implements ContextAware {
/**
* Called when element starts.
* @param interpreter SAX event interpreter
* @param name element name
* @param attributes element attributes
* @throws ActionException if action fails
*/
public abstract void begin(SaxEventInterpreter interpreter,
String name, Attributes attributes) throws ActionException;
/**
* Called when element ends.
* @param interpreter SAX event interpreter
* @param name element name
* @throws ActionException if action fails
*/
public abstract void end(SaxEventInterpreter interpreter,
String name) throws ActionException;
/**
* Called with element body text.
* @param interpreter SAX event interpreter
* @param body element body text
* @throws ActionException if action fails
*/
public void body(SaxEventInterpreter interpreter, String body) throws ActionException;
}Action for setting context properties from XML.
/**
* Action for processing <property> elements in configuration.
*/
public class PropertyAction extends Action {
@Override
public void begin(SaxEventInterpreter interpreter, String name, Attributes attributes);
@Override
public void end(SaxEventInterpreter interpreter, String name);
}Action for defining properties using PropertyDefiner implementations.
/**
* Action for processing <define> elements that create PropertyDefiners.
*/
public class DefinePropertyAction extends Action {
@Override
public void begin(SaxEventInterpreter interpreter, String name, Attributes attributes);
@Override
public void end(SaxEventInterpreter interpreter, String name);
}Support for conditional configuration using <if>, <then>, and <else> elements.
/**
* Base class for boolean conditions in configuration.
*/
public abstract class Condition implements ContextAware {
/**
* Evaluate the condition.
* @return true if condition is met
*/
public abstract boolean evaluate();
}
/**
* Property wrapper for script evaluation in conditional configuration.
*/
public class PropertyWrapperForScripts {
/**
* Get property value.
* @param key property key
* @return property value
*/
public String property(String key);
/**
* Short form for property access.
* @param key property key
* @return property value
*/
public String p(String key);
/**
* Check if property is null.
* @param key property key
* @return true if null or undefined
*/
public boolean isNull(String key);
/**
* Check if property is defined.
* @param key property key
* @return true if defined
*/
public boolean isDefined(String key);
}Object representation of configuration elements for processing.
/**
* Base class for configuration model elements.
*/
public class Model {
/**
* Add a sub-model as a child.
* @param model child model
*/
public void addSubModel(Model model);
/**
* Get all sub-models.
* @return list of child models
*/
public List<Model> getSubModels();
/**
* Set the XML tag name.
* @param tag tag name
*/
public void setTag(String tag);
/**
* Get the XML tag name.
* @return tag name
*/
public String getTag();
/**
* Set the body text content.
* @param bodyText body text
*/
public void setBodyText(String bodyText);
/**
* Get the body text content.
* @return body text
*/
public String getBodyText();
}
/**
* Model for component elements with class names.
*/
public class ComponentModel extends Model {
/**
* Set the component class name.
* @param className fully qualified class name
*/
public void setClassName(String className);
/**
* Get the component class name.
* @return class name
*/
public String getClassName();
}System for resolving variables in configuration values.
/**
* Utility for variable substitution in configuration values.
*/
public class OptionHelper {
/**
* Substitute variables in a string using property container.
* Variables are in ${variable} format.
* @param val string with variables
* @param pc property container for variable lookup
* @return string with variables substituted
*/
public static String substVars(String val, PropertyContainer pc);
/**
* Instantiate class by name with context.
* @param className class name to instantiate
* @param superClass expected superclass
* @param context context for dependency injection
* @return instantiated object
*/
public static Object instantiateByClassName(String className,
Class<?> superClass,
Context context);
}<configuration>
<!-- Set properties -->
<property name="LOG_DIR" value="/var/log/myapp" />
<property name="LOG_LEVEL" value="INFO" />
<!-- Define dynamic properties -->
<define name="HOSTNAME" class="ch.qos.logback.core.property.CanonicalHostNamePropertyDefiner" />
<!-- Configure appender -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_DIR}/application-${HOSTNAME}.log</file>
<append>true</append>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Configure rolling appender -->
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/app.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
</configuration><configuration>
<property name="ENV" value="${ENV:-development}" />
<!-- Development configuration -->
<if condition='property("ENV").equals("development")'>
<then>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</then>
</if>
<!-- Production configuration -->
<if condition='property("ENV").equals("production")'>
<then>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/app/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/app/application.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
</then>
</if>
</configuration>import ch.qos.logback.core.joran.JoranConfigurator;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
// Create context
Context context = new ContextBase();
// Set properties programmatically
context.putProperty("LOG_DIR", "/tmp/logs");
context.putProperty("APP_NAME", "MyApplication");
// Configure from file
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
try {
// Load configuration from classpath
URL configUrl = getClass().getResource("/logback-config.xml");
configurator.doConfigure(configUrl);
} catch (JoranException e) {
// Handle configuration error
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}import ch.qos.logback.core.spi.PropertyDefiner;
import ch.qos.logback.core.spi.ContextAwareBase;
public class EnvironmentPropertyDefiner extends ContextAwareBase implements PropertyDefiner {
private String variableName;
private String defaultValue;
@Override
public String getPropertyValue() {
String value = System.getenv(variableName);
if (value == null) {
value = System.getProperty(variableName, defaultValue);
}
return value != null ? value : defaultValue;
}
public void setVariableName(String variableName) {
this.variableName = variableName;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
}Usage in XML:
<configuration>
<define name="PORT" class="com.example.EnvironmentPropertyDefiner">
<variableName>SERVER_PORT</variableName>
<defaultValue>8080</defaultValue>
</define>
<property name="LOG_FILE" value="/var/log/app-${PORT}.log" />
</configuration>${variable} syntax for property substitution${variable:-default}<property> elements-Dprop=value)<if condition="..."> elements<include> elements for modular configurationInstall with Tessl CLI
npx tessl i tessl/maven-ch-qos-logback--logback-core