Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.log4j.spi.RendererSupport;
import org.apache.log4j.spi.ThrowableRenderer;
import org.apache.log4j.spi.ThrowableRendererSupport;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.net.UrlConnectionFactory;
import org.apache.logging.log4j.util.StackLocatorUtil;
Expand Down Expand Up @@ -307,7 +308,7 @@ public void doConfigure(final Properties properties, final LoggerRepository logg
Configuration doConfigure(
final Properties properties, final LoggerRepository loggerRepository, final ClassLoader classLoader) {
final PropertiesConfiguration configuration =
new PropertiesConfiguration(LogManager.getContext(classLoader), properties);
new PropertiesConfiguration((LoggerContext) LogManager.getContext(classLoader), properties);
configuration.doConfigure();

repository = loggerRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package org.apache.log4j.config;

import java.util.Optional;
import org.apache.log4j.Level;
import org.apache.log4j.builders.BuilderManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AbstractConfiguration;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
import org.apache.logging.log4j.plugins.di.DI;
import org.apache.logging.log4j.util.PropertiesUtil;

/**
* Base Configuration for Log4j 1.
Expand All @@ -49,7 +53,15 @@ public Log4j1Configuration(
final LoggerContext loggerContext,
final ConfigurationSource configurationSource,
final int monitorIntervalSeconds) {
super(loggerContext, configurationSource);
super(
loggerContext,
configurationSource,
Optional.ofNullable(loggerContext)
.map(LoggerContext::getEnvironment)
.orElseGet(PropertiesUtil::getProperties),
Optional.ofNullable(loggerContext)
.map(ctx -> (ConfigurableInstanceFactory) ctx.getInstanceFactory())
.orElseGet(DI::createInitializedFactory));
initializeWatchers(this, configurationSource, monitorIntervalSeconds);
manager = instanceFactory.getInstance(BuilderManager.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ public class PropertiesConfiguration extends Log4j1Configuration {
* Constructs a new instance.
*
* @param loggerContext The LoggerContext.
* @param source The ConfigurationSource.
* @param configurationSource The ConfigurationSource.
* @param monitorIntervalSeconds The monitoring interval in seconds.
*/
public PropertiesConfiguration(
final LoggerContext loggerContext, final ConfigurationSource source, final int monitorIntervalSeconds) {
super(loggerContext, source, monitorIntervalSeconds);
final LoggerContext loggerContext,
final ConfigurationSource configurationSource,
final int monitorIntervalSeconds) {
this(loggerContext, configurationSource, monitorIntervalSeconds, null);
}

/**
Expand All @@ -96,18 +98,16 @@ public PropertiesConfiguration(
* @param properties The ConfigurationSource, may be null.
*/
public PropertiesConfiguration(final LoggerContext loggerContext, final Properties properties) {
super(loggerContext, ConfigurationSource.NULL_SOURCE, 0);
this.properties = properties;
this(loggerContext, ConfigurationSource.NULL_SOURCE, 0, properties);
}

/**
* Constructs a new instance.
*
* @param loggerContext The LoggerContext.
* @param properties The ConfigurationSource.
*/
public PropertiesConfiguration(org.apache.logging.log4j.spi.LoggerContext loggerContext, Properties properties) {
this((LoggerContext) loggerContext, properties);
private PropertiesConfiguration(
final LoggerContext loggerContext,
final ConfigurationSource configurationSource,
final int monitorIntervalSeconds,
final Properties properties) {
super(loggerContext, configurationSource, monitorIntervalSeconds);
this.properties = properties;
}

@Override
Expand Down Expand Up @@ -149,7 +149,7 @@ public Configuration reconfigure() {

/**
* Reads a configuration from a file. <b>The existing configuration is not cleared nor reset.</b> If you require a
* different behavior, then call {@link LogManager#resetConfiguration resetConfiguration} method before calling
* different behavior, then call {@link LogManager#resetConfiguration()} resetConfiguration} method before calling
* <code>doConfigure</code>.
* <p>
* The configuration file consists of statements in the format <code>key=value</code>. The syntax of different
Expand Down Expand Up @@ -373,7 +373,7 @@ private void parseLoggers(final Properties props) {
LoggerConfig loggerConfig = getLogger(loggerName);
if (loggerConfig == null) {
final boolean additivity = getAdditivityForLogger(props, loggerName);
loggerConfig = new LoggerConfig(loggerName, org.apache.logging.log4j.Level.ERROR, additivity);
loggerConfig = new LoggerConfig(loggerName, Level.ERROR, additivity, this);
addLogger(loggerName, loggerConfig);
}
parseLogger(props, loggerConfig, key, loggerName, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ public Configuration getConfiguration(final LoggerContext loggerContext, final C
}

@Override
protected String getTestPrefix() {
public String getTestPrefix() {
return TEST_PREFIX;
}

@Override
protected String getDefaultPrefix() {
public String getDefaultPrefix() {
return DEFAULT_PREFIX;
}

@Override
protected String getVersion() {
public String getVersion() {
return LOG4J1_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ public class XmlConfiguration extends Log4j1Configuration {
private final Properties props = null;

public XmlConfiguration(
final LoggerContext loggerContext, final ConfigurationSource source, final int monitorIntervalSeconds) {
super(loggerContext, source, monitorIntervalSeconds);
final LoggerContext loggerContext,
final ConfigurationSource configurationSource,
final int monitorIntervalSeconds) {
super(loggerContext, configurationSource, monitorIntervalSeconds);
appenderMap = new HashMap<>();
}

Expand Down Expand Up @@ -563,7 +565,7 @@ private void parseCategory(final Element loggerElement) {
final boolean additivity = OptionConverter.toBoolean(subst(loggerElement.getAttribute(ADDITIVITY_ATTR)), true);
LoggerConfig loggerConfig = getLogger(catName);
if (loggerConfig == null) {
loggerConfig = new LoggerConfig(catName, org.apache.logging.log4j.Level.ERROR, additivity);
loggerConfig = new LoggerConfig(catName, org.apache.logging.log4j.Level.ERROR, additivity, this);
addLogger(catName, loggerConfig);
} else {
loggerConfig.setAdditive(additivity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ public Configuration getConfiguration(final LoggerContext loggerContext, final C
}

@Override
protected String getTestPrefix() {
public String getTestPrefix() {
return TEST_PREFIX;
}

@Override
protected String getDefaultPrefix() {
public String getDefaultPrefix() {
return DEFAULT_PREFIX;
}

@Override
protected String getVersion() {
public String getVersion() {
return LOG4J1_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class BasicConfigurationFactory extends ConfigurationFactory {

@Override
public String[] getSupportedTypes() {
protected String[] getSupportedTypes() {
return new String[] {"*"};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ public void handleException(final ExtensionContext context, final Throwable thro
logger.atLevel(data.getLevel())
.withThrowable(data.getThrowable())
.withLocation(data.getStackTraceElement())
.log("{} {}", formatter.format(Instant.ofEpochMilli(data.getTimestamp())), data.getMessage());
.log(
"{} {}",
formatter.format(Instant.ofEpochMilli(data.getTimestamp())),
data.getMessage().getFormattedMessage());
});
}
}
Expand Down
29 changes: 27 additions & 2 deletions log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import org.apache.logging.log4j.spi.LoggerContext;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.apache.logging.log4j.spi.LoggingSystem;
import org.apache.logging.log4j.spi.LoggingSystemProperty;
import org.apache.logging.log4j.spi.Terminable;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.LoaderUtil;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.StackLocatorUtil;
import org.apache.logging.log4j.util.Strings;

Expand Down Expand Up @@ -55,6 +58,28 @@ public class LogManager {
// for convenience
private static final String FQCN = LogManager.class.getName();

private static volatile LoggerContextFactory factory;

/*
* Scans the classpath to find all logging implementation. Currently, only one will be used but this could be
* extended to allow multiple implementations to be used.
*/
static {
// Shortcut binding to force a specific logging implementation.
final PropertiesUtil managerProps = PropertiesUtil.getProperties();
final String factoryClassName =
managerProps.getStringProperty(LoggingSystemProperty.LOGGER_CONTEXT_FACTORY_CLASS);
if (factoryClassName != null) {
try {
factory = LoaderUtil.newCheckedInstanceOf(factoryClassName, LoggerContextFactory.class);
} catch (final ClassNotFoundException cnfe) {
LOGGER.error("Unable to locate configured LoggerContextFactory {}", factoryClassName);
} catch (final Exception ex) {
LOGGER.error("Unable to create configured LoggerContextFactory {}", factoryClassName, ex);
}
}
}

/**
* Prevents instantiation
*/
Expand Down Expand Up @@ -387,7 +412,7 @@ public static void shutdown(final LoggerContext context) {
* @return The LoggerContextFactory.
*/
public static LoggerContextFactory getFactory() {
return LoggingSystem.getLoggerContextFactory();
return factory != null ? factory : LoggingSystem.getProvider().getLoggerContextFactory();
}

/**
Expand All @@ -405,7 +430,7 @@ public static LoggerContextFactory getFactory() {
* @see LoggingSystem
*/
public static void setFactory(final LoggerContextFactory factory) {
LoggingSystem.getInstance().setLoggerContextFactory(factory);
LogManager.factory = factory;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* </p>
* @see <a href="https://logging.apache.org/log4j/2.x/manual/thread-context.html">Thread Context Manual</a>
*/
public final class ThreadContext {
public class ThreadContext {

/**
* An empty read-only ThreadContextStack.
Expand Down Expand Up @@ -165,17 +165,15 @@ public ContextStack getImmutableStackOrNull() {
init();
}

private ThreadContext() {
// empty
}
protected ThreadContext() {}

/**
* <em>Consider private, used for testing.</em>
*/
@InternalApi
public static void init() {
contextMap = LoggingSystem.createContextMap();
contextStack = LoggingSystem.createContextStack();
contextMap = LoggingSystem.getProvider().getThreadContextMapFactory();
contextStack = LoggingSystem.getProvider().getThreadContextStack();
if (contextMap instanceof ReadOnlyThreadContextMap) {
readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
} else {
Expand Down Expand Up @@ -496,6 +494,13 @@ public static void trim(final int depth) {
contextStack.trim(depth);
}

/**
* @return The underlying context map implementation.
*/
protected static ThreadContextMap getContextMap() {
return contextMap;
}

/**
* The ThreadContext Stack interface.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ default String getName() {
* @return the PropertyEnvironment.
* @since 3.0
*/
default PropertyEnvironment getProperties() {
default PropertyEnvironment getEnvironment() {
return PropertiesUtil.getContextProperties(LoggerContext.class.getClassLoader(), getName());
}

Expand Down
Loading