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

Query StructuredArgument/Marker using JSONPath #933

Open
wsargent opened this issue Feb 7, 2023 · 0 comments
Open

Query StructuredArgument/Marker using JSONPath #933

wsargent opened this issue Feb 7, 2023 · 0 comments

Comments

@wsargent
Copy link
Contributor

wsargent commented Feb 7, 2023

Is your feature request related to a problem? Please describe.

Currently there is not a great way to query for specific event specific fields from an encoder. It's also tough to extract data from structured arguments/markers from a LoggingEvent because getFieldName/getFieldValue is protected and the only reliable way to pull out the field name and value is to fake out a JSONGenerator:

FakeJsonGenerator gen = new FakeJsonGenerator();
structuredArgument.writeTo(gen);
String name = gen.getFieldName();
Object value = gen.getFieldValue();

There are valid use cases for querying event specific fields, such as:

  • Use in evaluator, which would allow filtering on the presence/absence of event specific fields
  • Presentation of specific event specific field data in encoders, i.e. rendering "userId" if it exists
  • Special treatment of event specific fields i.e. highlighting on specific name/value pairs
  • Transformation of complex structured data, i.e. rendering a specific field from an argument containing a complex ObjectNode.

Describe the solution you'd like

Say that you have a structured logging statement:

logger.info(append("userId", userId), "foo bar", keyValue("title", name));

It would be nice to be able to query the structured argument using JSONPath through a custom converter:

<conversionRule conversionWord="argpath" converterClass="StructuredArgumentConverter" />
<conversionRule conversionWord="markerpath" converterClass="MarkerConverter" />

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%date{H:mm:ss.SSS} %logger: %argpath{$.title} %markerpath{$.userId} %message%n%ex</pattern>
    </encoder>
</appender>

Extraction could be provided through JSONPath API on the backend, for example rendering a store:

logger.info("store", keyValue("store", storeJsonNode));

Inside an evaluator you could get the title specifically using:

public Optional<String> getTitle(ILoggingEvent event) {
  return StructuredArguments.findString(event, "$.store.book[0].title");
} 

Using JSONPath would also be helpful in determining masking, for example masking by index.

Additional context

I've implemented a JSONPath API here in Echopraxia, but it's only really useful before logging. I can add conditions and query fields up to the point where it's time to write to the SLF4J Logger, but after that it's harder to get it from the logging event.

@wsargent wsargent changed the title Converters using JSONPath to query StructuredArgument/Marker Query StructuredArgument/Marker using JSONPath Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants