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

LogstashMarker refactoring #636

Merged
merged 10 commits into from
Sep 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@
*/
public class DeferredStructuredArgument implements StructuredArgument {

/**
* Supplier for the deferred {@link StructuredArgument}
*/
brenuart marked this conversation as resolved.
Show resolved Hide resolved
private final Supplier<? extends StructuredArgument> structureArgumentSupplier;

/**
* Cached value of the structured argument returned by {@link #structureArgumentSupplier}.
* {@code null} until {@link #getSuppliedValue()} is first called.
*/
private volatile StructuredArgument suppliedValue;

public DeferredStructuredArgument(Supplier<? extends StructuredArgument> structureArgumentSupplier) {
Expand All @@ -63,6 +70,12 @@ public void writeTo(JsonGenerator generator) throws IOException {
getSuppliedValue().writeTo(generator);
}

/**
* Get the deferred structure argument from the supplier or return it from the cache
* if already initialized.
*
* @return the deferred structured argument
*/
private StructuredArgument getSuppliedValue() {
if (suppliedValue == null) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@
* the supplier will be invoked when the first appender encodes the marker.
* That same supplied value will be used when the next appender encodes the marker.</p>
*/
@SuppressWarnings("serial")
public class DeferredLogstashMarker extends LogstashMarker {

public static final String DEFERRED_MARKER_NAME = "DEFERRED";

private final Supplier<? extends LogstashMarker> logstashMarkerSupplier;
/**
* Supplier for the deferred marker
*/
private final Supplier<? extends LogstashMarker> logstashMarkerSupplier;

/**
* Cached value of the marker returned by {@link #logstashMarkerSupplier}.
* {@code null} until {@link #getSuppliedValue()} is first called.
*/
private volatile LogstashMarker suppliedValue;

public DeferredLogstashMarker(Supplier<? extends LogstashMarker> logstashMarkerSupplier) {
Expand All @@ -57,6 +65,12 @@ public void writeTo(JsonGenerator generator) throws IOException {
writeMarker(generator, getSuppliedValue());
}

/**
* Get the deferred marker from the supplier or return it from the cache
* if already initialized.
*
* @return the deferred marker
*/
private LogstashMarker getSuppliedValue() {
if (suppliedValue == null) {
synchronized (this) {
Expand All @@ -82,8 +96,7 @@ private void writeMarker(JsonGenerator generator, Marker marker) throws IOExcept

if (marker.hasReferences()) {
for (Iterator<Marker> i = marker.iterator(); i.hasNext();) {
Marker next = i.next();
writeMarker(generator, next);
writeMarker(generator, i.next());
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/logstash/logback/marker/EmptyLogstashMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* }
* </pre>
*/
@SuppressWarnings("serial")
public class EmptyLogstashMarker extends LogstashMarker implements StructuredArgument {

public static final String EMPTY_MARKER_NAME = "EMPTY";
Expand All @@ -53,4 +54,14 @@ public void writeTo(JsonGenerator generator) throws IOException {
protected String toStringSelf() {
return "";
}

@Override
public boolean equals(Object obj) {
return obj instanceof EmptyLogstashMarker && super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
115 changes: 35 additions & 80 deletions src/main/java/net/logstash/logback/marker/LogstashBasicMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,29 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Marker;

/* Copy of {@link org.slf4j.helpers.BasicMarker} from slf4j-api v1.7.31, 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:
*
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* A simple implementation of the {@link Marker} interface.
*
* @author Ceki G&uuml;lc&uuml;
* @author Joern Huxhorn
*/
* A simple implementation of the SLF4J {@link Marker} interface.
*/
@SuppressWarnings("serial")
public class LogstashBasicMarker implements Marker {
class LogstashBasicMarker implements Marker {

private static final String OPEN = "[ ";
private static final String CLOSE = " ]";
private static final String SEP = ", ";

/**
* The marker name
*/
private final String name;
private final List<Marker> referenceList = new CopyOnWriteArrayList<>();

/*
* BEGIN Modification in logstash-logback-encoder to make this constructor public
*/
public LogstashBasicMarker(String name) {
/*
* END Modification in logstash-logback-encoder to make this constructor public
/**
* Referenced markers - initialized the first time a marker is added
*/
if (name == null) {
throw new IllegalArgumentException("A marker name cannot be null");
}
this.name = name;
private volatile List<Marker> referenceList;

LogstashBasicMarker(String name) {
this.name = Objects.requireNonNull(name);
}

/**
Expand All @@ -95,9 +56,7 @@ public String getName() {
*/
@Override
public void add(Marker reference) {
if (reference == null) {
throw new IllegalArgumentException("A null value cannot be added to a Marker as reference.");
}
Objects.requireNonNull(reference);

// no point in adding the reference multiple times
if (this.contains(reference)) {
Expand All @@ -107,6 +66,13 @@ public void add(Marker reference) {
return;
}

if (referenceList == null) {
synchronized (this) {
if (referenceList == null) {
referenceList = new CopyOnWriteArrayList<>();
}
}
}
referenceList.add(reference);
}

Expand All @@ -115,7 +81,7 @@ public void add(Marker reference) {
*/
@Override
public boolean hasReferences() {
return !referenceList.isEmpty();
return referenceList != null && !referenceList.isEmpty();
}

/**
Expand All @@ -132,36 +98,30 @@ public boolean hasChildren() {
*/
@Override
public Iterator<Marker> iterator() {
return referenceList.iterator();
if (referenceList == null) {
return Collections.emptyIterator();
} else {
return referenceList.iterator();
}
}

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

/**
* {@inheritDoc}
*/
@Override
public boolean remove(Marker referenceToRemove) {
return referenceList.remove(referenceToRemove);
if (referenceList != null) {
return referenceList.remove(referenceToRemove);
} else {
return false;
}
}

/**
* {@inheritDoc}
*/
@Override
public boolean contains(Marker other) {
if (other == null) {
throw new IllegalArgumentException("Other cannot be null");
}

if (this.equals(other)) {
return true;
}
Expand All @@ -182,10 +142,6 @@ public boolean contains(Marker other) {
*/
@Override
public boolean contains(String name) {
if (name == null) {
throw new IllegalArgumentException("Other cannot be null");
}

if (this.name.equals(name)) {
return true;
}
Expand Down Expand Up @@ -234,16 +190,15 @@ public String toString() {
return this.getName();
}
StringBuilder sb = new StringBuilder(this.getName())
.append(' ')
.append(OPEN);
.append(" [ ");
Iterator<Marker> it = this.iterator();
while (it.hasNext()) {
sb.append(it.next().getName());
if (it.hasNext()) {
sb.append(SEP);
sb.append(", ");
}
}
sb.append(CLOSE);
sb.append(" ]");

return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ public class ObjectAppendingMarker extends SingleFieldAppendingMarker {
* The object to write as the field's value.
* Can be a {@link String}, {@link Number}, array, or some other object that can be processed by an {@link ObjectMapper}
*/
private final Object object;
private final Object fieldValue;

public ObjectAppendingMarker(String fieldName, Object object) {
public ObjectAppendingMarker(String fieldName, Object fieldValue) {
super(MARKER_NAME, fieldName);
this.object = object;
this.fieldValue = fieldValue;
}

public ObjectAppendingMarker(String fieldName, Object object, String messageFormatPattern) {
public ObjectAppendingMarker(String fieldName, Object fieldValue, String messageFormatPattern) {
super(MARKER_NAME, fieldName, messageFormatPattern);
this.object = object;
this.fieldValue = fieldValue;
}

@Override
protected void writeFieldValue(JsonGenerator generator) throws IOException {
generator.writeObject(object);
generator.writeObject(fieldValue);
}

@Override
public Object getFieldValue() {
return StructuredArguments.toString(object);
return StructuredArguments.toString(fieldValue);
}

@Override
Expand All @@ -111,15 +111,15 @@ public boolean equals(Object obj) {
}

ObjectAppendingMarker other = (ObjectAppendingMarker) obj;
return Objects.equals(this.object, other.object);
return Objects.equals(this.fieldValue, other.fieldValue);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + super.hashCode();
result = prime * result + (this.object == null ? 0 : this.object.hashCode());
result = prime * result + (this.fieldValue == null ? 0 : this.fieldValue.hashCode());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public class ObjectFieldsAppendingMarker extends LogstashMarker implements Struc
*
* Since apps will typically serialize the same types of objects repeatedly, they shouldn't grow too much.
*/
private static final ConcurrentHashMap<Class<?>, JsonSerializer<Object>> beanSerializers = new ConcurrentHashMap<Class<?>, JsonSerializer<Object>>();
private static final ConcurrentHashMap<ObjectMapper, SerializerProvider> serializerProviders = new ConcurrentHashMap<ObjectMapper, SerializerProvider>();
private static final ConcurrentHashMap<Class<?>, JsonSerializer<Object>> beanSerializers = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<ObjectMapper, SerializerProvider> serializerProviders = new ConcurrentHashMap<>();

public ObjectFieldsAppendingMarker(Object object) {
super(MARKER_NAME);
Expand Down
Loading