[slf4j-dev] [PATCH] allow passing of objects through jcl-over-slf4j

Robert Varga varga at pantheon.sk
Mon Sep 29 23:36:12 CEST 2008


Hi,

we are considering migration of a major project to SLF4J -- but cannot 
do so due to what seems to be a limitation of jcl-over-slf4j.

Our project uses a combination of JCL and log4j: we use JCL in the code, 
and wire the messages via log4j to a custom appender. The users wrap 
messages in a container object along with a bit of metadata, which the 
appender then picks up and uses the metadata to act on the message. The 
entire scenario looks roughly like:

public class Metadata {
     public Metadata(...) {
         ...
     }
}

public class Message {
     final Metadata metadata;
     final String message;

     public Message(final Metadata metadata, final String message) {
         this.metadata = metadata;
         this.message = message;
     }

     Metadata getMetadata() {
         return metadata;
     }

     String toString() {
         return message;
     }
}

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class User {
     protected final Log log = LogFactory.getLog(getClass());
     protected static final Metadata SOME_METADATA = new Metadata(...);

     void someMethod() {
         log.debug(new Message(SOME_METADATA, "Some message"));
     }
}

import org.apache.log4j.AppenderSkeleton;
public class Appender implements AppenderSkeleton {
     public void append(final LoggingEvent event) {
         final Object msg = event.getMessage();
         if (msg instanceof Message) {
             final Metadata = ((Message)msg).getMetadata();
             ...
         } else {
             ...
         }
     }
}

Using jcl-over-slf4j breaks this by unconditionally converting passed 
Message to a String -- thus losing the metadata. The attached patch 
improves the situation by checking if the message object passed to 
SLF4JLog implements Marker interface and if does, it passes is down to 
the Logger.

This will allow us to change the Message/Metadata classes to implement 
Marker interface and switch the appender to Logger interface -- 
providing a smooth transition to SLF4J.

Please consider for inclusion.

Bye,
Robert
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: slf4j-pass-marker.patch
URL: <http://qos.ch/pipermail/slf4j-dev/attachments/20080929/93959c4e/attachment.diff>


More information about the slf4j-dev mailing list