Skip to content

Commit

Permalink
Backport PR #14727 to 8.5: Avoid the wrapping of LogstashMessageFacto…
Browse files Browse the repository at this point in the history
…ry with log4j's MessageFactory adapter #14741

Starting with Log4j2  2.6 if a subclass of MessageFactory associated with an Logger instance
is not subclass of MessageFactory2, then it's wrapped with MessageFactory2Adapter.

This trigger a log4j warn log that, when a class subclasses LogStash::Plugin for example, is noisy and report about
a Logger is not associated with the default MessagedFactory (LogstashMessageFactory) every time a subclass of Plugins is instantiated.

This commit adapt LogstashMessageFactory to implement the MessagedFactory2 instead of the older MessageFactory to avoid the wrapping with the adapter class.

(cherry picked from commit 05bfaff)

Co-authored-by: Andrea Selva <[email protected]>
  • Loading branch information
github-actions[bot] and andsel authored Nov 9, 2022
1 parent 41c8478 commit 8b920e6
Showing 1 changed file with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package org.logstash.log;

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.ObjectMessage;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.message.SimpleMessage;
Expand All @@ -30,8 +30,10 @@

/**
* Used in Log4j configuration.
*
* Requires Log4j 2.6 and above.
* */
public final class LogstashMessageFactory implements MessageFactory {
public final class LogstashMessageFactory implements MessageFactory2 {

public static final LogstashMessageFactory INSTANCE = new LogstashMessageFactory();

Expand All @@ -53,4 +55,59 @@ public Message newMessage(String message, Object... params) {
return new ParameterizedMessage(message, params);
}
}

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

@Override
public Message newMessage(String message, Object p0) {
return newMessage(message, new Object[]{p0});
}

@Override
public Message newMessage(String message, Object p0, Object p1) {
return newMessage(message, new Object[]{p0, p1});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2) {
return newMessage(message, new Object[]{p0, p1, p2});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3) {
return newMessage(message, new Object[]{p0, p1, p2, p3});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4) {
return newMessage(message, new Object[]{p0, p1, p2, p3, p4});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5) {
return newMessage(message, new Object[]{p0, p1, p2, p3, p4, p5});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) {
return newMessage(message, new Object[]{p0, p1, p2, p3, p4, p5, p6});
}

@Override
public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) {
return newMessage(message, new Object[]{p0, p1, p2, p3, p4, p5, p6, p7});
}

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

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

0 comments on commit 8b920e6

Please sign in to comment.