[slf4j-dev] Benefits of Message objects?

Ceki Gülcü ceki at qos.ch
Sat Sep 10 21:50:28 CEST 2011


On 10/09/2011 5:19 PM, Ralph Goers wrote:
>
> 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.

Invocation of FormattingTuple.format in LoggingEvent is there to keep 
the code simple. Eager formatting could be done lazily as long as the 
data remains unchanged and/or is non-mutable.

 > 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.
> }

Yes, indeed. Good point.

 > 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.

It would be trivial to correct EventLogger wouldn't it?

-- 
QOS.ch, main sponsor of cal10n, logback and slf4j open source projects, 
is looking to hire talented software developers. For further details, 
see http://logback.qos.ch/job.html


More information about the slf4j-dev mailing list