[slf4j-dev] svn commit: r940 - in slf4j/trunk/slf4j-converter: . src/main/java/org/slf4j/converter src/main/java/org/slf4j/converter/line
ceki at slf4j.org
ceki at slf4j.org
Thu Jan 10 22:05:03 CET 2008
Author: ceki
Date: Thu Jan 10 22:05:03 2008
New Revision: 940
Modified:
slf4j/trunk/slf4j-converter/LIMITATIONS.txt
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/InplaceFileConverter.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/ConversionRule.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/LineConverter.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/Log4jRuleSet.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/MultiGroupConversionRule.java
slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/SingleConversionRule.java
Log:
- added support for multi-line replacements. This is required for log4j conversions.
Modified: slf4j/trunk/slf4j-converter/LIMITATIONS.txt
==============================================================================
--- slf4j/trunk/slf4j-converter/LIMITATIONS.txt (original)
+++ slf4j/trunk/slf4j-converter/LIMITATIONS.txt Thu Jan 10 22:05:03 2008
@@ -1,4 +1,5 @@
+The slf4j-converter aims to
- if a method declares multipe loggers on the same line, the conversion will not be complete. Example:
@@ -15,4 +16,7 @@
-- the FATAL level is not supported
\ No newline at end of file
+- the FATAL level is not supported.
+
+ This is limitation is not deemed serious because there are usually
+ very few log statements bearing the FATAL level.
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/InplaceFileConverter.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/InplaceFileConverter.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/InplaceFileConverter.java Thu Jan 10 22:05:03 2008
@@ -9,21 +9,21 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.io.Writer;
import org.slf4j.converter.line.LineConverter;
public class InplaceFileConverter {
-
final static int BUFFER_LEN = 8 * 1024;
final LineConverter lineConverter;
final String lineTerminator;
-
+
InplaceFileConverter(LineConverter lineConverter) {
this.lineConverter = lineConverter;
lineTerminator = System.getProperty("line.separator");
}
-
+
byte[] readFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -37,7 +37,7 @@
return baos.toByteArray();
}
-
+
void convert(File file, byte[] input) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
Reader reader = new InputStreamReader(bais);
@@ -46,13 +46,19 @@
while (true) {
String line = breader.readLine();
if (line != null) {
- String newLine = lineConverter.getReplacement(line);
- fileWriter.write(newLine);
- fileWriter.write(lineTerminator);
+ String[] replacement = lineConverter.getReplacement(line);
+ writeReplacement(fileWriter, replacement);
} else {
fileWriter.close();
break;
}
}
}
+
+ void writeReplacement(Writer writer, String[] replacement) throws IOException {
+ for (int i = 0; i < replacement.length; i++) {
+ writer.write(replacement[i]);
+ writer.write(lineTerminator);
+ }
+ }
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/ConversionRule.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/ConversionRule.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/ConversionRule.java Thu Jan 10 22:05:03 2008
@@ -38,5 +38,14 @@
* @return String
*/
public String replace(Matcher matcher);
+
+ /**
+ * Returns a non-null value if there should be an additional line
+ * following a match of this rule. In most cases this method
+ * returns null.
+ *
+ * @return String
+ */
+ public String getAdditionalLine();
}
\ No newline at end of file
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/LineConverter.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/LineConverter.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/LineConverter.java Thu Jan 10 22:05:03 2008
@@ -23,11 +23,12 @@
* @param text
* @return String
*/
- public String getReplacement(String text) {
+ public String[] getReplacement(String text) {
ConversionRule conversionRule;
Pattern pattern;
Matcher matcher;
Iterator<ConversionRule> conversionRuleIterator = ruleSet.iterator();
+ String additionalLine = null;
while (conversionRuleIterator.hasNext()) {
conversionRule = conversionRuleIterator.next();
pattern = conversionRule.getPattern();
@@ -36,8 +37,16 @@
System.out.println("matching " + text);
String replacementText = conversionRule.replace(matcher);
text = matcher.replaceAll(replacementText);
+ if(conversionRule.getAdditionalLine() != null) {
+ additionalLine = conversionRule.getAdditionalLine();
+ }
}
}
- return text;
+
+ if(additionalLine == null) {
+ return new String[] {text};
+ } else {
+ return new String[] {text, additionalLine};
+ }
}
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/Log4jRuleSet.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/Log4jRuleSet.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/Log4jRuleSet.java Thu Jan 10 22:05:03 2008
@@ -10,13 +10,17 @@
private ArrayList<ConversionRule> conversionRuleList;
public Log4jRuleSet() {
+
SingleConversionRule cr0 = new SingleConversionRule(Pattern
+ .compile("import\\s*+org.apache.log4j.Logger;"),
+ "import org.slf4j.Logger;",
+ "import org.slf4j.LoggerFactory;");
+
+ SingleConversionRule cr1 = new SingleConversionRule(Pattern
.compile("import\\s*+org.apache.log4j.LogManager;"),
"import org.slf4j.LoggerFactory;");
- SingleConversionRule cr1 = new SingleConversionRule(Pattern
- .compile("import\\s*+org.apache.log4j.Logger;"),
- "import org.slf4j.Logger;");
+
SingleConversionRule cr2 = new SingleConversionRule(Pattern
.compile("Logger.getLogger\\("), "LoggerFactory.getLogger(");
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/MultiGroupConversionRule.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/MultiGroupConversionRule.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/MultiGroupConversionRule.java Thu Jan 10 22:05:03 2008
@@ -88,4 +88,8 @@
}
return replacementBuffer.toString();
}
+
+ public String getAdditionalLine() {
+ return null;
+ }
}
Modified: slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/SingleConversionRule.java
==============================================================================
--- slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/SingleConversionRule.java (original)
+++ slf4j/trunk/slf4j-converter/src/main/java/org/slf4j/converter/line/SingleConversionRule.java Thu Jan 10 22:05:03 2008
@@ -39,12 +39,18 @@
final private Pattern pattern;
final private String replacementText ;
-
+ final private String additionalLine;
+
public SingleConversionRule(Pattern pattern, String replacementText) {
+ this(pattern, replacementText, null);
+ }
+
+ public SingleConversionRule(Pattern pattern, String replacementText, String additionalLine) {
this.pattern = pattern;
this.replacementText = replacementText;
+ this.additionalLine = additionalLine;
}
-
+
/* (non-Javadoc)
* @see org.slf4j.converter.ConversionRule#getPattern()
*/
@@ -60,4 +66,9 @@
public String replace(Matcher matcher) {
return replacementText;
}
+
+ public String getAdditionalLine() {
+ return additionalLine;
+ }
+
}
More information about the slf4j-dev
mailing list