Details
-
Bug
-
Resolution: Unresolved
-
1.5.x
-
None
-
Operating System: Windows XP
Platform: PC
-
major
-
P1
-
177
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)