Skip to content

Commit c4882c0

Browse files
authored
Initial draft of the Recycler API (#1401)
1 parent 168369e commit c4882c0

File tree

118 files changed

+2505
-2061
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+2505
-2061
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/builders/appender/SyslogAppenderBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ private Appender createAppender(final String name, final Log4j1Configuration con
142142
resolveSyslogHost(syslogHost, host, port);
143143
final org.apache.logging.log4j.core.Layout messageLayout = LayoutAdapter.adapt(layout);
144144
final Log4j1SyslogLayout appenderLayout = Log4j1SyslogLayout.newBuilder()
145+
.setConfiguration(configuration)
145146
.setHeader(header)
146147
.setFacility(Facility.toFacility(facility))
147148
.setFacilityPrinting(facilityPrinting)

log4j-1.2-api/src/main/java/org/apache/log4j/builders/layout/HtmlLayoutBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.log4j.builders.AbstractBuilder;
2626
import org.apache.log4j.config.PropertiesConfiguration;
2727
import org.apache.log4j.xml.XmlConfiguration;
28+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
2829
import org.apache.logging.log4j.core.layout.HtmlLayout;
2930
import org.apache.logging.log4j.plugins.Namespace;
3031
import org.apache.logging.log4j.plugins.Plugin;
@@ -78,6 +79,7 @@ public Layout parse(final PropertiesConfiguration config) {
7879

7980
private Layout createLayout(final String title, final boolean locationInfo) {
8081
return LayoutWrapper.adapt(HtmlLayout.newBuilder()
82+
.setConfiguration(new DefaultConfiguration())
8183
.setTitle(title)
8284
.setLocationInfo(locationInfo)
8385
.build());

log4j-1.2-api/src/main/java/org/apache/log4j/builders/layout/XmlLayoutBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.log4j.config.PropertiesConfiguration;
2626
import org.apache.log4j.layout.Log4j1XmlLayout;
2727
import org.apache.log4j.xml.XmlConfiguration;
28+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
2829
import org.apache.logging.log4j.plugins.Namespace;
2930
import org.apache.logging.log4j.plugins.Plugin;
3031
import org.w3c.dom.Element;
@@ -73,6 +74,6 @@ public Layout parse(final PropertiesConfiguration config) {
7374
}
7475

7576
private Layout createLayout(final boolean properties, final boolean locationInfo) {
76-
return LayoutWrapper.adapt(Log4j1XmlLayout.createLayout(locationInfo, properties));
77+
return LayoutWrapper.adapt(Log4j1XmlLayout.createLayout(new DefaultConfiguration(), locationInfo, properties));
7778
}
7879
}

log4j-1.2-api/src/main/java/org/apache/log4j/layout/Log4j1SyslogLayout.java

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.logging.log4j.core.Layout;
2525
import org.apache.logging.log4j.core.LogEvent;
2626
import org.apache.logging.log4j.core.StringLayout;
27+
import org.apache.logging.log4j.core.config.Configuration;
2728
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
2829
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
2930
import org.apache.logging.log4j.core.net.Facility;
@@ -85,7 +86,7 @@ public Log4j1SyslogLayout build() {
8586
LOGGER.error("Log4j1SyslogLayout: the message layout must be a StringLayout.");
8687
return null;
8788
}
88-
return new Log4j1SyslogLayout(facility, facilityPrinting, header, (StringLayout) messageLayout, getCharset());
89+
return new Log4j1SyslogLayout(getConfiguration(), facility, facilityPrinting, header, (StringLayout) messageLayout, getCharset());
8990
}
9091

9192
public Facility getFacility() {
@@ -147,9 +148,14 @@ public static <B extends Builder<B>> B newBuilder() {
147148
private final LogEventPatternConverter dateConverter = DatePatternConverter.newInstance(dateFormatOptions);
148149

149150

150-
private Log4j1SyslogLayout(final Facility facility, final boolean facilityPrinting, final boolean header,
151-
final StringLayout messageLayout, final Charset charset) {
152-
super(charset);
151+
private Log4j1SyslogLayout(
152+
final Configuration config,
153+
final Facility facility,
154+
final boolean facilityPrinting,
155+
final boolean header,
156+
final StringLayout messageLayout,
157+
final Charset charset) {
158+
super(config, charset);
153159
this.facility = facility;
154160
this.facilityPrinting = facilityPrinting;
155161
this.header = header;
@@ -168,32 +174,36 @@ public String toSerializable(final LogEvent event) {
168174
// so we generate the message first
169175
final String message = messageLayout != null ? messageLayout.toSerializable(event)
170176
: event.getMessage().getFormattedMessage();
171-
final StringBuilder buf = getStringBuilder();
172-
173-
buf.append('<');
174-
buf.append(Priority.getPriority(facility, event.getLevel()));
175-
buf.append('>');
176-
177-
if (header) {
178-
final int index = buf.length() + 4;
179-
dateConverter.format(event, buf);
180-
// RFC 3164 says leading space, not leading zero on days 1-9
181-
if (buf.charAt(index) == '0') {
182-
buf.setCharAt(index, Chars.SPACE);
177+
final StringBuilder buf = stringBuilderRecycler.acquire();
178+
179+
try {
180+
buf.append('<');
181+
buf.append(Priority.getPriority(facility, event.getLevel()));
182+
buf.append('>');
183+
184+
if (header) {
185+
final int index = buf.length() + 4;
186+
dateConverter.format(event, buf);
187+
// RFC 3164 says leading space, not leading zero on days 1-9
188+
if (buf.charAt(index) == '0') {
189+
buf.setCharAt(index, Chars.SPACE);
190+
}
191+
192+
buf.append(Chars.SPACE);
193+
buf.append(localHostname);
194+
buf.append(Chars.SPACE);
183195
}
184196

185-
buf.append(Chars.SPACE);
186-
buf.append(localHostname);
187-
buf.append(Chars.SPACE);
188-
}
189-
190197
if (facilityPrinting) {
191198
buf.append(facility != null ? toRootLowerCase(facility.name()) : "user").append(':');
192199
}
193200

194-
buf.append(message);
195-
// TODO: splitting message into 1024 byte chunks?
196-
return buf.toString();
201+
buf.append(message);
202+
// TODO: splitting message into 1024 byte chunks?
203+
return buf.toString();
204+
} finally {
205+
stringBuilderRecycler.release(buf);
206+
}
197207
}
198208

199209
/**

log4j-1.2-api/src/main/java/org/apache/log4j/layout/Log4j1XmlLayout.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2626
import org.apache.logging.log4j.core.Layout;
2727
import org.apache.logging.log4j.core.LogEvent;
28+
import org.apache.logging.log4j.core.config.Configuration;
29+
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
2830
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
2931
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
32+
import org.apache.logging.log4j.core.layout.Encoder;
3033
import org.apache.logging.log4j.core.util.Transform;
3134
import org.apache.logging.log4j.plugins.Configurable;
3235
import org.apache.logging.log4j.plugins.Plugin;
@@ -53,15 +56,16 @@ public final class Log4j1XmlLayout extends AbstractStringLayout {
5356
@PluginFactory
5457
public static Log4j1XmlLayout createLayout(
5558
// @formatter:off
59+
@PluginConfiguration Configuration configuration,
5660
@PluginAttribute(value = "locationInfo") final boolean locationInfo,
5761
@PluginAttribute(value = "properties") final boolean properties
5862
// @formatter:on
5963
) {
60-
return new Log4j1XmlLayout(locationInfo, properties);
64+
return new Log4j1XmlLayout(configuration, locationInfo, properties);
6165
}
6266

63-
private Log4j1XmlLayout(final boolean locationInfo, final boolean properties) {
64-
super(StandardCharsets.UTF_8);
67+
private Log4j1XmlLayout(final Configuration configuration, final boolean locationInfo, final boolean properties) {
68+
super(configuration, StandardCharsets.UTF_8);
6569
this.locationInfo = locationInfo;
6670
this.properties = properties;
6771
}
@@ -76,16 +80,29 @@ public boolean isProperties() {
7680

7781
@Override
7882
public void encode(final LogEvent event, final ByteBufferDestination destination) {
79-
final StringBuilder text = getStringBuilder();
80-
formatTo(event, text);
81-
getStringBuilderEncoder().encode(text, destination);
83+
final StringBuilder text = stringBuilderRecycler.acquire();
84+
try {
85+
formatTo(event, text);
86+
final Encoder<StringBuilder> stringBuilderEncoder = stringBuilderEncoderRecycler.acquire();
87+
try {
88+
stringBuilderEncoder.encode(text, destination);
89+
} finally {
90+
stringBuilderEncoderRecycler.release(stringBuilderEncoder);
91+
}
92+
} finally {
93+
stringBuilderRecycler.release(text);
94+
}
8295
}
8396

8497
@Override
8598
public String toSerializable(final LogEvent event) {
86-
final StringBuilder text = getStringBuilder();
87-
formatTo(event, text);
88-
return text.toString();
99+
final StringBuilder text = stringBuilderRecycler.acquire();
100+
try {
101+
formatTo(event, text);
102+
return text.toString();
103+
} finally {
104+
stringBuilderRecycler.release(text);
105+
}
89106
}
90107

91108
@SuppressFBWarnings(

log4j-1.2-api/src/test/java/org/apache/log4j/layout/Log4j1SyslogLayoutTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.core.LogEvent;
2525
import org.apache.logging.log4j.core.StringLayout;
26+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
2627
import org.apache.logging.log4j.core.layout.PatternLayout;
2728
import org.apache.logging.log4j.core.net.Facility;
2829
import org.apache.logging.log4j.core.time.MutableInstant;
@@ -71,15 +72,18 @@ static Stream<Arguments> configurations() {
7172
public void testSimpleLayout(final String expected, final Facility facility, final boolean header, final boolean facilityPrinting) {
7273
final LogEvent logEvent = createLogEvent();
7374
StringLayout appenderLayout = Log4j1SyslogLayout.newBuilder()
75+
.setConfiguration(new DefaultConfiguration())
7476
.setFacility(facility)
7577
.setHeader(header)
7678
.setFacilityPrinting(facilityPrinting)
7779
.build();
7880
assertEquals(expected, appenderLayout.toSerializable(logEvent));
7981
final StringLayout messageLayout = PatternLayout.newBuilder()
82+
.setConfiguration(new DefaultConfiguration())
8083
.setPattern("%m")
8184
.build();
8285
appenderLayout = Log4j1SyslogLayout.newBuilder()
86+
.setConfiguration(new DefaultConfiguration())
8387
.setFacility(facility)
8488
.setHeader(header)
8589
.setFacilityPrinting(facilityPrinting)

log4j-1.2-api/src/test/java/org/apache/log4j/layout/Log4j1XmlLayoutTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.log4j.layout;
1818

1919
import org.apache.logging.log4j.Level;
20+
import org.apache.logging.log4j.core.config.DefaultConfiguration;
2021
import org.apache.logging.log4j.core.impl.ContextDataFactory;
2122
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2223
import org.apache.logging.log4j.message.SimpleMessage;
@@ -34,7 +35,7 @@ public class Log4j1XmlLayoutTest {
3435

3536
@Test
3637
public void testWithoutThrown() {
37-
final Log4j1XmlLayout layout = Log4j1XmlLayout.createLayout(false, true);
38+
final Log4j1XmlLayout layout = Log4j1XmlLayout.createLayout(new DefaultConfiguration(), false, true);
3839

3940
final Log4jLogEvent event = Log4jLogEvent.newBuilder()
4041
.setLoggerName("a.B")
@@ -55,7 +56,7 @@ public void testWithoutThrown() {
5556

5657
@Test
5758
public void testWithPropertiesAndLocationInfo() {
58-
final Log4j1XmlLayout layout = Log4j1XmlLayout.createLayout(true, true);
59+
final Log4j1XmlLayout layout = Log4j1XmlLayout.createLayout(new DefaultConfiguration(), true, true);
5960

6061
final StringMap contextMap = ContextDataFactory.createContextData(2);
6162
contextMap.putValue("key1", "value1");

log4j-api-test/src/test/java/org/apache/logging/log4j/message/ReusableMessageFactoryTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.message;
1818

19+
import org.apache.logging.log4j.spi.ThreadLocalRecyclerFactory;
20+
import org.junit.jupiter.api.BeforeEach;
1921
import org.junit.jupiter.api.Test;
2022

2123
import static org.junit.jupiter.api.Assertions.*;
@@ -25,35 +27,39 @@
2527
*/
2628
public class ReusableMessageFactoryTest {
2729

30+
private ReusableMessageFactory factory;
31+
32+
@BeforeEach
33+
void setUp() {
34+
factory = new ReusableMessageFactory(new ThreadLocalRecyclerFactory(8));
35+
}
36+
2837
@Test
2938
public void testCreateEventReturnsDifferentInstanceIfNotReleased() throws Exception {
30-
final ReusableMessageFactory factory = new ReusableMessageFactory();
3139
final Message message1 = factory.newMessage("text, p0={} p1={} p2={} p3={}", 1, 2, 3, 4);
3240
final Message message2 = factory.newMessage("text, p0={} p1={} p2={} p3={}", 9, 8, 7, 6);
3341
assertNotSame(message1, message2);
34-
ReusableMessageFactory.release(message1);
35-
ReusableMessageFactory.release(message2);
42+
factory.recycle(message1);
43+
factory.recycle(message2);
3644
}
3745

3846
@Test
3947
public void testCreateEventReturnsSameInstance() throws Exception {
40-
final ReusableMessageFactory factory = new ReusableMessageFactory();
4148
final Message message1 = factory.newMessage("text, p0={} p1={} p2={} p3={}", 1, 2, 3, 4);
4249

43-
ReusableMessageFactory.release(message1);
50+
factory.recycle(message1);
4451
final Message message2 = factory.newMessage("text, p0={} p1={} p2={} p3={}", 9, 8, 7, 6);
4552
assertSame(message1, message2);
4653

47-
ReusableMessageFactory.release(message2);
54+
factory.recycle(message2);
4855
final Message message3 = factory.newMessage("text, AAA={} BBB={} p2={} p3={}", 9, 8, 7, 6);
4956
assertSame(message2, message3);
50-
ReusableMessageFactory.release(message3);
57+
factory.recycle(message3);
5158
}
5259

5360
private void assertReusableParameterizeMessage(final Message message, final String txt, final Object[] params) {
5461
assertTrue(message instanceof ReusableParameterizedMessage);
5562
final ReusableParameterizedMessage msg = (ReusableParameterizedMessage) message;
56-
assertTrue(msg.reserved, "reserved");
5763

5864
assertEquals(txt, msg.getFormat());
5965
assertEquals(msg.getParameterCount(), params.length, "count");
@@ -65,7 +71,6 @@ private void assertReusableParameterizeMessage(final Message message, final Stri
6571

6672
@Test
6773
public void testCreateEventOverwritesFields() throws Exception {
68-
final ReusableMessageFactory factory = new ReusableMessageFactory();
6974
final Message message1 = factory.newMessage("text, p0={} p1={} p2={} p3={}", 1, 2, 3, 4);
7075
assertReusableParameterizeMessage(message1, "text, p0={} p1={} p2={} p3={}", new Object[]{
7176
new Integer(1), //
@@ -74,7 +79,7 @@ public void testCreateEventOverwritesFields() throws Exception {
7479
new Integer(4), //
7580
});
7681

77-
ReusableMessageFactory.release(message1);
82+
factory.recycle(message1);
7883
final Message message2 = factory.newMessage("other, A={} B={} C={} D={}", 1, 2, 3, 4);
7984
assertReusableParameterizeMessage(message1, "other, A={} B={} C={} D={}", new Object[]{
8085
new Integer(1), //
@@ -83,12 +88,11 @@ public void testCreateEventOverwritesFields() throws Exception {
8388
new Integer(4), //
8489
});
8590
assertSame(message1, message2);
86-
ReusableMessageFactory.release(message2);
91+
factory.recycle(message2);
8792
}
8893

8994
@Test
9095
public void testCreateEventReturnsThreadLocalInstance() throws Exception {
91-
final ReusableMessageFactory factory = new ReusableMessageFactory();
9296
final Message[] message1 = new Message[1];
9397
final Message[] message2 = new Message[1];
9498
final Thread t1 = new Thread("THREAD 1") {
@@ -123,8 +127,8 @@ public void run() {
123127
new Integer(3), //
124128
new Integer(4), //
125129
});
126-
ReusableMessageFactory.release(message1[0]);
127-
ReusableMessageFactory.release(message2[0]);
130+
factory.recycle(message1[0]);
131+
factory.recycle(message2[0]);
128132
}
129133

130134
}

0 commit comments

Comments
 (0)