[slf4j-user] basic MDC support for java.util.logging
Maarten Bosteels
mbosteels.dns at gmail.com
Thu Oct 18 12:05:28 CEST 2007
Hello Ceki,
I started coding a BasicMDCAdapter that could be used by java.util.logging
Had a look at the log4j MDC implementation and also at LogbackMDCAdapter
code.
Currently my implementation is roughly the same as the LogbackMDCAdapter
without the generics (since SLF4J should run on 1.4 right ?)
I didn't quite understand this remark in the code:
* Each time a value is added, a new instance of the map is created. This
is
* to be certain that the serialization process will operate on the
updated map
* and not send a reference to the old map, thus not allowing the remote
logback
* component to see the latest changes.
Can you describe a scenario where serialization would fail if one just add a
value to the map ?
Is it a threading issue ?
I also created an Log4jXmlFormatter which extends
java.util.logging.Formatter
and generates the same xml as org.apache.log4j.xml.XMLLayout. including the
MDC values.
Don't know where to post this, creating a project for just two or three
class seems overkill.
What about adding it to slf4j-jdk14 ?
It would be cool if logback would also have an Log4jXmlLayout.
I think the xml layout has several advantages over serialization:
* it's more compact (much to me surprise) certainly when combined with a
GZipOutputStream
* human readable
* easier to implement in other languages (eg. log4cxx)
* no need to depend on log4j's LoggingEvent
* protected against changes to the serialized form of log4j's LoggingEvent
for example, Chainsaw currently only displays the MDC when the sender is
using log4j 1.3
It would be trivial to write, I am willing to implement it, if there's a
chance you will add it to logback.
regards,
Maarten
**On 10/10/07, Ceki Gulcu <listid at qos.ch> wrote:
>
> Hi Maarteen,
>
> Comments inline.
>
> Maarten Bosteels wrote:
>
> [snip]
>
> > But unfortunately, not all SLF4J implementations (or adapters) support
> > the MDC mechanism.
>
> Indeed.
>
> > Would it be acceptable to add basic MDC support to JDK14LoggerAdapter ?
>
> Yes, it would.
>
> > The MDC would store its key/value pairs in a ThreadLocal and
> JDK14LoggerAdapter
> > could prepend these key/values to the msg (the same could be done for
> > SimpleLogger).
>
> If we did that, JDK14LoggerAdapter would be at least partially assuming
> the
> responsibility for formatting the log message which seems kind of wrong to
> me.
>
> > Another option would be to make subclasses of
> java.util.logging.XMLFormatter
> > and java.util.logging.SimpleFormatter that are MDC aware.
>
> Yah, that's better.
>
> > (IMHO these classes don't have to be part of SLF4J)
>
> Right.
>
> > Then JDK14LoggerAdapter wouldn't have to change.
> > It would be sufficient to change
> >
> http://svn.slf4j.org/viewvc/slf4j/trunk/slf4j-jdk14/src/main/java/org/slf4j/impl/StaticMDCBinder.java
>
> Nice.
>
> > I am willing to create a patch, if there's a chance it would be accepted
> :-)
>
> +1
>
> > Note that I have almost no experience with java.util.logging.
> > I am just trying to find a solution for libraries/frameworks that want
> > to use org.slf4j.MDC
> > without leaving users of java.util.logging in the cold.
>
> Sounds like a plan.
>
> > Thanks,
> > Maarten
>
>
> --
> Ceki Gülcü
> Logback: The reliable, generic, fast and flexible logging framework for
> Java.
> http://logback.qos.ch
> _______________________________________________
> user mailing list
> user at slf4j.org
> http://www.slf4j.org/mailman/listinfo/user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/slf4j-user/attachments/20071018/8b3f591b/attachment.htm>
More information about the slf4j-user
mailing list