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

Multiple markers not handled correctly #35

Closed
danjo133 opened this issue Jul 11, 2019 · 4 comments
Closed

Multiple markers not handled correctly #35

danjo133 opened this issue Jul 11, 2019 · 4 comments
Labels

Comments

@danjo133
Copy link

Hi,

I'm having some problems with the gelf output, if I understand correctly, this line should loop over related markers and add them as properties like Mdc does.

additionalFields.put("marker", marker.getName());

With multiple markers I get a _marker="LS_APPEND_OBJECT" in my json output.

For example logstash logback adds the various markers as separate properties

Best regards,
Daniel

@osiegmar
Copy link
Owner

Indeed, multiple markers are not yet supported. Could you give an example of logstash handles multiple markers / properties?

@danjo133
Copy link
Author

When you add multiple markers with and, they get added as a reference, from
https://github.com/logstash/logstash-logback-encoder/blob/master/src/main/java/net/logstash/logback/marker/LogstashMarker.java

    /**
     * Adds the given marker as a reference, and returns this marker.
     * <p>
     * This can be used to chain markers together fluently on a log line. For example:
     * 
     * <pre>
     * {@code
     * import static net.logstash.logback.marker.Markers.*
     *     
     * logger.info(append("name1", "value1).and(append("name2", "value2")), "log message");
     * }
     * </pre>
     * 
     * @param <T> subtype of LogstashMarker
     * @param reference The marker to add
     * @return A marker with this marker and the given marker
     */
    @SuppressWarnings("unchecked")
    public <T extends LogstashMarker> T and(Marker reference) {
        add(reference);
        return (T) this;
    }

Then they can be added as a string as they are in that file

public String toString() {
     String self = toStringSelf();
     if (!hasReferences()) {
         return self;
     }

     StringBuilder sb = new StringBuilder(self);
     boolean appendSeparator = !self.isEmpty();
     for (Marker marker : this) {
         if (appendSeparator) {
             sb.append(", ");
         }
         String referenceToString = marker.toString();
         sb.append(referenceToString);
         appendSeparator = !referenceToString.isEmpty();
     }

     return sb.toString();
 }

or as json properties as in:

https://github.com/logstash/logstash-logback-encoder/blob/master/src/main/java/net/logstash/logback/marker/MapEntriesAppendingMarker.java

    @Override
    public void writeTo(JsonGenerator generator) throws IOException {
        if (map != null) {
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                generator.writeFieldName(String.valueOf(entry.getKey()));
                generator.writeObject(entry.getValue());
            }
        }
    }

Which is then added the same way as mdc properties.

@osiegmar
Copy link
Owner

osiegmar commented Dec 1, 2019

I've added support for multiple markers in the bugfix/multiple-markers branch. Could you please check and confirm, that this also fixes your issue? I'm not sure where your LS_APPEND_OBJECT comes from.

osiegmar added a commit that referenced this issue Dec 14, 2019
@osiegmar
Copy link
Owner

Fix released in 2.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants