View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/Log.java,v 1.1.1.1 2001/06/18 02:01:06 najmi Exp $ 3 * Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved. 4 * 5 * This software is the confidential and proprietary information of Sun 6 * Microsystems, Inc. ("Confidential Information"). You shall not 7 * disclose such Confidential Information and shall use it only in 8 * accordance with the terms of the license agreement you entered into 9 * with Sun. 10 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE 11 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 12 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 13 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES 14 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 15 * THIS SOFTWARE OR ITS DERIVATIVES. 16 * 17 * 18 * 19 */ 20 21 package com.sun.ebxml.registry.util; 22 23 import java.util.*; 24 import java.io.*; 25 import java.net.*; 26 27 /*** 28 * Error and trace log implementation. A single log is implementated 29 * per process (e.g. Virtual Machine). This implementation uses a file 30 * based model. A separate file for each log category is supported. 31 * 32 * @author Frank Richichi 33 */ 34 35 public class Log { 36 private static Log log = null; 37 private int logLevel = 0; 38 private static boolean usePreamble = true; 39 private PrintWriter[] logWriter = new PrintWriter[LAST_CATEGORY-FIRST_CATEGORY + 1]; 40 41 /*** Log categories are used to differentiate messages into separate 42 * files. The various logs default to System.err and System.out 43 * if not set 44 */ 45 static public final int FIRST_CATEGORY = 0; 46 static public final int ERROR = 0; 47 static public final int WARN = 1; 48 static public final int INFO = 2; 49 static public final int TRACE = 3; 50 static public final int LAST_CATEGORY = 3; 51 static private final String[] CATEGORY_LABEL = {"ERROR", "WARN", "INFO", "TRACE"}; 52 53 /*** 54 * Class Constructor. 55 */ 56 private Log(){ } 57 58 59 /*** 60 * Overrides constructor setting of verbosity level. 61 * 62 * @param level Threshold for logging. 63 * 64 * @see 65 */ 66 private void setLogLevel(int level) { 67 logLevel = level; 68 } 69 70 71 /*** 72 * Sets the indicated logWriter to the PrintWriter. If already set 73 * then no action taken. Also write a preamble. 74 * 75 * @param category log type 76 * @param out writer to print to 77 */ 78 private void setlogWriter(int category, PrintWriter out) { 79 if ((category >= FIRST_CATEGORY ) && (category <= LAST_CATEGORY ) && 80 !(out.equals(logWriter[category]))){ 81 logWriter[category] = out; 82 if (Log.usePreamble == true) 83 writePreamble(category); 84 } 85 } 86 87 88 /*** 89 * writePreamble description 90 * @param category which log to write 91 */ 92 private void writePreamble(int category) { 93 try { 94 Log.print(category, 3, "Starting " + CATEGORY_LABEL[category] + " log at " + 95 (new Date(System.currentTimeMillis())).toString() + 96 " on host " + (InetAddress.getLocalHost()).getHostName() + 97 " log level is " + logLevel); 98 }catch (Exception e){}; 99 Log.print(category, 3, "****************************************************************"); 100 } 101 102 103 /*** 104 * Only log msg if msgLevel is less than current logLevel or if 105 * ERROR. Writes to a PrintWriter if initialized. Otherwise writes 106 * to System.out. Illegal category types are written to TRACE output. 107 * 108 * @param category set to INFO, ERROR, WARN, TRACE. 109 * @param msg text too write 110 * @param msgLevel a number from 0 to 10. Give rare important messages 111 * a low number and give numerous, not important messages a higher number. 112 */ 113 public static void print(int category, int msgLevel, String msg) { 114 if (log == null){ 115 System.out.println(CATEGORY_LABEL[category] + ": " + msg); 116 return; 117 } 118 119 if ((category != ERROR) && (msgLevel > log.logLevel)) 120 return; 121 122 if ((category < FIRST_CATEGORY) || (category > LAST_CATEGORY)) 123 category = TRACE; 124 125 log.logWriter[category].println(CATEGORY_LABEL[category] + ": " + msg); 126 } 127 128 129 /*** 130 * Convenience utility for printing a buffer of objects. 131 */ 132 public static void printBuffer(int category, int msgLevel, Collection buffer, String vName) { 133 int index = 0; 134 Object con; 135 Log.print(category, msgLevel, "Start Collection: " + vName + ", size=" + buffer.size()); 136 Iterator iter = buffer.iterator(); 137 while (iter.hasNext()) { 138 con = (Object) iter.next(); 139 Log.print(category, msgLevel, "\t\t" + index++ + ": " + con.toString()); 140 } 141 Log.print(category, msgLevel, "End Collection: " + vName); 142 } 143 144 145 /*** 146 * Convenience utility for printing stack trace to error log. 147 * Prints to standard error if called before initialized. 148 */ 149 public static void printStackTrace(Throwable t) { 150 if (log == null) { 151 t.printStackTrace(); 152 }else{ 153 t.printStackTrace(log.logWriter[ERROR]); 154 } 155 } 156 157 158 /*** 159 * Initialize log context. [deprecated]. Here for backward compatibility. 160 * Takes a single name and generates error and trace files by adding 161 * an extension to it. stderr and stdout are used if none is specified. 162 * Defaults to overwrite file. 163 * 164 * @param logFileName basename for error and trace logs. 165 * @param level verbosity level 166 */ 167 public static void initialize(String logFileName, int level) { 168 Log.initialize(logFileName, logFileName, level, false); 169 } 170 171 172 /*** 173 * Initialize log context. May be re-initialized. This method 174 * creates PrintWriter OR uses the System.err and System.out 175 * PrintStreams. This method is used for standalone operation. 176 * A preamble is used for output to a file. If System.err or 177 * System.out is used the preamble is not printed. 178 * 179 * @param errorFileName use stderr if errorFileName is null. 180 * @param traceFileName use stdout if traceFileName is null. 181 * @param level verbosity level 182 * @param append if true then append to end of file, otherwise overwrite 183 */ 184 public static void initialize(String errorFileName, String traceFileName, 185 int level, boolean append) { 186 PrintWriter errOut; 187 PrintWriter traceOut; 188 189 try { 190 if (errorFileName != null){ 191 errOut = new PrintWriter(new FileOutputStream(errorFileName, append),true); 192 }else{ 193 errOut = new PrintWriter(System.err, true); 194 Log.usePreamble = false; 195 } 196 } catch (Exception e) { 197 System.err.println("Failed error log file: " + errorFileName 198 + " using stderr. Exception " + e.getMessage()); 199 errOut = new PrintWriter(System.err, true); 200 Log.usePreamble = false; 201 } 202 203 try { 204 if (traceFileName != null){ 205 //If same file name but not NULL use same PrintWriter 206 traceOut = (traceFileName.equals(errorFileName)) ? 207 errOut : new PrintWriter(new FileOutputStream(traceFileName, append)); 208 }else{ 209 traceOut = new PrintWriter(System.out, true); 210 Log.usePreamble = false; 211 } 212 } catch (Exception e) { 213 System.err.println("Failed trace log file: " + traceFileName 214 + " using stdout. Exception " + e.getMessage()); 215 traceOut = new PrintWriter(System.out, true); 216 Log.usePreamble = false; 217 } 218 log.initialize(errOut, traceOut, level); 219 } 220 221 222 /*** 223 * Initialize log context. May be initialized only once. 224 * @param errorOut writer for stack trace and error 225 * @param traceOut use stdout if traceFileName is null. 226 * @param level verbosity level 227 */ 228 public static void initialize(PrintWriter errorOut, PrintWriter traceOut, int level) { 229 if (log != null) return; 230 231 log = new Log(); 232 233 log.setLogLevel(level); 234 log.setlogWriter(ERROR, errorOut); 235 log.setlogWriter(WARN, errorOut); 236 log.setlogWriter(INFO, traceOut); 237 log.setlogWriter(TRACE, traceOut); 238 } 239 240 } 241

This page was automatically generated by Maven