KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > collect > LogCollector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.collect;
24
25 import java.io.File JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39 import java.text.ParseException JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.logging.Level JavaDoc;
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 /**
54  * Visits one or more server.log files to collect log entries
55  * satisfying the log filters - startDate, endDate, minLogLevel and
56  * max number of log entries collected. Collects log entries
57  * pertaingin to ULF only.
58  * @author Manisha Umbarje
59  */

60 public class LogCollector implements Collector {
61     private static final SimpleDateFormat JavaDoc dateFormat =
62     new SimpleDateFormat JavaDoc(Constants.DATE_PATTERN);;
63     private ServiceConfig config;
64     private Date JavaDoc startDate;
65     private Date JavaDoc endDate;
66     private String JavaDoc destFolder;
67     private boolean partialPrevEntry = false;
68     private boolean prevEntryCopied = false;
69     private String JavaDoc logFileName ;
70     private static Logger JavaDoc logger =
71             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
72
73     public LogCollector(Date JavaDoc startDate, Date JavaDoc endDate,
74             String JavaDoc 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 JavaDoc destFolder, String JavaDoc logFile) {
82         this.destFolder = destFolder;
83         this.logFileName = logFile;
84     }
85
86    /**
87     * Captures contents for server.log
88     */

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 JavaDoc destLogFile = destFolder + Defaults.DEST_LOG_FILE;
102
103     // Start reading server.log*, may have to traverse mulitple
104
// log files when start date and/or end date is specified.
105
//If startDate != null
106
// if startDate is not specified, read from server.log only.
107

108     List JavaDoc logFiles;
109     try {
110         if (startDate != null) {
111         File JavaDoc logFile = new File JavaDoc(logFileName);
112         File JavaDoc logDir = new File JavaDoc(logFile.getParent());
113         String JavaDoc 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 JavaDoc[] {new File JavaDoc(logFileName)});
121         } else
122             logFiles = Arrays.asList(new File JavaDoc[] {new File JavaDoc(logFileName)});
123
124         logger.log(Level.FINE, "diagnostic-service.dest_log_file",
125                     new Object JavaDoc[] {destLogFile} );
126         Iterator JavaDoc filesIterator = logFiles.iterator();
127         BufferedReader JavaDoc inFile;
128         PrintWriter JavaDoc out ;
129         String JavaDoc logEntry;
130         try {
131         out = new PrintWriter JavaDoc
132         (new BufferedWriter JavaDoc(new FileWriter JavaDoc(destLogFile)));
133         }catch(IOException JavaDoc ioe1) {
134         File JavaDoc parent = (new File JavaDoc(destLogFile)).getParentFile();
135         parent.mkdirs();
136         out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(destLogFile)));
137         }
138
139
140         while(filesIterator.hasNext() ) {
141         if (noOfCapturedEntries < maxNoOfEntries) {
142             inFile = new BufferedReader JavaDoc
143             (new FileReader JavaDoc((File JavaDoc)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                 }//if(isValid)
155
}catch (Exception JavaDoc pe) {
156             }//catch
157
}//while
158

159             out.flush();
160         }//if (noOfCapturedEntries < maxNoOfEntris)
161
else
162             break;
163         } //while
164
return new FileData(destLogFile, DataType.LOG_INFO);
165     }catch (FileNotFoundException JavaDoc fnfe) {
166         throw new DiagnosticException(fnfe.getMessage());
167     }
168     catch (IOException JavaDoc ioe) {
169         throw new DiagnosticException(ioe.getMessage());
170     }
171     }//captureLog
172

173     /**
174      * Entries are in the following format
175      * [#|yyyy-mm-ddThh:mm:ss.SSS-Z|Log Level|ProductName_Version|LoggerName|
176      * Key Value Pairs|Message|#]
177      * Entries may span multiple lines
178      */

179     private boolean isValid(String JavaDoc entry, Date JavaDoc startDate,
180             Date JavaDoc endDate, int minLogLevel)
181             throws ParseException JavaDoc {
182
183     //entryDate >= startDate, entryDate <= endDate ,
184
//entryLogLevel >= minLogLevel
185

186     // Blank line
187
if(entry.length() <= 0)
188         return false;
189
190     //Previous Partial Line was copied, copy blindly
191
if (partialPrevEntry ) {
192         if(hasEndOfEntry(entry))
193         partialPrevEntry = false;
194         if (prevEntryCopied)
195         return true;
196         else
197         return false;
198     }
199
200     // New log entry
201
int logLevelSepBeginIndex = entry.indexOf
202     (Constants.FIELD_SEPARATOR,Constants.ENTRY_DATE_BEGIN_INDEX) + 1;
203
204     String JavaDoc entryLogLevelStr = entry.substring
205     (logLevelSepBeginIndex,entry.indexOf(Constants.FIELD_SEPARATOR,
206                         logLevelSepBeginIndex));
207     int entryLogLevel = Level.parse(entryLogLevelStr).intValue();
208
209     // Entry log level > min Log Level
210
if (entryLogLevel >= minLogLevel) {
211         if (startDate != null && endDate != null) {
212         Date JavaDoc 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; // Date comparison fails
221
}
222         else
223         prevEntryCopied = true; //entry is not restricted to date
224
}
225     else
226         prevEntryCopied = false;
227
228     //Determine whether the entry has end indicator
229
if(!hasEndOfEntry(entry))
230         partialPrevEntry = true;
231     return prevEntryCopied;
232     }
233
234     private boolean hasEndOfEntry(String JavaDoc entry) {
235     return (entry.indexOf(Constants.ENTRY_END_INDICATOR) > -1);
236     }
237
238 }
239
Popular Tags