Skip to content

Commit 45d575c

Browse files
committed
Improve structured logging docs
1 parent 8fc5553 commit 45d575c

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

log4j2-ecs-layout/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ To include any custom field in the output, use following syntax:
5858
```
5959

6060
Custom fields are included in the order they are declared. The values support [lookups](https://logging.apache.org/log4j/2.x/manual/lookups.html).
61+
6162
## Structured logging
6263

6364
By leveraging log4j2's `MapMessage` or even by implementing your own `MultiformatMessage` with JSON support,
@@ -66,9 +67,19 @@ you can add additional fields to the resulting JSON.
6667
Example:
6768

6869
```java
69-
logger.info(new StringMapMessage().with("message", "foo").with("foo", "bar"));
70+
logger.info(new StringMapMessage()
71+
.with("message", "Hello World!")
72+
.with("foo", "bar"));
7073
```
7174

75+
### Tips
76+
It's recommended to use existing [ECS fields](https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html).
77+
78+
If there is no appropriate ECS field,
79+
consider prefixing your fields with `labels.`, as in `labels.foo`, for simple key/value pairs.
80+
For nested structures consider prefixing with `custom.` to make sure you won't get conflicts if ECS later adds the same fields but with a different mapping.
81+
82+
7283
### Gotchas
7384

7485
A common pitfall is how dots in field names are handled in Elasticsearch and how they affect the mapping.

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

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

87+
@Test
88+
void testParameterizedStructuredMessage() throws Exception {
89+
root.info(ParameterizedStructuredMessage.of("hello {}", "world").with("foo", "bar"));
90+
assertThat(getLastLogLine().get("message").textValue()).isEqualTo("hello world");
91+
assertThat(getLastLogLine().get("foo").textValue()).isEqualTo("bar");
92+
}
93+
8794
@Test
8895
void testCustomPatternConverter() throws Exception {
8996
debug("test");
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package co.elastic.logging.log4j2;
2+
3+
import org.apache.logging.log4j.message.MapMessage;
4+
import org.apache.logging.log4j.message.ParameterizedMessage;
5+
import org.apache.logging.log4j.util.TriConsumer;
6+
7+
public class ParameterizedStructuredMessage extends MapMessage<ParameterizedStructuredMessage, Object> {
8+
9+
private static final String MESSAGE = "message";
10+
private static final TriConsumer<String, Object, StringBuilder> ADD_KEY_VALUE_PAIR = new TriConsumer<>() {
11+
@Override
12+
public void accept(String key, Object value, StringBuilder builder) {
13+
if (!MESSAGE.equals(key)) {
14+
builder.append(' ').append(key).append('[').append(value).append(']');
15+
}
16+
}
17+
};
18+
19+
private final String message;
20+
21+
private ParameterizedStructuredMessage(String message) {
22+
this.message = message;
23+
with(MESSAGE, message);
24+
}
25+
26+
private ParameterizedStructuredMessage(String messagePattern, Object... arguments) {
27+
this.message = ParameterizedMessage.format(messagePattern, arguments);
28+
with(MESSAGE, message);
29+
}
30+
31+
public static ParameterizedStructuredMessage of(String message) {
32+
return new ParameterizedStructuredMessage(message);
33+
}
34+
35+
public static ParameterizedStructuredMessage of(String messagePattern, Object... arguments) {
36+
return new ParameterizedStructuredMessage(messagePattern, arguments);
37+
}
38+
39+
@Override
40+
protected void asJson(StringBuilder sb) {
41+
super.asJson(sb);
42+
}
43+
44+
protected void appendMap(final StringBuilder sb) {
45+
sb.append(message);
46+
forEach(ADD_KEY_VALUE_PAIR, sb);
47+
}
48+
}

0 commit comments

Comments
 (0)