[slf4j-dev] Benefits of Message objects?

Ralph Goers rgoers at apache.org
Sat Sep 10 17:19:33 CEST 2011


On Sep 10, 2011, at 6:50 AM, Ceki Gulcu wrote:

> 
> 
> If the Message interface were supported in SLF4J, we would write:
> 
>  logger.info(new FlightRecordWrapper(fr));
> 
> This is cleaner than
> 
>  logger.info("{}", new FlightRecordWrapper(fr));
> 
> I am still left with the feeling that the main inconvenience of the "{}" form is its ugliness.
> 

Not just its ugliness. Logback always formats the message when creating the event which will convert FlightRecordWrapper.   I can imagine a use case where I filter FlightRecordMessages to a particular Appender and then serialize them using Avro, Hession or something similar so generating the formatted message is unnecessary.  It is also much easier to write Appenders or Layouts that simply do 


if (message instanceOf MyMessage) {
  MyMessage myMsg = (MyMessage) message;
  //  do stuff to my message
} else {
  // do something else
}

rather than 

MyMessage myMsg = null;
for (Object obj : event.getArgumentArray()) {
  If (obj instanceOf MyMessage) {
    myMsg = (MyMessage) obj;
    break;
  }
}

if (myMsg != null) {
  // Do stuff to my message
} else {
  // do something else.
}

I should also note that at some point LocationAwareLogger wasn't even passing the argument array so EventLogger had to convert the EventData to an XML String. When you added the argument array to LocationAwareLogger you modified EventLogger to pass a null, not the EventData object. So using EventLogger is very expensive. 


Ralph



More information about the slf4j-dev mailing list