001    /**
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "FileLog.java".  Description: 
010    "An implementation of Log that writes log data to files" 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2001.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL.
025    
026    */
027    
028    package ca.uhn.hl7v2;
029    
030    import java.io.FileWriter;
031    import java.io.PrintWriter;
032    import java.io.BufferedWriter;
033    import java.io.StringWriter;
034    import java.io.File;
035    import java.io.IOException;
036    import java.util.Date;
037    import java.text.DateFormat;
038    import java.util.GregorianCalendar;
039    import java.util.Calendar;
040    import ca.uhn.hl7v2.util.Status;
041    
042    /**
043     * An implementation of Log that writes log data to files.  
044     * @author Bryan Tripp (bryan_tripp@sourceforge.net)
045     * 
046     * @deprecated MUST USE jakarta-commons-logging 
047     */
048    public class FileLog extends Log {
049    
050        BufferedWriter exceptionWriter;  
051        BufferedWriter statusWriter;
052        int currentDay = -1;
053        private String prefix;
054        
055        /** 
056         * Creates new FileLog. 
057         * @exception LogException if one of the log files can not be opened.  
058         */
059        protected FileLog() throws LogException {
060            openLogs();
061        }
062        
063        /**
064         * Sets the exceptionWriter and statusWriter to point to the appropriate log files, based on 
065         * the VM-specific log name found in the property ca.uhn.hl7v2.log.name, and the current date. 
066         */
067        private void openLogs() throws LogException {
068            prefix = System.getProperty("ca.uhn.hl7v2.log.name");
069            if (prefix == null || prefix.equals("")) {  //use default
070                prefix = "hl7";
071            }
072            
073            GregorianCalendar cal = new GregorianCalendar();
074            cal.setTime(new Date(System.currentTimeMillis()));
075            this.currentDay = cal.get(Calendar.DAY_OF_MONTH);
076            String date = DateFormat.getDateInstance(DateFormat.MEDIUM).format(cal.getTime());
077            Status.writeStatus("Setting current log date to " + date);
078    
079            String exceptionLogFileName = prefix + "_exception_" + date + ".log";
080            String statusLogFileName = prefix + "_status_" + date + ".log";
081            
082            //try to open writers to the log files
083            try { 
084                exceptionWriter = new BufferedWriter(new FileWriter(exceptionLogFileName, true));
085            } catch (IOException ioe) {
086                throw new LogException("Can't open writer to exception log file " + exceptionLogFileName);
087            } 
088            try { 
089                statusWriter = new BufferedWriter(new FileWriter(statusLogFileName, true));
090            } catch (IOException ioe) {
091                throw new LogException("Can't open writer to status log file " + statusLogFileName);
092            }                     
093        }
094        
095        /**
096         * If the current day of the month doesn't match the day of the month of the log files, 
097         * openLogs() is called so that new log files can be opened for today. 
098         */
099        private void maybeNewFile() throws LogException {
100           GregorianCalendar cal = new GregorianCalendar();
101           cal.setTime(new Date(System.currentTimeMillis()));
102           
103           if (cal.get(Calendar.DAY_OF_MONTH) != this.currentDay) {
104               openLogs();
105           }
106        }
107    
108        public synchronized void log(Exception e, String message) throws LogException {
109            System.out.println("FileLog: logging exception");
110            if (e == null) { 
111                throw new LogException("Can't log null exception");
112            }
113    
114            maybeNewFile();
115    
116            try {            
117                exceptionWriter.write(getCurrentTime());
118                exceptionWriter.write("\t");
119                exceptionWriter.write(e.getClass().getName());
120                exceptionWriter.write("\t");
121                exceptionWriter.write((e.getMessage() == null) ? "" : e.getMessage());
122                exceptionWriter.write("\t");
123                if (message != null) exceptionWriter.write(message);
124                exceptionWriter.write("\r\n");
125    
126                StringWriter stackTraceWriter = new StringWriter();
127                e.printStackTrace(new PrintWriter(stackTraceWriter));
128                exceptionWriter.write(stackTraceWriter.toString());
129                
130                exceptionWriter.write("\r\n");            
131                exceptionWriter.flush();
132            } catch (IOException ioe) {
133                throw new LogException("Can't write to exception log: " + ioe.toString());
134            }            
135        }
136    
137        public synchronized void log(String status) throws LogException {
138            maybeNewFile();
139            
140            try {
141                statusWriter.write(getCurrentTime());
142                statusWriter.write("\t");
143                statusWriter.write(status);
144                statusWriter.write("\r\n");
145                statusWriter.flush();
146            } catch (IOException ioe) {
147                throw new LogException("Can't write to status log: " + ioe.getMessage());
148            }
149        }
150        
151        public String getDescription() {
152            StringBuffer desc = new StringBuffer();
153            desc.append("HL7 exceptions and status messages are being logged by a FileLog object.  ");
154            desc.append("Exceptions are being logged to files beginning ./");
155            desc.append(prefix);
156            desc.append("_exceptions and status messages are being logged to files beginning ./");
157            desc.append(prefix);
158            desc.append("_status. ");
159            return desc.toString();
160        }
161        
162        private String getCurrentTime() {
163            return DateFormat.getDateTimeInstance().format(new Date());
164        }
165    
166    }