Skip to content

Commit d045433

Browse files
committed
Add another method for adding static field to GelfEncoder #80
1 parent 07b8a85 commit d045433

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Support for key value pairs
1010
[\#86](https://github.com/osiegmar/logback-gelf/issues/86)
11+
- Add another method for adding static field to GelfEncoder
12+
[\#80](https://github.com/osiegmar/logback-gelf/issues/80)
1113

1214
### Changed
1315
- Upgrade to Java 11 (Premier Support of Java 8 ended in March 2022).

src/main/java/de/siegmar/logbackgelf/GelfEncoder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,22 @@ public Map<String, Object> getStaticFields() {
264264
return Collections.unmodifiableMap(staticFields);
265265
}
266266

267+
public void addStaticField(final String key, final Object value) {
268+
try {
269+
addField(staticFields, key, value);
270+
} catch (final IllegalArgumentException e) {
271+
addWarn("Could not add field " + key, e);
272+
}
273+
}
274+
267275
public void addStaticField(final String staticField) {
268276
final String[] split = staticField.split(":", 2);
269277
if (split.length != 2) {
270278
addWarn("staticField must be in format key:value - rejecting '" + staticField + "'");
271279
return;
272280
}
273281

274-
try {
275-
addField(staticFields, split[0].trim(), split[1].trim());
276-
} catch (final IllegalArgumentException e) {
277-
addWarn("Could not add field " + staticField, e);
278-
}
282+
addStaticField(split[0].trim(), split[1].trim());
279283
}
280284

281285
public List<GelfFieldMapper<?>> getFieldMappers() {

src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public void complex() throws IOException {
197197
encoder.setIncludeRawMessage(true);
198198
encoder.setIncludeLevelName(true);
199199
encoder.addStaticField("foo:bar");
200+
encoder.addStaticField("bar", "baz");
200201
encoder.setIncludeCallerData(true);
201202
encoder.setIncludeRootCauseData(true);
202203
encoder.start();
@@ -215,6 +216,7 @@ public void complex() throws IOException {
215216
basicValidation(jsonNode);
216217
assertEquals("DEBUG", jsonNode.get("_level_name").textValue());
217218
assertEquals("bar", jsonNode.get("_foo").textValue());
219+
assertEquals("baz", jsonNode.get("_bar").textValue());
218220
assertEquals("mdc_value", jsonNode.get("_mdc_key").textValue());
219221
assertEquals("message {}", jsonNode.get("_raw_message").textValue());
220222
assertNull(jsonNode.get("_exception"));

0 commit comments

Comments
 (0)