Skip to content
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

Move Recycler to log4j-kit #2400

Merged
merged 3 commits into from
Mar 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @since 2.6
*/
@PerformanceSensitive("allocation")
public final class ReusableMessageFactory implements MessageFactory {
public final class ReusableMessageFactory implements MessageFactory2 {

/**
* Instance of {@link ReusableMessageFactory}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static Throwable determineThrowable(final Object[] args, final int argCo
return null;
}

protected ReusableParameterizedMessage set(final String messagePattern, final Object... arguments) {
public ReusableParameterizedMessage set(final String messagePattern, final Object... arguments) {
init(messagePattern, arguments == null ? 0 : arguments.length, arguments);
varargs = arguments;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* This class implements all {@link MessageFactory} methods.
* </p>
*/
public final class StringFormatterMessageFactory implements MessageFactory {
public final class StringFormatterMessageFactory implements MessageFactory2 {

/**
* Instance of StringFormatterMessageFactory.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* 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.
*/
package org.apache.logging.log4j.spi;
ppkarwasz marked this conversation as resolved.
Show resolved Hide resolved

import java.util.Objects;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.message.MessageFactory2;
import org.apache.logging.log4j.message.SimpleMessage;

/**
* Adapts a legacy {@link MessageFactory} to the new {@link MessageFactory2} interface.
*
* @since 2.24.0
*/
public class MessageFactory2Adapter implements MessageFactory2 {
private final MessageFactory wrapped;

public MessageFactory2Adapter(final MessageFactory wrapped) {
this.wrapped = Objects.requireNonNull(wrapped);
}

public MessageFactory getOriginal() {
return wrapped;
}

@Override
public Message newMessage(final CharSequence charSequence) {
return new SimpleMessage(charSequence);
}

@Override
public Message newMessage(final String message, final Object p0) {
return wrapped.newMessage(message, p0);
}

@Override
public Message newMessage(final String message, final Object p0, final Object p1) {
return wrapped.newMessage(message, p0, p1);
}

@Override
public Message newMessage(final String message, final Object p0, final Object p1, final Object p2) {
return wrapped.newMessage(message, p0, p1, p2);
}

@Override
public Message newMessage(
final String message, final Object p0, final Object p1, final Object p2, final Object p3) {
return wrapped.newMessage(message, p0, p1, p2, p3);
}

@Override
public Message newMessage(
final String message, final Object p0, final Object p1, final Object p2, final Object p3, final Object p4) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4);
}

@Override
public Message newMessage(
final String message,
final Object p0,
final Object p1,
final Object p2,
final Object p3,
final Object p4,
final Object p5) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4, p5);
}

@Override
public Message newMessage(
final String message,
final Object p0,
final Object p1,
final Object p2,
final Object p3,
final Object p4,
final Object p5,
final Object p6) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4, p5, p6);
}

@Override
public Message newMessage(
final String message,
final Object p0,
final Object p1,
final Object p2,
final Object p3,
final Object p4,
final Object p5,
final Object p6,
final Object p7) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7);
}

@Override
public Message newMessage(
final String message,
final Object p0,
final Object p1,
final Object p2,
final Object p3,
final Object p4,
final Object p5,
final Object p6,
final Object p7,
final Object p8) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8);
}

@Override
public Message newMessage(
final String message,
final Object p0,
final Object p1,
final Object p2,
final Object p3,
final Object p4,
final Object p5,
final Object p6,
final Object p7,
final Object p8,
final Object p9) {
return wrapped.newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9);
}

@Override
public Message newMessage(final Object message) {
return wrapped.newMessage(message);
}

@Override
public Message newMessage(final String message) {
return wrapped.newMessage(message);
}

@Override
public Message newMessage(final String message, final Object... params) {
return wrapped.newMessage(message, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.logging.log4j.Logger;

/**
* Handles {@link ServiceLoader} lookups with better error handling.
Expand All @@ -54,6 +55,12 @@ public static <S> Stream<S> safeStream(final ServiceLoader<S> serviceLoader) {
.filter(service -> classes.add(service.getClass()));
}

// Available in Log4j API 2.x
public static <S> Stream<S> safeStream(
final Class<S> ignoredServiceType, final ServiceLoader<S> serviceLoader, final Logger ignoredStatusLogger) {
return safeStream(serviceLoader);
}

private static class ServiceLoaderSpliterator<S> extends Spliterators.AbstractSpliterator<S> {
private final Iterator<S> serviceIterator;
private final String serviceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
import org.apache.logging.log4j.core.time.Clock;
import org.apache.logging.log4j.core.time.NanoClock;
import org.apache.logging.log4j.kit.logger.AbstractLogger;
import org.apache.logging.log4j.kit.recycler.Recycler;
import org.apache.logging.log4j.kit.recycler.RecyclerFactory;
import org.apache.logging.log4j.message.FlowMessageFactory;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.plugins.Inject;
import org.apache.logging.log4j.plugins.Named;
import org.apache.logging.log4j.spi.recycler.Recycler;
import org.apache.logging.log4j.spi.recycler.RecyclerFactory;
import org.apache.logging.log4j.util.StringMap;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.message.StringFormatterMessageFactory;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.apache.logging.log4j.spi.LoggingSystem;
import org.awaitility.Awaitility;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Tag;
Expand All @@ -65,7 +64,8 @@ public class LoggerTest {

private static void checkMessageFactory(final MessageFactory messageFactory, final Logger testLogger) {
if (messageFactory == null) {
assertSame(LoggingSystem.getMessageFactory(), testLogger.getMessageFactory());
final org.apache.logging.log4j.Logger newLogger = LogManager.getLogger("checkMessageFactory");
assertSame(newLogger.getMessageFactory(), testLogger.getMessageFactory());
} else {
final MessageFactory actual = testLogger.getMessageFactory();
assertEquals(messageFactory, actual);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import org.apache.logging.log4j.core.config.ReliabilityStrategy;
import org.apache.logging.log4j.core.filter.CompositeFilter;
import org.apache.logging.log4j.kit.logger.AbstractLogger;
import org.apache.logging.log4j.kit.recycler.RecyclerFactory;
import org.apache.logging.log4j.message.FlowMessageFactory;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.plugins.Inject;
import org.apache.logging.log4j.plugins.Named;
import org.apache.logging.log4j.spi.recycler.RecyclerFactory;
import org.apache.logging.log4j.util.Strings;
import org.apache.logging.log4j.util.Supplier;
import org.jspecify.annotations.NullMarked;
Expand Down
Loading