logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject LogMF and LogSF sandbox projects
Date Fri, 27 Apr 2007 05:01:28 GMT
I've reworked the previous formatter sandbox project and have split  
it up into two very similar sandbox projects: LogMF and LogSF.  Both  
simplify efficient parameterized logging by providing a utility class  
(o.a.l.LogMF and o.a.l.LogSF respectively) to coordinate formatting  
and attempt to defer boxing of primitive types, array creation and  
message formatting until after isEnableFor(Level) has been checked.   
LogMF supports java.text.MessageFormat pattern syntax and LogSF  
supports log4j 1.3 or SLF4J pattern syntax.  I've killed LogF (which  
used JDK 1.5's sprintf type formatter) for the time being.  The  
utility classes have been tested against both log4j 1.2.8 and 1.2.14.
The following changes were made from the previous API:

LogF was removed
The classes were moved from org.apache.log4j.formatter to  
LogXF.log and LogXF.logrb were added (where LogXF is either LogMF or  
LogXF.error and .fatal were removed (use LogXF.log(logger,  
Level.ERROR,...) in its place).

For a log4j 1.3 parameterized log request like:

logger.info("Hello, {}", username);

The equivalents are:

LogSF.info(logger, "Hello, {}", username);
LogMF.info(logger, "Hello, {0}", username);

All single substitution parameter methods have support for all  
primitive types.  Multiple substitution parameter methods require  
Objects.  All levels also have an Object[] method.  You should not  
notice that you do not have vararg capability until after you have  
more than 4 arguments.

//    all the following are legal
LogMF.info(logger, "Iteration {0}", 16);
LogMF.info(logger, "Iteration {0}: Tolerance {1}", Integer.valueOf 
(i), Double.valueOf(tol));
LogMF.info(logger, "Iteration {0}: Errors {1}", Integer.valueOf(i),  
new double[] { Math.PI, Math.PI});
LogMF.info(logger, "Iteration {0}: Tolerance {1}", new Object[]  
{ Integer.valueOf(i), Double.valueOf(tol));
//  Object cast is needed to treat first as a single parameter.   
Would result in "Errors: { 3.14, 3.14 }"
LogMF.info(logger, "Errors {0}", (Object)  new Object[]  
{ Double.valueOf(Math.PI), Double.valueOf(Math.PI}));
//  Without cast array is two substitution parameters, only one of  
which is used.  Would result in "Errors:  3.14"
LogMF.info(logger, "Errors {0}", (Object)  new Object[]  
{ Double.valueOf(Math.PI), Double.valueOf(Math.PI}));

The logrb methods take the name of a resource bundle and key and load  
the resource bundle for the current locale.

LogMF.logrb(logger, Level.INFO, "com.example.widget.LogPattern",  
"Greeting", username);

LogSF is provided to support migration of code that used log4j 1.3  
style parameterized logging.    The simple formatter used did and  
does not support arbitrary ordering of replacement which makes it  
unsuitable when localization may need to change word order.  LogMF  
should be used for new code since java.util.MessageFormat supports a  
much richer pattern syntax and is well suited for localization.

Earlier performance tests showed no noticeable performance  
degradation for using LogSF or LogMF over direct Logger calls using a  
current JVM.  Your mileage may depend on your VM.

You will get null pointer exceptions if logger or level arguments are  
null.  The classes should be robust in face on any other parameter  
being null.

I believe they are ready for use and do not anticipate any  
significant changes in the near future, so feel free to play and give  

LogMF:  svn co https://svn.apache.org/repos/asf/logging/sandbox/log4j/ 
LogSF:  svn co https://svn.apache.org/repos/asf/logging/sandbox/log4j/ 

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

View raw message