Skip to content

Commit 17a64cb

Browse files
Change Custom Fields JSON key to #cf
This is the first step towards the new custom fields approach. Since #cf is not a valid Java variable name, the code generation needed to be changed. Based on the new fields.yml code and documentation were generated. The failing unit test were adapted to the new JSON key.
1 parent 4a65ad3 commit 17a64cb

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

cf-java-logging-support-core/beats/app-logs/docs/fields.asciidoc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,23 @@ required: False
336336
A list of names to further categorize this log message.
337337

338338

339-
=== custom_fields Fields
339+
==== #cf
340340

341-
A collection of non-standard fields as key-value pairs.
341+
type: object
342+
343+
example: "#cf": {
344+
"string": [
345+
{"l":"some_label", "v":"some_value", "i": 0},
346+
{"l":"other_label", "v":"other_value", "i": 1}
347+
]
348+
}
349+
350+
351+
required: False
352+
353+
An object containing collections of non-standard fields.
354+
The field "string" contains custom fields with label "l", value "v" and an index "i".
355+
The index can be used for field order during parsing.
342356

343357
NOTE: As this is "custom" there are no predefined fields here!
344358

cf-java-logging-support-core/beats/app-logs/etc/fields.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,20 @@ app-logs:
249249
description: |
250250
A list of names to further categorize this log message.
251251
252-
- name: "custom_fields"
253-
type: group
252+
- name: "#cf"
253+
java_alias: "CUSTOM_FIELDS"
254+
type: object
254255
required: false
255-
example: "cutom_fields: {\"some_key\": \"some_value\"}"
256+
example: |
257+
"#cf": {
258+
"string": [
259+
{"l":"some_label", "v":"some_value", "i": 0},
260+
{"l":"other_label", "v":"other_value", "i": 1}
261+
]
262+
}
256263
description: |
257-
A collection of non-standard fields as key-value pairs.
264+
An object containing collections of non-standard fields.
265+
The field "string" contains custom fields with label "l", value "v" and an index "i".
266+
The index can be used for field order during parsing.
258267
259268
NOTE: As this is "custom" there are no predefined fields here!

cf-java-logging-support-core/beats/scripts/gen_java_fields.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
spec.keys.each do |k|
1818
if spec[k].class == Hash && spec[k].has_key?('fields')
1919
spec[k]['fields'].each do |f|
20-
fields.add(f['name'])
20+
if fields.select { |c| c['name'].upcase == f['name'].upcase}.empty?
21+
fields.add(f)
22+
end
2123
end
2224
end
2325
end
@@ -37,7 +39,8 @@
3739
EOD
3840

3941
fields.each do |f|
40-
puts " public String #{f.upcase} = \"#{f}\";"
42+
key = f.has_key?('java_alias') ? f['java_alias'] : f['name']
43+
puts " public String #{key.upcase} = \"#{f['name']}\";"
4144
end
4245

4346
puts "}"

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/Fields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface Fields {
3232
public String MSG = "msg";
3333
public String STACKTRACE = "stacktrace";
3434
public String CATEGORIES = "categories";
35-
public String CUSTOM_FIELDS = "custom_fields";
35+
public String CUSTOM_FIELDS = "#cf";
3636
public String REQUEST = "request";
3737
public String REQUEST_SENT_AT = "request_sent_at";
3838
public String REQUEST_RECEIVED_AT = "request_received_at";

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/log4j2/layout/LayoutPatternBuilderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void contextProperties() throws Exception {
5151
public void customFields() throws Exception {
5252
String pattern = new LayoutPatternBuilder().addCustomFields(Arrays.asList("this key", "that key")).build();
5353

54-
assertThat(pattern, specificPart(is(",\"custom_fields\":{%cf{this key}{that key}}")));
54+
assertThat(pattern, specificPart(is(",\"#cf\":{%cf{this key}{that key}}")));
5555
}
5656

5757
@Test
@@ -83,7 +83,7 @@ public void applicationScenario() throws Exception {
8383
.suppressExceptions().build();
8484

8585
assertThat(pattern, specificPart(is(
86-
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{excluded-field},\"custom_fields\":{%cf{custom-field}}%ex{0} ")));
86+
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{excluded-field},\"#cf\":{%cf{custom-field}}%ex{0} ")));
8787
}
8888

8989
@Test
@@ -93,7 +93,7 @@ public void exceptionScenario() throws Exception {
9393
.build();
9494

9595
assertThat(pattern, specificPart(is(
96-
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{excluded-field},\"custom_fields\":{%cf{custom-field}},\"stacktrace\":%stacktrace")));
96+
",\"type\":\"log\",\"logger\":\"%replace{%logger}{\"}{\\\\\"}\",\"thread\":\"%replace{%thread}{\"}{\\\\\"}\",\"level\":\"%p\",\"categories\":%categories,\"msg\":%jsonmsg{escape},%ctxp{excluded-field},\"#cf\":{%cf{custom-field}},\"stacktrace\":%stacktrace")));
9797
}
9898

9999
private static Matcher<String> specificPart(Matcher<String> expected) {

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/logging/common/AbstractTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private String getLastLine() {
6666
}
6767

6868
protected String getCustomField(String fieldName) {
69-
Map<String, Object> cfMap = getMap("custom_fields");
69+
Map<String, Object> cfMap = getMap(Fields.CUSTOM_FIELDS);
7070
Object fObj = cfMap.get(fieldName);
7171
if (fObj != null) {
7272
return fObj.toString();

cf-java-logging-support-log4j2/src/test/java/com/sap/hcp/cf/logging/common/TestCustomFields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void testOrderOfLogMessageWithTwoCustomFields() {
8686
}
8787

8888
private String getCustomFields() {
89-
return getField("custom_fields");
89+
return getField(Fields.CUSTOM_FIELDS);
9090
}
9191

9292
@Test

0 commit comments

Comments
 (0)