Author: sergeyb
Date: Mon Mar 7 14:38:32 2011
New Revision: 1078785
URL: http://svn.apache.org/viewvc?rev=1078785&view=rev
Log:
[CXF-3372] Better support for daily file appenders
Added:
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/2011-01-22-karaf.log
- copied unchanged from r1078549,
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/karaf.log.1
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/2011-01-23-karaf.log
- copied unchanged from r1078549,
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/karaf.log
Removed:
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/karaf.log
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/logs/karaf.log.1
Modified:
cxf/trunk/rt/management-web/src/main/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorage.java
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorageTest.java
Modified:
cxf/trunk/rt/management-web/src/main/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorage.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/management-web/src/main/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorage.java?rev=1078785&r1=1078784&r2=1078785&view=diff
==============================================================================
---
cxf/trunk/rt/management-web/src/main/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorage.java
(original)
+++
cxf/trunk/rt/management-web/src/main/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorage.java
Mon Mar 7 14:38:32 2011
@@ -26,7 +26,6 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -38,7 +37,7 @@ import java.util.regex.Pattern;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
/**
- * Facilitates reading the log entries from the existing log files
+ * Facilitates reading the log entries from the existing log files.
*/
public class ReadOnlyFileStorage implements ReadableLogStorage {
@@ -48,7 +47,7 @@ public class ReadOnlyFileStorage impleme
public static final String CATEGORY_PROPERTY = "category";
public static final String THREAD_PROPERTY = "thread";
- public static final String DATE_ONLY_FORMAT = "yyyy_MM_dd";
+ public static final String DATE_ONLY_FORMAT = "yyyy-MM-dd";
private static final String LINE_SEP =
System.getProperty("line.separator");
@@ -117,12 +116,14 @@ public class ReadOnlyFileStorage impleme
* the next one with an optional scanning
**/
private FileInfo getNextLogFileInfo(FileInfo logFileInfo, boolean
firstTry) {
- for (Iterator<FileInfo> it = logFiles.iterator(); it.hasNext();) {
- FileInfo fileInfo = it.next();
- if (fileInfo == logFileInfo && it.hasNext()) {
- return it.next();
- } else {
- break;
+ for (int i = 0; i < logFiles.size(); i++) {
+ FileInfo fileInfo = logFiles.get(i);
+ if (fileInfo == logFileInfo) {
+ if (i + 1 < logFiles.size()) {
+ return logFiles.get(i + 1);
+ } else {
+ break;
+ }
}
}
if (firstTry && logDirectory != null && scanLogDirectory()) {
@@ -132,7 +133,7 @@ public class ReadOnlyFileStorage impleme
}
/**
- * Gets the file corresponding to the current page
+ * Gets the file corresponding to the current page.
*/
private FileInfo getLogFileInfo(int pageNumber) {
PageInfo pageInfo = pagesMap.get(pageNumber);
@@ -146,12 +147,12 @@ public class ReadOnlyFileStorage impleme
}
return pageInfo.getFileInfo();
}
- if (pageNumber == 1
- && logDirectory != null
- && logFiles.size() == 0
+ int oldSize = logFiles.size();
+ if (logDirectory != null
&& scanLogDirectory()) {
- FileInfo fileInfo = logFiles.get(0);
- savePagePosition(0, fileInfo);
+ FileInfo fileInfo = logFiles.get(oldSize);
+ // savePagePosition increases the number by 1
+ savePagePosition(pageNumber - 1, fileInfo);
return fileInfo;
}
return null;
@@ -163,7 +164,15 @@ public class ReadOnlyFileStorage impleme
private void savePagePosition(int pageNumber, FileInfo fileInfo) {
try {
long pos = fileInfo.getFile().getFilePointer();
- pagesMap.put(pageNumber + 1, new PageInfo(fileInfo, pos));
+ if (pos < fileInfo.getFile().length()) {
+ pagesMap.put(pageNumber + 1, new PageInfo(fileInfo, pos));
+ } else {
+ FileInfo nextFileInfo = getNextLogFileInfo(fileInfo, false);
+ if (nextFileInfo != null) {
+ pagesMap.put(pageNumber + 1,
+ new PageInfo(nextFileInfo,
nextFileInfo.getFile().getFilePointer()));
+ }
+ }
} catch (IOException ex) {
// ignore
}
@@ -321,22 +330,25 @@ public class ReadOnlyFileStorage impleme
}
}
+
+ /**
+ * It make make sense to map logFile.getChannel() to memory for large files
+ * >= 1MB
+ */
private void processNewLogFile(File file) throws IOException {
RandomAccessFile logFile = new RandomAccessFile(file, "r");
String fileModifiedDate = null;
if (useFileModifiedDate) {
- String dateFormat = fileNameDateFormat == null ? DATE_ONLY_FORMAT
: fileNameDateFormat;
-
if (fileNameDatePattern != null) {
Matcher m = fileNameDatePattern.matcher(file.getName());
- if (m.find()) {
- fileModifiedDate = m.group();
+ if (m.matches() && m.groupCount() > 0) {
+ fileModifiedDate = m.group(1);
}
}
if (fileModifiedDate == null) {
Date fileDate = new Date(file.lastModified());
- fileModifiedDate = new
SimpleDateFormat(dateFormat).format(fileDate);
+ fileModifiedDate = getLogDateFormat().format(fileDate);
}
}
@@ -345,6 +357,11 @@ public class ReadOnlyFileStorage impleme
logFiles.add(fileInfo);
}
+ private SimpleDateFormat getLogDateFormat() {
+ String format = fileNameDateFormat == null ? DATE_ONLY_FORMAT :
fileNameDateFormat;
+ return new SimpleDateFormat(format);
+ }
+
private String getRealLocation(String location) {
int indexOpen = location.indexOf("{");
int indexClose = location.indexOf("}");
@@ -450,10 +467,10 @@ public class ReadOnlyFileStorage impleme
private boolean scanLogDirectory() {
int oldSize = logFiles.size();
for (File file : logDirectory.listFiles()) {
- // just in case
- if (file.isDirectory()) {
- // continue
+ if (file.isDirectory() || file.isHidden()) {
+ continue;
}
+
boolean isNew = true;
for (FileInfo fInfo : logFiles) {
if (fInfo.getFileName().equalsIgnoreCase(file.getName())) {
@@ -535,6 +552,18 @@ public class ReadOnlyFileStorage impleme
protected class FileInfoComparator implements Comparator<FileInfo> {
public int compare(FileInfo info1, FileInfo info2) {
+
+ if (useFileModifiedDate && fileNameDatePattern != null) {
+ SimpleDateFormat dateFormat = getLogDateFormat();
+ try {
+ Date date1 = dateFormat.parse(info1.getFileModified());
+ Date date2 = dateFormat.parse(info2.getFileModified());
+ return date1.compareTo(date2);
+ } catch (Exception ex) {
+ // continue
+ }
+ }
+
String name1 = info1.getFileName();
String name2 = info2.getFileName();
if (fileNameComparator != null) {
Modified:
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorageTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorageTest.java?rev=1078785&r1=1078784&r2=1078785&view=diff
==============================================================================
---
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorageTest.java
(original)
+++
cxf/trunk/rt/management-web/src/test/java/org/apache/cxf/management/web/logging/ReadOnlyFileStorageTest.java
Mon Mar 7 14:38:32 2011
@@ -48,8 +48,10 @@ public class ReadOnlyFileStorageTest ext
storage.setRecordDateFormat(ReadOnlyFileStorage.DATE_ONLY_FORMAT
+ " " + "kk:mm:ss,SSS");
+ storage.setFileNameDatePattern("(\\d\\d\\d\\d-\\d\\d-\\d\\d)-.+");
+
storage.setUseFileModifiedDate(true);
-
+
}
@@ -61,7 +63,7 @@ public class ReadOnlyFileStorageTest ext
@Test
public void testReadRecords() throws Exception {
-
storage.setLogLocation(getClass().getResource("logs/karaf.log.1").toURI().getPath());
+
storage.setLogLocation(getClass().getResource("logs/2011-01-22-karaf.log").toURI().getPath());
List<LogRecord> recordsFirstPage1 = readPage(1, 10, 10);
readPage(2, 10, 10);
@@ -81,7 +83,7 @@ public class ReadOnlyFileStorageTest ext
@Test
public void testReadRecordsWithMultiLines() throws Exception {
-
storage.setLogLocation(getClass().getResource("logs/karaf.log").toURI().getPath());
+
storage.setLogLocation(getClass().getResource("logs/2011-01-23-karaf.log").toURI().getPath());
List<LogRecord> recordsFirstPage1 = readPage(1, 10, 10);
List<LogRecord> recordsLastPage1 = readPage(2, 10, 10);
@@ -104,8 +106,8 @@ public class ReadOnlyFileStorageTest ext
public void testReadRecordsWithMultipleFiles() throws Exception {
List<String> locations = new ArrayList<String>();
-
locations.add(getClass().getResource("logs/karaf.log.1").toURI().getPath());
-
locations.add(getClass().getResource("logs/karaf.log").toURI().getPath());
+
locations.add(getClass().getResource("logs/2011-01-22-karaf.log").toURI().getPath());
+
locations.add(getClass().getResource("logs/2011-01-23-karaf.log").toURI().getPath());
storage.setLogLocations(locations);
List<LogRecord> recordsFirstPage1 = readPage(1, 10, 10);
readPage(2, 10, 10);
@@ -129,6 +131,34 @@ public class ReadOnlyFileStorageTest ext
}
@Test
+ public void testReadRecordsWithMultipleFiles2() throws Exception {
+
+ List<String> locations = new ArrayList<String>();
+
locations.add(getClass().getResource("logs/2011-01-23-karaf.log").toURI().getPath());
+
locations.add(getClass().getResource("logs/2011-01-22-karaf.log").toURI().getPath());
+ storage.setLogLocations(locations);
+ List<LogRecord> recordsFirstPage1 = readPage(1, 10, 10);
+ readPage(2, 10, 10);
+ readPage(3, 10, 10);
+ readPage(4, 10, 10);
+ readPage(5, 10, 10);
+ List<LogRecord> recordsLastPage1 = readPage(6, 10, 2);
+
+ LogRecord recordWithExceptionInMessage = recordsFirstPage1.get(2);
+ assertEquals(LogLevel.ERROR, recordWithExceptionInMessage.getLevel());
+ assertTrue(recordWithExceptionInMessage.getMessage()
+ .contains("mvn:org.apache.cxf/cxf-bundle/"));
+ assertTrue(recordWithExceptionInMessage.getMessage()
+ .contains("Caused by: org.osgi.framework.BundleException"));
+
+ List<LogRecord> recordsFirstPage2 = readPage(1, 10, 10);
+ compareRecords(recordsFirstPage1, recordsFirstPage2);
+
+ List<LogRecord> recordsLastPage2 = readPage(6, 10, 2);
+ compareRecords(recordsLastPage1, recordsLastPage2);
+ }
+
+ @Test
public void testReadRecordsWithScanDirectory() throws Exception {
String dir = getClass().getResource("logs").toURI().getPath();