1 23 package com.sun.enterprise.diagnostics.collect; 24 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.PrintWriter ; 30 import java.io.FileWriter ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.util.Date ; 34 import java.util.logging.Level ; 35 import java.util.List ; 36 import java.util.Arrays ; 37 import java.util.Iterator ; 38 import java.text.SimpleDateFormat ; 39 import java.text.ParseException ; 40 import java.util.logging.Logger ; 41 import java.util.logging.Level ; 42 import com.sun.logging.LogDomains; 43 import com.sun.enterprise.diagnostics.Constants; 44 import com.sun.enterprise.diagnostics.Data; 45 import com.sun.enterprise.diagnostics.Defaults; 46 import com.sun.enterprise.diagnostics.ServiceConfig; 47 import com.sun.enterprise.diagnostics.CLIOptions; 48 import com.sun.enterprise.diagnostics.DiagnosticException; 49 import com.sun.enterprise.diagnostics.util.FileUtils; 50 import com.sun.enterprise.diagnostics.util.LogNameFilter; 51 import com.sun.enterprise.diagnostics.util.LogNameComparator; 52 import com.sun.enterprise.diagnostics.collect.*; 53 60 public class LogCollector implements Collector { 61 private static final SimpleDateFormat dateFormat = 62 new SimpleDateFormat (Constants.DATE_PATTERN);; 63 private ServiceConfig config; 64 private Date startDate; 65 private Date endDate; 66 private String destFolder; 67 private boolean partialPrevEntry = false; 68 private boolean prevEntryCopied = false; 69 private String logFileName ; 70 private static Logger logger = 71 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 72 73 public LogCollector(Date startDate, Date endDate, 74 String destFolder,ServiceConfig config) { 75 this.startDate = startDate; 76 this.endDate = endDate; 77 this.destFolder = destFolder; 78 this.config = config; 79 } 80 81 public LogCollector(String destFolder, String logFile) { 82 this.destFolder = destFolder; 83 this.logFileName = logFile; 84 } 85 86 89 public Data capture() throws DiagnosticException { 90 int noOfCapturedEntries = 0; 91 int maxNoOfEntries = Defaults.MAX_NO_OF_ENTRIES; 92 int minLogLevel = Defaults.MIN_LOG_LEVEL; 93 94 if(config != null) { 95 maxNoOfEntries = config.getMaxNoOfEntries(); 96 minLogLevel = config.getMinLogLevel(); 97 logFileName = config.getLogFile(); 98 if(logFileName.indexOf(config.getRepositoryDir()) == -1) 99 logFileName = config.getRepositoryDir() + logFileName; 100 } 101 String destLogFile = destFolder + Defaults.DEST_LOG_FILE; 102 103 108 List logFiles; 109 try { 110 if (startDate != null) { 111 File logFile = new File (logFileName); 112 File logDir = new File (logFile.getParent()); 113 String fileNamePrefix = logFile.getName(); 114 logFiles = FileUtils.getFileListing 115 (logDir, false, 116 new LogNameFilter 117 (fileNamePrefix, startDate, endDate), 118 new LogNameComparator()); 119 if(logFiles != null && logFiles.size() == 0) 120 logFiles = Arrays.asList(new File [] {new File (logFileName)}); 121 } else 122 logFiles = Arrays.asList(new File [] {new File (logFileName)}); 123 124 logger.log(Level.FINE, "diagnostic-service.dest_log_file", 125 new Object [] {destLogFile} ); 126 Iterator filesIterator = logFiles.iterator(); 127 BufferedReader inFile; 128 PrintWriter out ; 129 String logEntry; 130 try { 131 out = new PrintWriter 132 (new BufferedWriter (new FileWriter (destLogFile))); 133 }catch(IOException ioe1) { 134 File parent = (new File (destLogFile)).getParentFile(); 135 parent.mkdirs(); 136 out = new PrintWriter (new BufferedWriter (new FileWriter (destLogFile))); 137 } 138 139 140 while(filesIterator.hasNext() ) { 141 if (noOfCapturedEntries < maxNoOfEntries) { 142 inFile = new BufferedReader 143 (new FileReader ((File )filesIterator.next())); 144 145 while((logEntry = inFile.readLine()) != null) { 146 try { 147 if (isValid 148 (logEntry, startDate, endDate, minLogLevel)) { 149 out.println(logEntry); 150 if (!partialPrevEntry) 151 noOfCapturedEntries++; 152 if (noOfCapturedEntries >= maxNoOfEntries) 153 break; 154 } }catch (Exception pe) { 156 } } 159 out.flush(); 160 } else 162 break; 163 } return new FileData(destLogFile, DataType.LOG_INFO); 165 }catch (FileNotFoundException fnfe) { 166 throw new DiagnosticException(fnfe.getMessage()); 167 } 168 catch (IOException ioe) { 169 throw new DiagnosticException(ioe.getMessage()); 170 } 171 } 173 179 private boolean isValid(String entry, Date startDate, 180 Date endDate, int minLogLevel) 181 throws ParseException { 182 183 186 if(entry.length() <= 0) 188 return false; 189 190 if (partialPrevEntry ) { 192 if(hasEndOfEntry(entry)) 193 partialPrevEntry = false; 194 if (prevEntryCopied) 195 return true; 196 else 197 return false; 198 } 199 200 int logLevelSepBeginIndex = entry.indexOf 202 (Constants.FIELD_SEPARATOR,Constants.ENTRY_DATE_BEGIN_INDEX) + 1; 203 204 String entryLogLevelStr = entry.substring 205 (logLevelSepBeginIndex,entry.indexOf(Constants.FIELD_SEPARATOR, 206 logLevelSepBeginIndex)); 207 int entryLogLevel = Level.parse(entryLogLevelStr).intValue(); 208 209 if (entryLogLevel >= minLogLevel) { 211 if (startDate != null && endDate != null) { 212 Date entryDate = dateFormat.parse 213 (entry.substring(Constants.ENTRY_DATE_BEGIN_INDEX, 214 Constants.ENTRY_DATE_BEGIN_INDEX + Constants.ENTRY_DATE_LENGTH)); 215 216 if (entryDate.compareTo(startDate) >=0 && 217 entryDate.compareTo(endDate) <=0) 218 prevEntryCopied = true; 219 else 220 prevEntryCopied = false; } 222 else 223 prevEntryCopied = true; } 225 else 226 prevEntryCopied = false; 227 228 if(!hasEndOfEntry(entry)) 230 partialPrevEntry = true; 231 return prevEntryCopied; 232 } 233 234 private boolean hasEndOfEntry(String entry) { 235 return (entry.indexOf(Constants.ENTRY_END_INDICATOR) > -1); 236 } 237 238 } 239 | Popular Tags |