Hi,
Vincent Massol wrote:
On Sep 13, 2008, at 11:30 PM, Jonas von Malottki
wrote:
Oh, Hibernate now uses slf4j, which I once suggested for use in
XWiki
(Again it is *not* an implementation, it's just a facade, with
plug- able
implementations.)
But we don't need it... We have our own simple facade which
can be
plugged to any logging system including slf4j.
See
http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/src/…
Ah, seeing this remembered me, that I wanted to introduce one of the
slf4j features: delayed expanding of messages for logging see:
http://n2.nabble.com/Logging-architecture-proposal-tt516791.html#none
Ah I had forgotten this email of yours, sorry about that. I've read it
again and it seems good to me, especially since our best practice is
now to use this:
logger.debug(String.format("text with %s in format syntax", var1,
var2,...);
So I'm +1 for modifying our logger interface to support this style.
Actually I even think we should maybe deprecate the old signatures.
I have done a small patch with this now (attachment).
Or should I
create a Jira issue?
Can you create a jira issue and attach your patch there?
Thanks a lot Jonas for pursuing this!
-Vincent
An SLF4J Logger implementation is also very easy then.
And I can use
it ;).
Greetings
Jonas
I'm completely against replacing it with any
external framework
since those frameworks come and go. As an example the de facto
standard was log4j a few years ago, then came commons-logging,
then it was included in the JDK, then now slf4j. Then who knows
what will happen tomorrow. Our rule is to use our own interface
for all our core components so that we are independent of such
changes.
Thanks
-Vincent
Index: core/xwiki-component/src/main/java/org/xwiki/component/
logging/Logger.java
===================================================================
--- core/xwiki-component/src/main/java/org/xwiki/component/logging/
Logger.java (revision 12676)
+++ core/xwiki-component/src/main/java/org/xwiki/component/logging/
Logger.java (working copy)
@@ -39,17 +39,41 @@
void debug( String message );
+ /**
+ * Logs the {@code message} to the logger while replacing all
+ * '{}' in the message String with the corresponding object
+ * from the objects array. Replacing first '{}' by
+ * {@code objects[0].toString()}, second '{}' by
+ * {@code objects[1].toString()} and so forth.
+ * </br>
+ * Ideally the message should only be expanded and thus the
+ * {@code toString()} be called iff the message is really
logged.
+ *
+ * @param message The message to be formatted
+ * @param objects The objects to be filled into the message
+ */
+ void debug( String message, Object... objects );
+
void debug( String message, Throwable throwable );
boolean isDebugEnabled();
void info( String message );
+
+ /**
+ * @see Logger.debug(String, Object...)
+ * @param message
+ * @param objects
+ */
+ void info( String message, Object... objects );
void info( String message, Throwable throwable );
boolean isInfoEnabled();
void warn( String message );
+
+ void warn( String message, Object... objects );
void warn( String message, Throwable throwable );
@@ -57,6 +81,8 @@
void error( String message );
+ void error( String message, Object... objects );
+
void error( String message, Throwable throwable );
boolean isErrorEnabled();
Index: core/xwiki-plexus/src/main/java/org/xwiki/plexus/logging/
PlexusLogger.java
===================================================================
--- core/xwiki-plexus/src/main/java/org/xwiki/plexus/logging/
PlexusLogger.java (revision 12676)
+++ core/xwiki-plexus/src/main/java/org/xwiki/plexus/logging/
PlexusLogger.java (working copy)
@@ -26,6 +26,64 @@
{
private org.codehaus.plexus.logging.Logger logger;
+ private static String expandMessage(String message, Object...
objects){
+ if(message == null)
+ return "";
+ if(objects == null )
+ return message;
+ int olast = objects.length - 1;
+ if(olast == -1)
+ return message;
+
+ char[] msgchar = message.toCharArray();
+ int mlast = msgchar.length - 1;
+
+ StringBuffer sb = new StringBuffer();
+
+ int o = 0;
+
+ // iterate through the Object Array and replace '{}' in the
msg
+ for(int i = 0; i < mlast; i++)
+ {
+ if(msgchar[i] == '{' && msgchar[i+1] == '}')
+ {
+ i++; // the next one can be skipped
+ if( o < objects.length )
+ {
+ if(objects[o] == null)
+ {
+ sb.append("(null)");
+ } else {
+ sb.append(objects[o].toString());
+ }
+ o++;
+ }
+ } else {
+ sb.append(msgchar[i]);
+ }
+ }
+
+ // append last char only if it did not came from a '{}'
+ if( !(msgchar[mlast-1] == '{' && msgchar[mlast] == '}')
)
+ {
+ sb.append(msgchar[mlast]);
+ }
+
+ if(objects[olast] instanceof Throwable)
+ {
+ Throwable e = (Throwable)objects[olast];
+ sb.append("\n");
+ for(StackTraceElement se : e.getStackTrace())
+ {
+ sb.append(" ");
+ sb.append(se.toString());
+ }
+ }
+
+ return sb.toString();
+
+ }
+
public PlexusLogger(org.codehaus.plexus.logging.Logger logger)
{
this.logger = logger;
@@ -36,6 +94,15 @@
this.logger.debug(message);
}
+ public void debug(String message, Object... objects)
+ {
+ // only expand the message if the Debug level is enabled
+ if(this.logger.isDebugEnabled())
+ {
+ this.logger.debug(expandMessage(message, objects));
+ }
+ }
+
public void debug(String message, Throwable throwable)
{
this.logger.debug(message, throwable);
@@ -50,6 +117,15 @@
{
this.logger.info(message);
}
+
+ public void info(String message, Object... objects)
+ {
+ // only expand the message if the Info level is enabled
+ if(this.logger.isInfoEnabled())
+ {
+ this.logger.info(expandMessage(message, objects));
+ }
+ }
public void info(String message, Throwable throwable)
{
@@ -65,6 +141,15 @@
{
this.logger.warn(message);
}
+
+ public void warn(String message, Object... objects)
+ {
+ // only expand the message if the Warn level is enabled
+ if(this.logger.isWarnEnabled())
+ {
+ this.logger.debug(expandMessage(message, objects));
+ }
+ }
public void warn(String message, Throwable throwable)
{
@@ -81,6 +166,15 @@
this.logger.error(message);
}
+ public void error(String message, Object... objects)
+ {
+ // only expand the message if the Error level is enabled
+ if(this.logger.isErrorEnabled())
+ {
+ this.logger.error(expandMessage(message, objects));
+ }
+ }
+
public void error(String message, Throwable throwable)
{
this.logger.error(message, throwable);