logback-gelf icon indicating copy to clipboard operation
logback-gelf copied to clipboard

Add another method for adding static field to GelfEncoder

Open vavrtom opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. If I have two objects containing key (String) and value (String/Object) and I want to add them as static field to GelfEncoder I have to create new string in key:value format and pass it to method addStaticField(String staticField), where key and value are parsed from the staticField string.

Describe the solution you'd like Method addStaticField(String key, String value) or addStaticField(String key, Object value) could be introduced in GelfEncoder, then key and value can be directly passed to method without creating/parsing of the string.

Describe alternatives you've considered Create a string in key:value format and pass the string to method addStaticField(String staticField).

vavrtom avatar Feb 08 '22 15:02 vavrtom

I don't exactly understand your use case. If you're using the API (not the XML configuration) you could easily use a custom field mapper for this. See https://github.com/osiegmar/logback-gelf/issues/78 for example. Doing so programmatically is even simpler:

GelfEncoder e = new GelfEncoder();
e.addFieldMapper((event, valueHandler) -> valueHandler.accept("MY_KEY", "MY_VALUE"));

The static fields are a very basic (and limited) concept mainly for XML based configuration.

osiegmar avatar Feb 08 '22 18:02 osiegmar

Yes, I am using the API. In versions <=3.0.0 I was using the setStaticFields(Map<String, Object> staticFields) method in GelfEncoder initialization.

I did not want to use field mapper, because then all static fields keys are validated in the addField() method for each message, which seems redundant to me (keys will be the same). When addStaticField(String staticField) method is used, key validation is performed only during adding static field.

I just thought it could be useful for someone who was using setStaticFields() method in <=3.0.0 versions (reintroducing setStaticFields() could be another option).

If you think, it is not worth it, then issue can be closed. I am perfectly fine with the existing methods for setting static fields.

vavrtom avatar Feb 09 '22 11:02 vavrtom