Skip to content

Commit

Permalink
Make AbstractJsonPatternParser throw an exception for invalid pattern…
Browse files Browse the repository at this point in the history
… instead of logging an ERROR status (#691)

AbstractJsonPatternParser now throws a JsonPatternException in case of invalid pattern. It is up to the AbstractPatternLayoutJsonProvider to catch the exception, log an error status and decide how it should behave.

Invalid PatternLayout formats are now detected and throw a JsonPatternException as well. Detection is done by inspecting the parsed output returned by the PatternLayout when it is started, looking for the special %PARSER_ERROR marker inserted when it encounters an error.

The previous implementation used to ignore fields with an invalid pattern layout while keeping the other. This results in a partial JSON output with only the valid fields. This behaviour is now changed and the provider reverts to producing nothing in case of bad configuration.

These changes address issue #686.
  • Loading branch information
brenuart authored Nov 8, 2021
1 parent 5b6cd61 commit 145310d
Show file tree
Hide file tree
Showing 15 changed files with 648 additions and 597 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,9 @@ LOGGER.info("{\"type\":\"example\",\"msg\":\"example of json message with type\"
Note that the value that is sent for `line_long` is a number even though in your pattern it is a quoted text.
And the json_message field value is a json object, not a string.

You can escape an operation by prefixing it with `\` if you don't want it to be interpreted.


#### Omitting fields with empty values

The pattern provider can be configured to omit fields with the following _empty_ values:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;

import net.logstash.logback.pattern.AbstractJsonPatternParser;
import net.logstash.logback.pattern.AbstractJsonPatternParser.JsonPatternException;
import net.logstash.logback.pattern.NodeWriter;

import ch.qos.logback.access.spi.IAccessEvent;
Expand Down Expand Up @@ -79,19 +80,28 @@ public void start() {
if (jsonFactory == null) {
throw new IllegalStateException("JsonFactory has not been set");
}
initializeNodeWriter();

try {
this.nodeWriter = initializeNodeWriter();
} catch (JsonPatternException e) {
this.nodeWriter = null;
addError("Invalid [pattern]: " + e.getMessage(), e);
}

super.start();
}


/**
* Parses the pattern into a {@link NodeWriter}.
*
* @return a {@link NodeWriter}
* @throws JsonPatternException thrown in case of invalid pattern
*/
private void initializeNodeWriter() {
private NodeWriter<Event> initializeNodeWriter() throws JsonPatternException {
AbstractJsonPatternParser<Event> parser = createParser(this.jsonFactory);
parser.setOmitEmptyFields(omitEmptyFields);
this.nodeWriter = parser.parse(pattern);
return parser.parse(pattern);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AccessEventPatternJsonProvider extends AbstractPatternJsonProvider<

@Override
protected AbstractJsonPatternParser<IAccessEvent> createParser(JsonFactory jsonFactory) {
return new AccessEventJsonPatternParser(this, jsonFactory);
return new AccessEventJsonPatternParser(getContext(), jsonFactory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LoggingEventPatternJsonProvider extends AbstractPatternJsonProvider

@Override
protected AbstractJsonPatternParser<ILoggingEvent> createParser(JsonFactory jsonFactory) {
return new LoggingEventJsonPatternParser(this, jsonFactory);
return new LoggingEventJsonPatternParser(getContext(), jsonFactory);
}

}
Loading

0 comments on commit 145310d

Please sign in to comment.