Skip to content

Move async support to log4j-async-logger #2241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 29, 2024
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
86 changes: 86 additions & 0 deletions log4j-async-logger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>${revision}</version>
<relativePath>../log4j-parent</relativePath>
</parent>

<artifactId>log4j-async-logger</artifactId>
<name>Apache Log4j Async Logger</name>
<description>Alternative implementation of logger that uses LMAX Disruptor.</description>

<dependencies>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-plugin-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import com.lmax.disruptor.ExceptionHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.util.List;
import org.apache.logging.log4j.Level;
Expand All @@ -24,6 +24,8 @@
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.ReusableLogEvent;
import org.apache.logging.log4j.core.async.AsyncQueueFullMessageUtil;
import org.apache.logging.log4j.core.async.EventRoute;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.Property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.async.AsyncQueueFullMessageUtil;
import org.apache.logging.log4j.core.async.EventRoute;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
Expand Down Expand Up @@ -79,6 +82,7 @@ public LoggerConfig build() {
final String name = getLoggerName().equals(ROOT) ? Strings.EMPTY : getLoggerName();
final LevelAndRefs container =
LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(), getConfig());
final String includeLocationConfigValue = getIncludeLocation();
return new AsyncLoggerConfig(
name,
container.refs,
Expand All @@ -87,7 +91,7 @@ public LoggerConfig build() {
isAdditivity(),
getProperties(),
getConfig(),
includeLocation(getIncludeLocation()),
Boolean.parseBoolean(includeLocationConfigValue),
getLogEventFactory());
}
}
Expand Down Expand Up @@ -116,9 +120,9 @@ public void initialize() {
super.initialize();
}

protected void log(final LogEvent event, final LoggerConfigPredicate predicate) {
protected void log(final LogEvent event, final Predicate<LoggerConfig> predicate) {
// See LOG4J2-2301
if (predicate == LoggerConfigPredicate.ALL
if (predicate == null
&& ASYNC_LOGGER_ENTERED.get() == Boolean.FALSE
&&
// Optimization: AsyncLoggerConfig is identical to LoggerConfig
Expand All @@ -132,7 +136,7 @@ protected void log(final LogEvent event, final LoggerConfigPredicate predicate)
if (!isFiltered(event)) {
// Detect the first time we encounter an AsyncLoggerConfig. We must log
// to all non-async loggers first.
processLogEvent(event, LoggerConfigPredicate.SYNCHRONOUS_ONLY);
processLogEvent(event, lc -> !(lc instanceof AsyncLoggerConfig));
// Then pass the event to the background thread where
// all async logging is executed. It is important this
// happens at most once and after all synchronous loggers
Expand Down Expand Up @@ -175,7 +179,17 @@ private void handleQueueFull(final LogEvent event) {
} else {
// otherwise, we leave it to the user preference
final EventRoute eventRoute = delegate.getEventRoute(event.getLevel());
eventRoute.logMessage(this, event);
switch (eventRoute) {
case DISCARD:
break;
case ENQUEUE:
logInBackgroundThread(event);
break;
case SYNCHRONOUS:
logToAsyncLoggerConfigsOnCurrentThread(event);
break;
default:
}
}
}

Expand All @@ -196,7 +210,7 @@ void logInBackgroundThread(final LogEvent event) {
*/
void logToAsyncLoggerConfigsOnCurrentThread(final LogEvent event) {
// skip the filter, which was already called on the logging thread
processLogEvent(event, LoggerConfigPredicate.ASYNCHRONOUS_ONLY);
processLogEvent(event, lc -> lc instanceof AsyncLoggerConfig);
}

private String displayName() {
Expand All @@ -218,11 +232,6 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
return true;
}

// Note: for asynchronous loggers, includeLocation default is FALSE
protected static boolean includeLocation(final String includeLocationConfigValue) {
return Boolean.parseBoolean(includeLocationConfigValue);
}

/**
* An asynchronous root Logger.
*/
Expand All @@ -241,6 +250,7 @@ public static class Builder<B extends Builder<B>> extends RootLogger.Builder<B>
public LoggerConfig build() {
final LevelAndRefs container =
LoggerConfig.getLevelAndRefs(getLevel(), getRefs(), getLevelAndRefs(), getConfig());
final String includeLocationConfigValue = getIncludeLocation();
return new AsyncLoggerConfig(
LogManager.ROOT_LOGGER_NAME,
container.refs,
Expand All @@ -249,7 +259,7 @@ public LoggerConfig build() {
isAdditivity(),
getProperties(),
getConfig(),
AsyncLoggerConfig.includeLocation(getIncludeLocation()),
Boolean.parseBoolean(includeLocationConfigValue),
getLogEventFactory());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

/**
* Default disruptor exception handler for errors that occur in the AsyncLogger background thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.async.EventRoute;
import org.apache.logging.log4j.core.impl.LogEventFactory;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
Expand All @@ -34,6 +34,11 @@
import org.apache.logging.log4j.core.AbstractLifeCycle;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.ReusableLogEvent;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicyFactory;
import org.apache.logging.log4j.core.async.DiscardingAsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.EventRoute;
import org.apache.logging.log4j.core.async.InternalAsyncUtil;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.impl.LogEventFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.net.URI;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -130,6 +130,11 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
return true;
}

@Override
public boolean includeLocation() {
return false;
}

// package-protected for tests
AsyncLoggerDisruptor getAsyncLoggerDisruptor() {
return loggerDisruptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.net.URI;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
import org.apache.logging.log4j.plugins.Inject;
import org.apache.logging.log4j.plugins.Singleton;
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
import org.apache.logging.log4j.util.PropertiesUtil;

/**
* {@code ContextSelector} that manages {@code AsyncLoggerContext} instances.
Expand All @@ -33,19 +31,6 @@
@Singleton
public class AsyncLoggerContextSelector extends ClassLoaderContextSelector {

/**
* Returns {@code true} if the user specified this selector as the Log4jContextSelector, to make all loggers
* asynchronous.
*
* @return {@code true} if all loggers are asynchronous, {@code false} otherwise.
*/
public static boolean isSelected() {
// FIXME(ms): this should check Injector bindings
return AsyncLoggerContextSelector.class
.getName()
.equals(PropertiesUtil.getProperties().getStringProperty(Log4jPropertyKey.CONTEXT_SELECTOR_CLASS_NAME));
}

@Inject
public AsyncLoggerContextSelector(final ConfigurableInstanceFactory instanceFactory) {
super(instanceFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

/**
* Default disruptor exception handler for errors that occur in the AsyncLogger background thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.RingBuffer;
Expand All @@ -30,6 +30,10 @@
import java.util.function.Supplier;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.AbstractLifeCycle;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicyFactory;
import org.apache.logging.log4j.core.async.DiscardingAsyncQueueFullPolicy;
import org.apache.logging.log4j.core.async.EventRoute;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.util.Log4jThread;
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import com.lmax.disruptor.WaitStrategy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.net.URI;
import org.apache.logging.log4j.core.LoggerContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.BusySpinWaitStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.core.async;
package org.apache.logging.log4j.async.logger;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down
Loading