logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: Garbage-free Log4j docs preview
Date Fri, 03 Jun 2016 15:50:35 GMT

On Friday, June 3, 2016 at 4:42:51 PM UTC+9, Philippe Marschall wrote:
> On Tuesday, May 17, 2016 at 7:13:09 PM UTC+2, Remko Popma wrote:
>> Hi all,
>> First, my thanks to the many people who gave helpful advice and feedback 
>> on how to measure Log4j response time on this list some time ago.
>> We're about to start the Log4j 2.6 release.
>> If anyone is interested, a preview of the garbage-free logging manual 
>> page is here: 
>> http://home.apache.org/~rpopma/log4j/2.6/manual/garbagefree.html
>> and a preview of the updated performance page is here: 
>> http://home.apache.org/~rpopma/log4j/2.6/performance.html
> Forgive me my ignorance. Maybe I'm missing something or misunderstanding 
> something. If I want to a custom domain object like this
> LOG.info("something happened with {}", aDomainObject);
> then #toString() will be called on that object which very likely will 
> result in allocation. If I don't want this my domain object has to 
> implement CharSequence. I image this to be quite though to do without 
> allocation eg. how can I know the value of #charAt(10)? I would have to 
> "render" the entire object without allocation for every #charAt call.
> What I would really need is an alternative to #toString() that takes an 
> Appendable. Ideally that would be a JDK interface.
> public interface Loggable {
>   void logTo(Appendable log);
> }

Philippe, I completely agree. The only thing is that no such interface 
exists in the JDK, so Log4j 2.6 includes a custom interface:

package org.apache.logging.log4j.util;
public interface StringBuilderFormattable {
     * Writes a text representation of this object into the specified {@code StringBuilder},
     * ideally without allocating temporary objects.

     * @param buffer the StringBuilder to write into
    void formatTo(StringBuilder buffer);

We chose StringBuilder because the Appendable API is not rich enough.
Any Object that is logged, either standalone or as a parameter, if it does 
not implement CharSequence then Log4j 2.6 will check if it implements 
StringBuilderFormattable before calling toString() on it.

I hope this is useful.

Kind regards, Remko

> Cheers
> Philippe 

View raw message