You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by g....@accenture.com on 2011/04/04 17:27:50 UTC

Avoiding the unnecessary message build cost

Hi,
      why don't we add a method in the Logger class or at least a Util with a method like the following one in order to avoid the cost of the message build.
      In this way we'll not have to build the message unless the priority is enabled for the logger.

      I'll wait for your thoughts.

Regards,


import org.apache.log4j.Logger;

import org.apache.log4j.Priority;



public class LoggerUtil {



    static public void log(Logger logger, Priority priority, Object... objs){



        if (logger.isEnabledFor(priority) ){

            StringBuffer sb = new StringBuffer();

            for(Object obj : objs){

                sb.append( obj.toString() );

            }

            logger.log(priority, sb.toString() );

        }

    }

}



Cusano Pineda, Gerardo H.
Information System Engineer
Accenture – System Integration & Technology
Buenos Aires, Argentina
• g.x.cusano.pineda@accenture.com<ma...@accenture.com>

________________________________
This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited.

Re: Avoiding the unnecessary message build cost

Posted by Curt Arnold <ca...@apache.org>.
See the LogMF or LogSF classes.  LogMF used MessageFormat type format specifiers ("{1}") while LogSF uses SLF4J format specifiers ("{}") .

varargs were introduced in Java 5 and have an unavoidable array construction/destruction expense even if the level is not reached.  The LogMF and LogSF classes approximate the usability of varargs by having a lot of different signatures.




On Apr 4, 2011, at 10:27 AM, <g....@accenture.com> <g....@accenture.com> wrote:

> Hi,
>       why don't we add a method in the Logger class or at least a Util with a method like the following one in order to avoid the cost of the message build.
>       In this way we'll not have to build the message unless the priority is enabled for the logger.
>  
>       I'll wait for your thoughts.
>  
> Regards,
>  
> import org.apache.log4j.Logger;
> import org.apache.log4j.Priority;
>  
> public class LoggerUtil {
>  
>     static public void log(Logger logger, Priority priority, Object... objs){
>  
>         if (logger.isEnabledFor(priority) ){
>             StringBuffer sb = new StringBuffer();
>             for(Object obj : objs){
>                 sb.append( obj.toString() );
>             }
>             logger.log(priority, sb.toString() );
>         }
>     }
> }
>  
>  
> Cusano Pineda, Gerardo H.
> Information System Engineer
> Accenture – System Integration & Technology
> Buenos Aires, Argentina
> * g.x.cusano.pineda@accenture.com
> 
> This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org