Uploaded image for project: 'SLF4J'
  1. SLF4J
  2. SLF4J-168

Feature-Request: allow Object as value in MDC.put

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • 2.0.14
    • 1.5.x
    • Core API
    • None
    • Operating System: Windows XP
      Platform: PC

    Description

      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)

      Attachments

        Activity

          People

            slf4j-dev SLF4J developers list
            issues@wennysoft.de Michael Wenig
            Votes:
            5 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated: