Skip to content

Commit 6a020a8

Browse files
authored
Merge pull request #15 from avaje/feature/propertyNames
Support json property name override via logger.propertyNames
2 parents 8e407cb + efda803 commit 6a020a8

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ By default, the log format is JSON. Example:
120120
}
121121
```
122122

123+
To override the json property names use `logger.propertyNames` delimited by `=` and `,` like:
124+
```properties
125+
## delimited by comma and equals
126+
logger.propertyNames=logger=loggerName,env=environment,timestamp=@timestamp
127+
```
128+
129+
Example overriding a single property:
130+
```properties
131+
logger.propertyNames=logger=loggerName
132+
```
133+
123134
#### component
124135

125136
A `component` key value is added if:

avaje-simple-json-logger/src/main/java/io/avaje/simplelogger/encoder/Bootstrap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private static LogWriter createWriter(Properties properties, String writerType)
5757
var jsonEncoder = new JsonEncoderBuilder()
5858
.component(property(properties, "logger.component"))
5959
.environment(property(properties,"logger.environment"))
60+
.propertyNames(property(properties,"logger.propertyNames"))
6061
.customFields(property(properties, "logger.customFields"))
6162
.timestampPattern(timestampPattern)
6263
.timeZone(timeZone)

avaje-simple-json-logger/src/main/java/io/avaje/simplelogger/encoder/JsonEncoder.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ final class JsonEncoder {
2424
private final String environment;
2525
private final boolean includeStackHash;
2626

27-
JsonEncoder(JsonStream json, String component, String environment, StackHasher stackHasher, DateTimeFormatter formatter, boolean includeStackHash, Map<String, String> customFieldsMap, ThrowableConverter throwableConverter) {
27+
JsonEncoder(
28+
String[] propertyNames,
29+
JsonStream json,
30+
String component,
31+
String environment,
32+
StackHasher stackHasher,
33+
DateTimeFormatter formatter,
34+
boolean includeStackHash,
35+
Map<String, String> customFieldsMap,
36+
ThrowableConverter throwableConverter) {
37+
2838
this.json = json;
29-
this.properties = this.json.properties("component", "env", "timestamp", "level", "logger", "message", "thread", "stackhash", "stacktrace");
39+
this.properties = this.json.properties(propertyNames);
3040
this.component = component;
3141
this.environment = environment;
3242
this.stackHasher = stackHasher;

avaje-simple-json-logger/src/main/java/io/avaje/simplelogger/encoder/JsonEncoderBuilder.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class JsonEncoderBuilder {
2222
private String timestampPattern;
2323
private String component;
2424
private String environment;
25+
private String propertyNames;
2526
private boolean includeStackHash = true;
2627

2728
JsonEncoderBuilder json(JsonStream json) {
@@ -59,6 +60,11 @@ JsonEncoderBuilder environment(String environment) {
5960
return this;
6061
}
6162

63+
JsonEncoderBuilder propertyNames(String propertyNames) {
64+
this.propertyNames = propertyNames;
65+
return this;
66+
}
67+
6268
JsonEncoderBuilder includeStackHash(boolean includeStackHash) {
6369
this.includeStackHash = includeStackHash;
6470
return this;
@@ -96,8 +102,38 @@ JsonEncoder build() {
96102
if (stackHasher == null) {
97103
stackHasher = new StackHasher(StackElementFilter.builder().allFilters().build());
98104
}
105+
String[] mappedPropertyNames = toPropertyNames(propertyNames);
99106
final DateTimeFormatter formatter = TimeZoneUtils.jsonFormatter(timestampPattern, timeZone.toZoneId());
100-
return new JsonEncoder(json, component, environment, stackHasher, formatter, includeStackHash, customFieldsMap, throwableConverter);
107+
return new JsonEncoder(mappedPropertyNames, json, component, environment, stackHasher, formatter, includeStackHash, customFieldsMap, throwableConverter);
108+
}
109+
110+
static String[] toPropertyNames(String mapping) {
111+
String[] keys = {"component", "env", "timestamp", "level", "logger", "message", "thread", "stackhash", "stacktrace"};
112+
if (mapping == null || mapping.isEmpty()) {
113+
return keys;
114+
}
115+
return toPropertyNames(parseNameMapping(mapping), keys);
116+
}
117+
118+
private static Map<String,String> parseNameMapping(String mapping) {
119+
Map<String,String> map = new HashMap<>();
120+
String[] split = mapping.split("[,;]");
121+
for (String keyVal : split) {
122+
String[] nameVal = keyVal.trim().split("=");
123+
if (nameVal.length == 2) {
124+
map.put(nameVal[0].toLowerCase().trim(), nameVal[1].trim());
125+
}
126+
}
127+
return map;
128+
}
129+
130+
private static String[] toPropertyNames(Map<String,String> map, String[] baseNames) {
131+
String[] names = new String[baseNames.length];
132+
for (int i = 0; i < baseNames.length; i++) {
133+
String overrideName = map.get(baseNames[i]);
134+
names[i] = overrideName != null ? overrideName : baseNames[i];
135+
}
136+
return names;
101137
}
102138

103139
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"resources": [
33
{
4-
"pattern": "avaje-logger.*properties"
4+
"pattern": "avaje-logger*.properties"
55
}
66
]
77
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.avaje.simplelogger.encoder;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class JsonEncoderBuilderTest {
11+
12+
String[] baseKeys = {"component", "env", "timestamp", "level", "logger", "message", "thread", "stackhash", "stacktrace"};
13+
14+
@Test
15+
void toPropertyNames_when_null() {
16+
String[] names = JsonEncoderBuilder.toPropertyNames(null);
17+
assertThat(Arrays.equals(names, baseKeys)).isTrue();
18+
}
19+
20+
@Test
21+
void toPropertyNames_several() {
22+
String[] names = JsonEncoderBuilder.toPropertyNames("component = c; env=e,thread=_foo ");
23+
assertThat(names).isEqualTo(new String[]{"c", "e", "timestamp", "level", "logger", "message", "_foo", "stackhash", "stacktrace"});
24+
}
25+
26+
@Test
27+
void toPropertyNames_onlyOne() {
28+
String[] names = JsonEncoderBuilder.toPropertyNames("logger=loggerName");
29+
assertThat(names).isEqualTo(new String[]{"component", "env", "timestamp", "level", "loggerName", "message", "thread", "stackhash", "stacktrace"});
30+
}
31+
}

0 commit comments

Comments
 (0)