Bug 975731

Summary: IllegalArgumentException thrown in ExoLogger when characters used in RegExps such as $,(,) are used in the message
Product: [JBoss] JBoss Enterprise Portal Platform 6 Reporter: Adam Kovari <akovari>
Component: PortalAssignee: Nobody <nobody>
Status: VERIFIED --- QA Contact:
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.0.0CC: bdawidow, epp-bugs, jpallich, nobody, theute, tkyjovsk
Target Milestone: ER01   
Target Release: 6.1.1   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Adam Kovari 2013-06-19 08:44:31 UTC
Description of problem:
    ExoLogger.getLogger(ExoLoggerTest.class).info("Hello {}", "$myself");

causes:

java.lang.IllegalArgumentException: Illegal group reference
    at java.util.regex.Matcher.appendReplacement(Matcher.java:808)
    at java.util.regex.Matcher.replaceFirst(Matcher.java:955)
    at org.exoplatform.services.log.impl.LogMessageFormatter.getMessage(LogMessageFormatter.java:44)
    at org.exoplatform.services.log.impl.SimpleExoLog.info(SimpleExoLog.java:157)


Version-Release number of selected component (if applicable):
JPP 6

How reproducible:
Always

Steps to Reproduce:
1. ExoLogger.getLogger(ExoLoggerTest.class).info("Hello {}", "$myself");

Actual results:
Exception thrown

Expected results:
Message to be properly replaced

Additional info:
Proposed fix:

diff --git a/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java b/exo.kernel.commons/src/main/java/org/exoplatform/services/log
index faed574..3c18d59 100644
--- a/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java
+++ b/exo.kernel.commons/src/main/java/org/exoplatform/services/log/impl/LogMessageFormatter.java
@@ -19,6 +19,7 @@
 package org.exoplatform.services.log.impl;
 
 import java.util.regex.Pattern;
+import java.util.regex.Matcher;
 
 /**
  * Simple class to provide format parsing of log messages similar to what slf4j library does.
@@ -41,7 +42,7 @@ public class LogMessageFormatter
             if (i != argsArray.length - 1 || !(argsArray[i] instanceof Throwable))
             {
                String message = String.valueOf(argsArray[i]);
-               str = REPLACE_PATTERN.matcher(str).replaceFirst(message != null ? message : "null");
+               str = REPLACE_PATTERN.matcher(str).replaceFirst(message != null ? Matcher.quoteReplacement(message) : "null");
             }
          }
       }
diff --git a/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java b/exo.kernel.commons/src/test/java/org/exoplatform/commons/util
index 4f4ebfe..06e1abf 100644
--- a/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java
+++ b/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestLogMessageFormatter.java
@@ -73,4 +73,8 @@ public class TestLogMessageFormatter extends TestCase
          return "";
       }
    }
+   
+       public void testSpecialCharacterReplacement() throws Exception{
+               assertEquals("Hello, World $var", LogMessageFormatter.getMessage("Hello, World {}", "$var"));
+               assertEquals("Hello $foo and $bar", LogMessageFormatter.getMessage("Hello {} and {}", "$foo", "$bar"));
+       }   
 }

Comment 1 Thomas Heute 2013-06-19 09:28:15 UTC
eXo logger is a private logger and should not be used by customers. There are many other logger alternatives.

Any reason to use eXo logger here ?

Comment 4 Tomas Kyjovsky 2014-02-20 13:31:17 UTC
Tested with the simplest-hello-world-portlet in 6.1.1.CR1.



ExoLogger.getLogger(SimplestHelloWorldPortlet.class).info("Hello {}", "$myself");

14:26:24,844 INFO  [org.jboss.quickstarts.portal.simplest.SimplestHelloWorldPortlet] (http-/0.0.0.0:8080-2) Hello $myself


ExoLogger.getLogger(SimplestHelloWorldPortlet.class).info("Hello {}", "$myself ()");

14:28:13,032 INFO  [org.jboss.quickstarts.portal.simplest.SimplestHelloWorldPortlet] (http-/0.0.0.0:8080-43) Hello $myself ()