Skip to content

Commit 5a78032

Browse files
committed
Test with custom pattern converter
1 parent 82451b7 commit 5a78032

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

log4j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/AbstractLog4j2EcsLayoutTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ void testMapMessage() throws Exception {
8484
assertThat(log.get("foo").textValue()).isEqualTo("bar");
8585
}
8686

87+
@Test
88+
void testCustomPatternConverter() throws Exception {
89+
debug("test");
90+
assertThat(getLastLogLine().get("custom").textValue()).isEqualTo("foo");
91+
}
92+
8793
@Override
8894
public void putMdc(String key, String value) {
8995
ThreadContext.put(key, value);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* #%L
3+
* Java ECS logging
4+
* %%
5+
* Copyright (C) 2019 Elastic and contributors
6+
* %%
7+
* Licensed to Elasticsearch B.V. under one or more contributor
8+
* license agreements. See the NOTICE file distributed with
9+
* this work for additional information regarding copyright
10+
* ownership. Elasticsearch B.V. licenses this file to you under
11+
* the Apache License, Version 2.0 (the "License"); you may
12+
* not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing,
18+
* software distributed under the License is distributed on an
19+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
* KIND, either express or implied. See the License for the
21+
* specific language governing permissions and limitations
22+
* under the License.
23+
* #L%
24+
*/
25+
package co.elastic.logging.log4j2;
26+
27+
import org.apache.logging.log4j.core.LogEvent;
28+
import org.apache.logging.log4j.core.config.plugins.Plugin;
29+
import org.apache.logging.log4j.core.pattern.ConverterKeys;
30+
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
31+
import org.apache.logging.log4j.core.pattern.PatternConverter;
32+
33+
@Plugin(category = PatternConverter.CATEGORY, name = "CustomPatternConverter")
34+
@ConverterKeys({"custom"})
35+
public class CustomPatternConverter extends LogEventPatternConverter {
36+
37+
public CustomPatternConverter(final String[] options) {
38+
super("Custom", "custom");
39+
}
40+
41+
public static CustomPatternConverter newInstance(final String[] options) {
42+
return new CustomPatternConverter(options);
43+
}
44+
45+
@Override
46+
public void format(LogEvent event, StringBuilder toAppendTo) {
47+
toAppendTo.append("foo");
48+
}
49+
}

log4j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/Log4j2EcsLayoutTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void setUp() {
7676
new KeyValuePair("node.id", "${node.id}"),
7777
new KeyValuePair("empty", "${empty}"),
7878
new KeyValuePair("clazz", "%C"),
79+
new KeyValuePair("custom", "%custom"),
7980
new KeyValuePair("emptyPattern", "%notEmpty{%invalidPattern}"),
8081
})
8182
.build();

log4j2-ecs-layout/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<KeyValuePair key="node.id" value="${node.id}"/>
1111
<KeyValuePair key="empty" value="${empty}"/>
1212
<KeyValuePair key="clazz" value="%C"/>
13+
<KeyValuePair key="custom" value="%custom"/>
1314
<KeyValuePair key="emptyPattern" value="%notEmpty{%invalidPattern}"/>
1415
</EcsLayout>
1516
</List>

0 commit comments

Comments
 (0)