Skip to content

Commit

Permalink
AbstractLayout changed to Layout (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfoltyns committed Feb 4, 2020
1 parent aafdbe4 commit 1ff08e6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.ConfigurationException;
Expand All @@ -32,8 +33,6 @@
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.apache.logging.log4j.core.layout.AbstractLayout;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;

import java.util.concurrent.TimeUnit;

Expand All @@ -43,8 +42,8 @@
* <p>
* Formatted message may be produced by
* <ul>
* <li> (default) {@code org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(LogEvent)}
* <li> provided {@code org.apache.logging.log4j.core.layout.AbstractLayout.toSerializable(LogEvent)}
* <li> {@code org.apache.logging.log4j.core.Layout.toSerializable(LogEvent)} e.g. {@code org.apache.logging.log4j.core.layout.JsonLayout}
* <li> {@link ItemSourceLayout} e.g. {@link JacksonJsonLayout}
* <li> or {@code org.apache.logging.log4j.message.Message.getFormattedMessage()} (see {@link ElasticsearchAppender.Builder#withMessageOnly(boolean)}
* messageOnly})
* </ul>
Expand All @@ -57,7 +56,7 @@ public class ElasticsearchAppender extends AbstractAppender {
private final IndexNameFormatter indexNameFormatter;
private final ItemAppender itemAppender;

protected ElasticsearchAppender(String name, Filter filter, AbstractLayout layout,
protected ElasticsearchAppender(String name, Filter filter, Layout layout,
boolean ignoreExceptions, BatchDelivery batchDelivery, boolean messageOnly, IndexNameFormatter indexNameFormatter) {
super(name, filter, layout, ignoreExceptions);
this.indexNameFormatter = indexNameFormatter;
Expand Down Expand Up @@ -94,7 +93,7 @@ public static class Builder implements org.apache.logging.log4j.core.util.Builde
private Filter filter;

@PluginElement("layout")
private AbstractLayout layout;
private Layout layout;

@PluginBuilderAttribute
private boolean ignoreExceptions;
Expand Down Expand Up @@ -141,7 +140,7 @@ public Builder withFilter(Filter filter) {
* @param layout layout to be used
* @return Builder this
*/
public Builder withLayout(AbstractLayout layout) {
public Builder withLayout(Layout layout) {
this.layout = layout;
return this;
}
Expand All @@ -168,7 +167,7 @@ public Builder withBatchDelivery(BatchDelivery batchDelivery) {
* Default: false
*
* @param messageOnly If true, formatted message will be produced by {@link org.apache.logging.log4j.message.Message#getFormattedMessage}.
* Otherwise, configured {@link AbstractStringLayout} will be used
* Otherwise, configured {@link Layout} will be used
* @return Builder this
*/
public Builder withMessageOnly(boolean messageOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
* #L%
*/

import org.apache.logging.log4j.core.config.ConfigurationException;
import org.apache.logging.log4j.core.layout.AbstractLayout;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.Layout;

public class ItemAppenderFactory {

Expand All @@ -35,7 +33,7 @@ public class ItemAppenderFactory {
* @param batchDelivery serialization result consumer
* @return configured {@link ItemAppender}
*/
public ItemAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemAppender createInstance(boolean messageOnly, Layout<String> layout, BatchDelivery batchDelivery) {

if (layout instanceof ItemSourceLayout) {
ItemSourceLayout itemSourceLayout = (ItemSourceLayout) layout;
Expand All @@ -46,12 +44,7 @@ public ItemAppender createInstance(boolean messageOnly, AbstractLayout layout, B
return new StringAppender(batchDelivery, logEvent -> logEvent.getMessage().getFormattedMessage());
}

if (layout instanceof AbstractStringLayout) {
AbstractStringLayout abstractStringLayout = (AbstractStringLayout) layout;
return new StringAppender(batchDelivery, abstractStringLayout::toSerializable);
}

throw new ConfigurationException("Unsupported layout: " + layout.getClass());
return new StringAppender(batchDelivery, layout::toSerializable);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LifeCycle;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationException;
import org.apache.logging.log4j.core.filter.ThresholdFilter;
import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
import org.apache.logging.log4j.core.layout.AbstractLayout;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.JsonLayout;
import org.apache.logging.log4j.message.SimpleMessage;
import org.appenders.log4j2.elasticsearch.ElasticsearchAppender.Builder;
Expand Down Expand Up @@ -148,7 +148,7 @@ public void appenderDelegatesToItemAppender() {
public void appenderUsesProvidedLayoutWhenMessageOnlyIsSetToFalse() {

// given
AbstractStringLayout layout = PowerMockito.mock(AbstractStringLayout.class);
Layout layout = PowerMockito.mock(Layout.class);

ElasticsearchAppender.Builder builder = ElasticsearchAppenderTest.createTestElasticsearchAppenderBuilder();
builder.withMessageOnly(false);
Expand Down Expand Up @@ -236,7 +236,7 @@ public void lifecycleStartStartsItemAppender() {

ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() {
@Override
public ItemSourceAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemSourceAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) {
return mockItemAppender;
}
};
Expand All @@ -260,7 +260,7 @@ public void lifecycleStopStopsItemAppenderOnlyOnce() {

ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() {
@Override
public ItemSourceAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemSourceAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) {
return mockItemAppender;
}
};
Expand All @@ -285,7 +285,7 @@ public void lifecycleStopWithTimeoutStopsItemAppenderOnlyOnce() {

ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() {
@Override
public ItemSourceAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemSourceAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) {
return mockItemAppender;
}
};
Expand All @@ -312,7 +312,7 @@ public void lifecycleStopStopsItemSourceLayout() {

ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() {
@Override
public ItemAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) {
return mockItemAppender;
}
};
Expand Down Expand Up @@ -340,7 +340,7 @@ public void lifecycleStopDoesntInteractWithAbstractLayout() {

ItemAppenderFactory itemAppenderFactory = new ItemAppenderFactory() {
@Override
public ItemAppender createInstance(boolean messageOnly, AbstractLayout layout, BatchDelivery batchDelivery) {
public ItemAppender createInstance(boolean messageOnly, Layout layout, BatchDelivery batchDelivery) {
return mockItemAppender;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
* #L%
*/

import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationException;
import org.apache.logging.log4j.core.layout.AbstractLayout;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
import org.apache.logging.log4j.message.Message;
import org.junit.Rule;
Expand All @@ -33,12 +30,14 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.Serializable;
import java.util.Map;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -69,15 +68,15 @@ public class ItemAppenderFactoryTest {
public ExpectedException expectedException = ExpectedException.none();

@Test
public void messageOnlyAbstractStringLayout() {
public void messageOnlyLayout() {

// given
ItemAppenderFactory factory = new ItemAppenderFactory();
String formattedIndexName = UUID.randomUUID().toString();
BatchDelivery batchDelivery = mock(BatchDelivery.class);

AbstractStringLayout abstractStringLayout = mock(AbstractStringLayout.class);
ItemAppender itemAppender = factory.createInstance(true, abstractStringLayout, batchDelivery);
Layout Layout = mock(Layout.class);
ItemAppender itemAppender = factory.createInstance(true, Layout, batchDelivery);

String expectedMessage = UUID.randomUUID().toString();
LogEvent logEvent = mock(LogEvent.class);
Expand All @@ -89,9 +88,9 @@ public void messageOnlyAbstractStringLayout() {
itemAppender.append(formattedIndexName, logEvent);

// then
assertEquals(StringAppender.class, itemAppender.getClass());
assertTrue(itemAppender instanceof StringAppender);

verify(abstractStringLayout, never()).toSerializable(any());
verify(Layout, never()).toSerializable(any());

verify(batchDelivery).add(indexNameCaptor.capture(), stringLogCaptor.capture());
assertEquals(formattedIndexName, indexNameCaptor.getValue());
Expand All @@ -100,14 +99,14 @@ public void messageOnlyAbstractStringLayout() {
}

@Test
public void nonMessageOnlyAbstractStringLayout() {
public void nonMessageOnlyLayout() {

// given
ItemAppenderFactory factory = new ItemAppenderFactory();
String formattedIndexName = UUID.randomUUID().toString();
BatchDelivery batchDelivery = mock(BatchDelivery.class);

AbstractStringLayout stringBasedLayout = mock(AbstractStringLayout.class);
Layout stringBasedLayout = mock(Layout.class);
ItemAppender itemAppender = factory.createInstance(false, stringBasedLayout, batchDelivery);

String expectedMessage = UUID.randomUUID().toString();
Expand All @@ -119,7 +118,7 @@ public void nonMessageOnlyAbstractStringLayout() {
itemAppender.append(formattedIndexName, logEvent);

// then
assertEquals(StringAppender.class, itemAppender.getClass());
assertTrue(itemAppender instanceof StringAppender);

verify(stringBasedLayout).toSerializable(logEventCaptor.capture());
assertEquals(logEvent, logEventCaptor.getValue());
Expand All @@ -131,16 +130,16 @@ public void nonMessageOnlyAbstractStringLayout() {
}

@Test
public void givenAbstractLayoutImplementingItemSourceLayoutDelegatesToItemSourceLayoutFactoryMethod() {
public void givenAnyLayoutImplementingItemSourceLayoutDelegatesToItemSourceLayoutFactoryMethod() {

// given
ItemAppenderFactory factory = spy(new ItemAppenderFactory());

BatchDelivery batchDelivery = mock(BatchDelivery.class);
ItemSourceLayout itemSourceLayout = spy(new TestItemSourceaLayout(mock(Configuration.class)));
ItemSourceLayout itemSourceLayout = spy(new TestItemSourceLayout());

// when
factory.createInstance(false, (AbstractLayout)itemSourceLayout, batchDelivery);
factory.createInstance(false, itemSourceLayout, batchDelivery);

// then
verify(factory).createInstance(eq(false), eq(itemSourceLayout), eq(batchDelivery));
Expand All @@ -156,8 +155,8 @@ public void nonMessageOnlyItemSourceLayout() {
String formattedIndexName = UUID.randomUUID().toString();

BatchDelivery batchDelivery = mock(BatchDelivery.class);
ItemSourceLayout itemSourceLayout = spy(new TestItemSourceaLayout(mock(Configuration.class)));
ItemSourceAppender itemAppender = (ItemSourceAppender) factory.createInstance(false, (AbstractLayout)itemSourceLayout, batchDelivery);
ItemSourceLayout itemSourceLayout = spy(new TestItemSourceLayout());
ItemSourceAppender itemAppender = factory.createInstance(false, itemSourceLayout, batchDelivery);

String expectedMessage = UUID.randomUUID().toString();
ItemSource itemSource = new StringItemSource(expectedMessage);
Expand Down Expand Up @@ -187,8 +186,9 @@ public void messageOnlyItemSourceLayout() {
String formattedIndexName = UUID.randomUUID().toString();
BatchDelivery batchDelivery = mock(BatchDelivery.class);

ItemSourceLayout itemSourceLayout = spy(new TestItemSourceaLayout(mock(Configuration.class)));
ItemSourceAppender itemAppender = (ItemSourceAppender) factory.createInstance(true, (AbstractLayout)itemSourceLayout, batchDelivery);
ItemSourceLayout itemSourceLayout = spy(new TestItemSourceLayout());
ItemSourceAppender itemAppender =
factory.createInstance(true, itemSourceLayout, batchDelivery);

String expectedMessage = UUID.randomUUID().toString();
ItemSource itemSource = new StringItemSource(expectedMessage);
Expand All @@ -214,34 +214,25 @@ public void messageOnlyItemSourceLayout() {

}

@Test
public void unsupportedLayout() {

// given
ItemAppenderFactory factory = new ItemAppenderFactory();

expectedException.expect(ConfigurationException.class);
expectedException.expectMessage("Unsupported layout");

// when
factory.createInstance(false, mock(AbstractLayout.class), mock(BatchDelivery.class));

}

private LogEvent createDefaultTestLogEvent() {
LogEvent logEvent = mock(LogEvent.class);
return logEvent;
return mock(LogEvent.class);
}

private class TestItemSourceaLayout extends AbstractLayout implements ItemSourceLayout {
private class TestItemSourceLayout implements ItemSourceLayout, Layout {

public TestItemSourceaLayout(Configuration configuration) {
super(configuration, null, null);
@Override
public byte[] getFooter() {
return new byte[0];
}

@Override
public byte[] getHeader() {
return new byte[0];
}

@Override
public byte[] toByteArray(LogEvent event) {
return null;
return new byte[0];
}

@Override
Expand All @@ -254,6 +245,11 @@ public String getContentType() {
return null;
}

@Override
public Map<String, String> getContentFormat() {
return null;
}

@Override
public void encode(Object source, ByteBufferDestination destination) {

Expand Down
6 changes: 3 additions & 3 deletions log4j2-elasticsearch2-bulkprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ Delivery is triggered each `deliveryInterval` or when number of undelivered logs
`deliveryInterval` is the main driver of delivery. However, in high load scenarios, both parameters should be configured accordingly to prevent sub-optimal behaviour. See [Indexing performance tips](https://www.elastic.co/guide/en/elasticsearch/guide/current/indexing-performance.html) and [Performance Considerations](https://www.elastic.co/blog/performance-considerations-elasticsearch-indexing) for more info.

### Message output
There are at least three ways to generate output
* (default) JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
There are multiple ways to generate output
* JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
* `messageOnly="true"` can be configured set to make use of user-provided (or default) `org.apache.logging.log4j.message.Message.getFormattedMessage()` implementation
* custom `org.apache.logging.log4j.core.layout.AbstractStringLayout` can be provided to appender config to use any other serialization mechanism
* custom `org.apache.logging.log4j.core.Layout` can be provided to appender config to use any other serialization mechanism

### Failover
Each unsuccessful batch can be redirected to any given `FailoverPolicy` implementation. By default, each log entry will be separately delivered to configured strategy class, but this behaviour can be amended by providing custom `ClientObjectFactory` implementation.
Expand Down
4 changes: 2 additions & 2 deletions log4j2-elasticsearch5-bulkprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Delivery is triggered each `deliveryInterval` or when number of undelivered logs

### Message output
There are at least three ways to generate output
* (default) JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
* JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
* `messageOnly="true"` can be configured set to make use of user-provided (or default) `org.apache.logging.log4j.message.Message.getFormattedMessage()` implementation
* custom `org.apache.logging.log4j.core.layout.AbstractStringLayout` can be provided to appender config to use any other serialization mechanism
* custom `org.apache.logging.log4j.core.Layout` can be provided to appender config to use any other serialization mechanism

### Failover
Each unsuccessful batch can be redirected to any given `FailoverPolicy` implementation. By default, each log entry will be separately delivered to configured strategy class, but this behaviour can be amended by providing custom `ClientObjectFactory` implementation.
Expand Down
4 changes: 2 additions & 2 deletions log4j2-elasticsearch6-bulkprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Delivery is triggered each `deliveryInterval` or when number of undelivered logs

### Message output
There are at least three ways to generate output
* (default) JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
* JsonLayout will serialize LogEvent using Jackson mapper configured in log4j-core
* `messageOnly="true"` can be configured set to make use of user-provided (or default) `org.apache.logging.log4j.message.Message.getFormattedMessage()` implementation
* custom `org.apache.logging.log4j.core.layout.AbstractStringLayout` can be provided to appender config to use any other serialization mechanism
* custom `org.apache.logging.log4j.core.Layout` can be provided to appender config to use any other serialization mechanism

### Failover
Each unsuccessful batch can be redirected to any given `FailoverPolicy` implementation. By default, each log entry will be separately delivered to configured strategy class, but this behaviour can be amended by providing custom `ClientObjectFactory` implementation.
Expand Down

0 comments on commit 1ff08e6

Please sign in to comment.