You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/07/31 22:28:04 UTC
svn commit: r681472 - in /geronimo/server/trunk:
framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java
plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java
Author: gawor
Date: Thu Jul 31 13:28:03 2008
New Revision: 681472
URL: http://svn.apache.org/viewvc?rev=681472&view=rev
Log:
read log file line by line instead of loading the entire file into memory (GERONIMO-4224)
Modified:
geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java
geronimo/server/trunk/plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java
Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java?rev=681472&r1=681471&r2=681472&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/main/java/org/apache/geronimo/system/logging/log4j/Log4jService.java Thu Jul 31 13:28:03 2008
@@ -17,25 +17,19 @@
package org.apache.geronimo.system.logging.log4j;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
-import java.io.RandomAccessFile;
import java.io.Reader;
import java.net.MalformedURLException;
-import java.nio.CharBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
-import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
@@ -48,7 +42,6 @@
import org.apache.geronimo.system.logging.SystemLog;
import org.apache.geronimo.system.properties.JvmVendor;
import org.apache.geronimo.system.serverinfo.DirectoryUtils;
-import org.apache.geronimo.system.serverinfo.ServerConstants;
import org.apache.geronimo.system.serverinfo.ServerInfo;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
@@ -77,8 +70,6 @@
private final static Pattern UNKNOWN_WARN_START = Pattern.compile("(WARN|ERROR|FATAL)");
private final static Pattern UNKNOWN_INFO_START = Pattern.compile("(INFO|WARN|ERROR|FATAL)");
private final static Pattern UNKNOWN_DEBUG_START = Pattern.compile("(DEBUG|INFO|WARN|ERROR|FATAL)");
- // Pattern that matches a single line (used to calculate line numbers and check for follow-on stack traces)
- private final static Pattern FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE);
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Log4jService.class);
@@ -361,21 +352,20 @@
List list = new LinkedList();
boolean capped = false;
int lineCount = 0;
+ FileInputStream logInputStream = null;
try {
- RandomAccessFile raf = new RandomAccessFile(file, "r");
- FileChannel fc = raf.getChannel();
- MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
- CharBuffer cb = Charset.forName(System.getProperty("file.encoding")).decode(bb);
+ logInputStream = new FileInputStream(file);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(logInputStream, System.getProperty("file.encoding")));
Matcher target = null;
Matcher any = null;
- Matcher lines = FULL_LINE_PATTERN.matcher(cb);
Matcher text = textSearch == null ? null : textSearch.matcher("");
boolean hit = false;
max = Math.min(max, MAX_SEARCH_RESULTS);
- while(lines.find()) {
+ String line;
+ while ((line = reader.readLine()) != null) {
++lineCount;
if(target == null) {
- if(DEFAULT_ANY_START.matcher(cb.subSequence(lines.start(), lines.end())).find()) {
+ if(DEFAULT_ANY_START.matcher(line).find()) {
target = getDefaultPatternForLevel(targetLevel).matcher("");
any = DEFAULT_ANY_START.matcher("");
} else {
@@ -389,7 +379,6 @@
if(stop != null && stop.intValue() < lineCount) {
continue;
}
- CharSequence line = cb.subSequence(lines.start(), lines.end());
target.reset(line);
if(target.find()) {
if(text != null) {
@@ -418,9 +407,17 @@
}
}
}
- fc.close();
- raf.close();
- } catch (Exception e) {} // TODO: improve exception handling
+ } catch (Exception e) {
+ // TODO: improve exception handling
+ } finally {
+ if (logInputStream != null) {
+ try {
+ logInputStream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped);
}
Modified: geronimo/server/trunk/plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java?rev=681472&r1=681471&r2=681472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java (original)
+++ geronimo/server/trunk/plugins/system-database/geronimo-derby/src/main/java/org/apache/geronimo/derby/DerbyLogGBean.java Thu Jul 31 13:28:03 2008
@@ -19,12 +19,11 @@
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import java.io.BufferedReader;
import java.io.File;
-import java.io.RandomAccessFile;
-import java.nio.CharBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
@@ -37,8 +36,7 @@
* @version $Rev$ $Date$
*/
public class DerbyLogGBean implements DerbyLog {
- // Pattern that matches a single line (used to calculate line numbers and text search in a line)
- private final static Pattern FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE);
+
private final DerbySystem derby;
private File logFile = null;
@@ -69,15 +67,14 @@
List list = new LinkedList();
boolean capped = false;
int lineCount = 0;
+ FileInputStream logInputStream = null;
try {
- RandomAccessFile raf = new RandomAccessFile(file, "r");
- FileChannel fc = raf.getChannel();
- MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
- CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Derby use a different charset on a foreign PC?
- Matcher lines = FULL_LINE_PATTERN.matcher(cb);
+ logInputStream = new FileInputStream(file);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(logInputStream, "US-ASCII"));
Matcher text = textSearch == null ? null : textSearch.matcher("");
max = Math.min(max, MAX_SEARCH_RESULTS);
- while(lines.find()) {
+ String line;
+ while ((line = reader.readLine()) != null) {
++lineCount;
if(start != null && start.intValue() > lineCount) {
continue;
@@ -85,7 +82,6 @@
if(stop != null && stop.intValue() < lineCount) {
continue;
}
- CharSequence line = cb.subSequence(lines.start(), lines.end());
if(text != null) {
text.reset(line);
if(!text.find()) {
@@ -98,9 +94,18 @@
capped = true;
}
}
- fc.close();
- raf.close();
- } catch (Exception e) {}
+
+ } catch (Exception e) {
+ // TODO: improve exception handling
+ } finally {
+ if (logInputStream != null) {
+ try {
+ logInputStream.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped);
}
public static final GBeanInfo GBEAN_INFO;