Skip to content
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

Fix marker aggregation when multiple empty markers are aggregated. #529

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/main/java/net/logstash/logback/marker/LogstashBasicMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.util.List;
import java.util.Vector;

/* Copy of {@link org.slf4j.helpers.BasicMarker} from slf4j-api v1.7.12,
* with a minor change to make the constructor public,
* so that it can be extended in other packages.
* <p>
/* Copy of {@link org.slf4j.helpers.BasicMarker} from slf4j-api v1.7.12, with minor changes:
* 1. make the constructor public so that it can be extended in other packages
* 2. add getReferences() method

* slf4j-api, {@link org.slf4j.helpers.BasicMarker}, and the portions
* of this class that have been copied from BasicMarker are provided under
* the MIT License copied here:
Expand Down Expand Up @@ -119,6 +119,18 @@ public synchronized Iterator<Marker> iterator() {
}
}

/*
* BEGIN Modification in logstash-logback-encoder to add this method
*/
protected List<Marker> getReferences() {
return refereceList == null
? Collections.emptyList()
: Collections.unmodifiableList(refereceList);
}
/*
* END Modification in logstash-logback-encoder to add this method
*/

public synchronized boolean remove(Marker referenceToRemove) {
if (refereceList == null) {
return false;
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/net/logstash/logback/marker/LogstashMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package net.logstash.logback.marker;

import java.io.IOException;
import java.util.Objects;

import org.slf4j.Marker;

Expand Down Expand Up @@ -76,6 +77,42 @@ public <T extends LogstashMarker> T with(Marker reference) {
*/
public abstract void writeTo(JsonGenerator generator) throws IOException;

@Override
public synchronized void add(Marker reference) {
if (reference instanceof EmptyLogstashMarker) {
for (Marker m : (EmptyLogstashMarker) reference) {
add(m);
}
} else {
super.add(reference);
}
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + super.hashCode();
result = prime * result + this.getReferences().hashCode();
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof LogstashMarker)) {
return false;
}

LogstashMarker other = (LogstashMarker) obj;
return Objects.equals(this.getReferences(), other.getReferences());
}

/**
* Returns a String in the form of
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public String toStringSelf() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public Object getFieldValue() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ private SerializerProvider getSerializerProvider(ObjectMapper mapper) {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public Object getFieldValue() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public String toStringSelf() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/logstash/logback/marker/MarkersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public void aggregate() {
assertThat(aggregate.contains(marker2)).isTrue();
}

@Test
public void aggregate_multi() {
LogstashMarker marker1 = Markers.append("fieldName1", "fieldValue1");
LogstashMarker marker2 = Markers.append("fieldName2", "fieldValue2");
LogstashMarker aggregate = Markers.aggregate(marker1, marker2);

LogstashMarker marker3 = Markers.append("fieldName3", "fieldValue3");
aggregate = Markers.aggregate(aggregate, marker3);

assertThat(aggregate).isInstanceOf(EmptyLogstashMarker.class);
assertThat(aggregate.contains(marker1)).isTrue();
assertThat(aggregate.contains(marker2)).isTrue();
assertThat(aggregate.contains(marker3)).isTrue();
}

@Test
public void testToString() {

Expand Down