-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setting to exclude MDC keys from being sent to Elastic #57
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
package com.internetitem.logback.elasticsearch; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.Context; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.internetitem.logback.elasticsearch.config.ElasticsearchProperties; | ||
import com.internetitem.logback.elasticsearch.config.HttpRequestHeaders; | ||
import com.internetitem.logback.elasticsearch.config.Property; | ||
import com.internetitem.logback.elasticsearch.config.Settings; | ||
import com.internetitem.logback.elasticsearch.util.AbstractPropertyAndEncoder; | ||
import com.internetitem.logback.elasticsearch.util.ClassicPropertyAndEncoder; | ||
import com.internetitem.logback.elasticsearch.util.ErrorReporter; | ||
import com.internetitem.logback.elasticsearch.config.*; | ||
import com.internetitem.logback.elasticsearch.util.*; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
|
||
public class ClassicElasticsearchPublisher extends AbstractElasticsearchPublisher<ILoggingEvent> { | ||
|
||
|
@@ -40,10 +36,29 @@ protected void serializeCommonFields(JsonGenerator gen, ILoggingEvent event) thr | |
gen.writeObjectField("message", formattedMessage); | ||
} | ||
|
||
if(settings.isIncludeMdc()) { | ||
if (settings.isIncludeMdc()) { | ||
List<String> excludedKeys = getExcludedMdcKeys(); | ||
for (Map.Entry<String, String> entry : event.getMDCPropertyMap().entrySet()) { | ||
gen.writeObjectField(entry.getKey(), entry.getValue()); | ||
if (!excludedKeys.contains(entry.getKey())) { | ||
gen.writeObjectField(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private List<String> getExcludedMdcKeys() { | ||
/* | ||
* using a List instead of a Map because the assumption is that | ||
* the number of excluded keys will be very small and not cause | ||
* a performance issue | ||
*/ | ||
List<String> result = new ArrayList<>(); | ||
if (settings.getExcludedMdcKeys() != null) { | ||
String[] parts = settings.getExcludedMdcKeys().split(","); | ||
for (String part : parts) { | ||
result.add(part.trim()); | ||
} | ||
} | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this list is re-allocated and populated anew every time a log entry is written? Why is this not parsed once when reading the config and then re-used on subsequent accesses? Looks like this could incur a performance overhead. List instead of a map or set is probably fine, because this will likely only matter when there are hundreds of items in the collection and calculating the hash code of each tested key can be avoided. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New to the project, but I don't think star-imports are used anywhere else (and haven't been used before in this file).