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 "Log.java".  Description: 
010    "Manages the logging of exceptions and status information" 
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    /**
031     * <p>Manages the logging of exceptions and status information.<p>  
032     * <p>To select a particular implementation with which to log, set the system property
033     * <code>ca.on.uhn.hl7.log</code> to the fully qualified class name of a subclass 
034     * of Log before getInstance() is first called.  If this property is not set, the 
035     * default FileLog is used with the log files "hl7_exceptions_<name>_<date>.log" and "hl7_status_<name>_<date>.log" in  
036     * the working directory.</p>  
037     * <p>The logs entries arising from a particular VM can be named, to distinguish them from 
038     * log entries from other VMs.  To do this, set the system property "ca.uhn.hl7v2.log.name" to something 
039     * descriptive.<p>
040     * @author Bryan Tripp (bryan_tripp@sourceforge.net)
041     * 
042     * @deprecated MUST USE jakarta-commons-logging
043     */
044    public abstract class Log {
045    
046        private static Log log;
047        
048        /**
049         * <p>Returns the singleton instance of Log.  The Log returned will be an instance of a 
050         * subclass that uses a specific means of logging (e.g. logging to a particular text 
051         * file or database).  </p>
052         * @exception LogException there is a problem opening the Log or the subclass 
053         *      specified can not be found or is not in fact a subclass of Log.  
054         */
055        public static synchronized Log getInstance() throws LogException {
056            if (log == null) {
057                String logClass = System.getProperty("ca.on.uhn.hl7.log");
058                if (logClass == null) {
059                    log = new FileLog();
060                } else {
061                    try {
062                        log = (Log)Class.forName(logClass).newInstance();
063                    } catch (ClassNotFoundException ce) {
064                        throw new LogException("Can't initialize HL7 Log - can't find the Log subclass " + 
065                            logClass + ".");
066                    } catch (ClassCastException cce) {
067                        throw new LogException("Can't initialize HL7 Log - the specified log class - " + 
068                            logClass + " - does not extend ca.on.uhn.hl7.Log");
069                    } catch (Exception e) {
070                        throw new LogException("Can't initialize HL7 log of class " + 
071                            logClass + ": " + e.getClass().getName() + ": " + e.getMessage());
072                    }
073                }
074            }
075            return log;
076            
077        }
078    
079      
080        /**
081         * Convenience method for logging exceptions - if a LogException 
082         * is thrown while logging, the original exception is written to std out 
083         * and no exception is thrown by this method.  
084         */
085        public static void tryToLog(Exception e, String message) {
086            try {
087                Log.getInstance().log(e, message);
088            } catch (Exception loge) {
089                loge.printStackTrace();
090                System.err.println("\r\n\r\n------------------\r\nCan't write to exception log - writing to standard out ..." + message); 
091                e.printStackTrace();
092                System.err.println("-----------------\r\n\r\n");
093                
094            }
095        }
096        
097        /**
098         * Logs an Exception. 
099         * @exception LogException if there is an error writing to the log.  
100         */
101        public abstract void log(Exception e, String message) throws LogException;
102        
103        /**
104         * Logs a status message. 
105         * @exception LogException if there is an error writing to the log.  
106         */
107        public abstract void log(String status) throws LogException;
108        
109        /**
110         * Returns a description of where logged data are being stored.  Implementing 
111         * classes should return sufficient text to allow a human who reads this message 
112         * to independently view information in the log (e.g. the file name, password for the 
113         * database, etc.).  
114         */
115        public abstract String getDescription();
116        
117        //test
118        public static void main(String args[]) {
119            try { 
120                throw new LogException("this is a test message");
121            } catch (Exception e) {
122                try {
123                    Log.getInstance().log(e, "thing message");
124                } catch (LogException le) {
125                    e.printStackTrace();
126                }
127            }
128            try { 
129                throw new LogException("this is a test message");
130            } catch (Exception e) {
131                try {
132                    Log.getInstance().log(e, "");
133                } catch (LogException le) {
134                    e.printStackTrace();
135                }
136            }
137            try { 
138                throw new LogException("this is a test message");
139            } catch (Exception e) {
140                try {
141                    Log.getInstance().log(e, "");
142                } catch (LogException le) {
143                    e.printStackTrace();
144                }
145            }
146            try { 
147                Log.getInstance().log("status 1");
148            } catch (Exception e) {
149                e.printStackTrace();
150            }
151            try { 
152                Log.getInstance().log("status 2");
153            } catch (Exception e) {
154                e.printStackTrace();
155            }
156        }
157    }