[slf4j-dev] [JIRA] (SLF4J-168) Feature-Request: allow Object as value in MDC.put

QOS.CH (JIRA) noreply-jira at qos.ch
Tue Dec 24 19:08:00 CET 2019


    [ https://jira.qos.ch/browse/SLF4J-168?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=20050#comment-20050 ] 

Jon Wolski commented on SLF4J-168:
----------------------------------

To build on what @nithril said: I have a use case where I'm logging everything JSON-serialized and ingesting that into an ELK stack (technically "EFK", Filebeat instead of Logstash). I want to log certain attributes as (JSON) Number so that I can do numeric operations on that data in Kibana dashboards.

Ironically, the data still needs to be converted to String (because I'm writing JSON, i.e. character data, not something like Avro or Protobuf), but not a _JSON_ String. i.e. I need my 4 byte integer serialized as a string of decimal characters, but I do not need the double quotes surrounding it.

In this case, not only do I wish to `MDC.put` non-String, I'd really like to `put` a primitive. I'll settle for a boxed primitive, though, if I must.

> Feature-Request: allow Object as value in MDC.put
> -------------------------------------------------
>
>                 Key: SLF4J-168
>                 URL: https://jira.qos.ch/browse/SLF4J-168
>             Project: SLF4J
>          Issue Type: Bug
>          Components: Core API
>    Affects Versions: 1.5.x
>         Environment: Operating System: Windows XP
> Platform: PC
>            Reporter: Michael Wenig
>            Assignee: SLF4J developers list
>
> Currently Log4j accepts in MDC.put java.lang.Object as value while slf4j only accepts strings.
> If you have some Information which is only needed rarely and expensive to construct you are either forced to do a toString() for each request or you have to fall back to the native use of log4j.
> For example we have an Object which wraps a httpServletRequest and will be inserted in the MDC via a ServletFilter in order to be used in ERROR-Level (configured via layout of the according appender). This objects constructs a complete request dump via toString() which is expensive as for example the values of some parameters gets masked (e.g. passwords) for privacy. 
> Using log4j this object was put in the MDC as object and toString() was only called if the information was really needed in a log statement (which is only the case when an unexpected error occurs and will normally not occur).
> When we switched to slf4j we realized that we have to either call toString() explicitely in the filter (which would result in an expensive calculation on each request) or have to fall back to log4j for this case which reduces compatibility.
> I would suggest to explicitly allow MDC.put(String, Object) and delegate it directly to the underlying implementation if it is supported. If it is not supported the adapter could call the toString() method accepting the overhead. (This should be documented of course!)
> Are there any reasons not to support this? It is a major issue for us as we are currently not able to switch the logging mechanism (and the functionality is part of a library)



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)


More information about the slf4j-dev mailing list