Giter Site home page Giter Site logo

Comments (8)

rfoltyns avatar rfoltyns commented on May 23, 2024 1

I'll release it in 1.4.3. Enjoy! :)

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

No, it's not possible. But, if you're not using Markers yet, you can achieve that with mixins:

One for LogEvent to stop ignoring Marker field:

@JsonSerialize(as = LogEvent.class)
public abstract class CustomLogEventMixIn extends LogEventJacksonJsonMixIn {

    @JsonUnwrapped
    @JsonProperty(value = "event.category")
    @JsonIgnore(false)
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @Override
    public abstract Marker getMarker();

}

Second one for Marker (package-private):

package org.apache.logging.log4j.core.jackson;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.logging.log4j.Marker;

public abstract class CustomMarkerMixIn extends MarkerMixIn {

    CustomMarkerMixIn() {
        super(null);
    }

    @JsonValue
    @Override
    public abstract String getName();

    @JsonIgnore
    @Override
    public abstract Marker[] getParents();

}

JacksonJsonLayout config:

    <JacksonMixIn mixInClass="foo.bar.CustomLogEventMixIn"
                             targetClass="org.apache.logging.log4j.core.LogEvent" />
    <JacksonMixIn mixInClass="org.apache.logging.log4j.core.jackson.CustomMarkerMixIn"
                             targetClass="org.apache.logging.log4j.Marker" />

and then simply:

Marker category = MarkerManager.getMarker("custom category");
logger.info(category, "Hello, World!");

Do you think it could be an acceptable solution? It will for for event.category. Other fields can also be extracted from LogEvent to conform to ECS.

Context data can also be serialized as described here

from log4j2-elasticsearch.

dysan54 avatar dysan54 commented on May 23, 2024

Yes, it could do the trick for event.category but not for the others.

I would imagined an additional attribute in virtual properties that can control generation of json output, maybe like this in VirtualPropertiesWriter :

  @Override
  public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
    if (virtualProperties.length > 0) {
      for (int i = 0; i < virtualProperties.length; i++) {
        String val = valueResolver.resolve(virtualProperties[i]);
        if (!val.isEmpty()) {
          gen.writeFieldName(virtualProperties[i].getName());
          gen.writeString(val);
        }
      }
    }
  }

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

Yeah, it will work, but I need to think it through. Initially I didn't want to pack too many flags there.

Ideally, value based inclusion should be done with Jackson's Include, but it's not applicable to this field.

Maybe I'll just add a configurable filters as an alternative so you can choose what you'd like to do (including writing your own ones). What do you think?

String val = valueResolver.resolve(virtualProperties[i]);
if (!filter.test(val)) {
    return;
}
gen.writeFieldName(virtualProperties[i].getName());
gen.writeString(val);

from log4j2-elasticsearch.

dysan54 avatar dysan54 commented on May 23, 2024

Yes it would work because in my case i don't have to apply different rules between virtual properties, just a general rule to filter blank values.
Choosing virtual properties to which apply this filter would be a plus.

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

Ok, I'll add the configurable filters and NonBlankFilter it in 1.4.3.

Choosing properties might be useful, but it should be possible to achieve with custom filters and default values. It might look a bit awkward, but actually allows maximum functionality with minimum amount of changes to VirtualProperty and the writer.

<JacksonJsonLayout>
    <VirtualProperty name="field1" value="$${ctx:val1:-someDefaultValue}"/><!-- included, default behaviour -->
    <VirtualProperty name="field2" value="$${ctx:val2:-}"/><!-- excluded by NonBlankFilter -->
    <VirtualProperty name="field3" value="$${ctx:val3:-filter2}"/><!-- excluded by UserProvidedFilter2 -->
    <VirtualProperty name="field4" value="$${ctx:val4:-filter1}"/><!-- excluded by UserProvidedFilter1 -->
    <VirtualProperty name="field5" value="$${ctx:val5:-someDefaultValue}"/><!-- excluded by UserProvidedFieldFilter -->
    <VirtualProperty name="field6" value="$${ctx:val6:-someOtherValue}"/><!-- included, default behaviour -->
    <UserProvidedFilter1/>
    <UserProvidedFilter2/>
    <NonBlankFilter/><!-- available out-of-the-box -->
    <UserProvidedFieldFilter fields="field5,field6" condition="eq someDefaultValue" />
    
</JacksonJsonLayout>

from log4j2-elasticsearch.

dysan54 avatar dysan54 commented on May 23, 2024

It's perfect, many thanks !

from log4j2-elasticsearch.

rfoltyns avatar rfoltyns commented on May 23, 2024

Released in 1.4.3.

from log4j2-elasticsearch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.