1 23 24 29 30 package com.sun.enterprise.cli.framework; 31 32 import java.util.logging.Logger ; 34 import java.util.logging.Level ; 35 import java.util.logging.Handler ; 36 import java.io.ByteArrayOutputStream ; 37 38 42 public class CLILogger 43 { 44 45 private static CLILogger logger; 46 private Logger s1asLogger; 47 private final static String DEBUG_FLAG = "Debug"; 48 private final static int kDefaultBufferSize = 512; 49 private final static String PACKAGE_NAME = "com.sun.enterprise.cli.framework"; 50 51 52 protected CLILogger() 53 { 54 s1asLogger = Logger.getLogger(PACKAGE_NAME, null); 55 if (System.getProperty(DEBUG_FLAG) != null) 56 s1asLogger.setLevel(Level.FINEST); 57 else 58 { 59 s1asLogger.setLevel(Level.INFO); 60 } 62 s1asLogger.addHandler(new CLILoggerHandler()); 63 s1asLogger.setUseParentHandlers(false); 64 } 65 66 public static boolean isDebug() 67 { 68 if (System.getProperty(DEBUG_FLAG) != null) { 69 return true; 70 } else { 71 return false; 72 } 73 } 74 75 78 public static CLILogger getInstance() 79 { 80 if (logger == null) 81 { 82 logger = new CLILogger(); 83 } 84 return logger; 85 } 86 87 91 public Level getOutputLevel() 92 { 93 return s1asLogger.getLevel(); 94 } 95 96 100 public void setOutputLevel(Level level) 101 { 102 if (System.getProperty(DEBUG_FLAG) == null) 103 s1asLogger.setLevel(level); 104 } 105 106 110 public void printMessage(String message) 111 { 112 s1asLogger.log(Level.INFO, message); 113 } 114 115 119 public void printDetailMessage(String message) 120 { 121 s1asLogger.log(Level.FINE, message); 122 } 123 124 128 public void printWarning(String message) 129 { 130 s1asLogger.log(Level.WARNING, message); 131 } 132 133 137 public void printError(String message) 138 { 139 s1asLogger.log(Level.SEVERE, message); 140 } 141 142 146 public void printDebugMessage(String message) 147 { 148 s1asLogger.log(Level.FINEST, message); 149 } 150 151 152 156 public void printExceptionStackTrace(java.lang.Throwable e) 157 { 158 165 final ByteArrayOutputStream output = new ByteArrayOutputStream ( kDefaultBufferSize ); 166 e.printStackTrace( new java.io.PrintStream (output)); 167 printDebugMessage(output.toString()); 168 } 169 170 171 public class CLILoggerHandler extends Handler 172 { 173 174 175 public CLILoggerHandler() 176 { 177 } 178 179 public void publish(java.util.logging.LogRecord logRecord) 180 { 181 if (logRecord.getLevel() == Level.SEVERE) 182 { 183 InputsAndOutputs.getInstance().getErrorOutput().println(logRecord.getMessage()); 184 } 185 else 186 { 187 InputsAndOutputs.getInstance().getUserOutput().println(logRecord.getMessage()); 188 } 189 } 191 192 public void close() throws java.lang.SecurityException 193 { 194 } 195 196 public void flush() 197 { 198 } 199 } 200 201 public static void main(String [] args) 202 { 203 CLILogger logger = new CLILogger(); 204 try 205 { 206 String sLevel = null; 207 LocalStringsManager lsm = LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 209 210 InputsAndOutputs.getInstance().getUserOutput().print(lsm.getString("PROMPT")); 211 sLevel = InputsAndOutputs.getInstance().getUserInput().getLine(); 212 logger.setOutputLevel(java.util.logging.Level.parse(sLevel)); 213 214 System.out.println("Logger level = " + logger.getOutputLevel()); 215 logger.printDetailMessage("Fine"); 216 logger.printMessage("Info"); 217 logger.printError("Error"); 218 logger.printWarning("Warning"); 219 logger.printDebugMessage("Debug"); 220 221 InputsAndOutputs.getInstance().setUserOutputFile("UserOutput.txt"); 223 InputsAndOutputs.getInstance().setErrorOutputFile("ErrorOutput.txt"); 224 InputsAndOutputs.getInstance().setUserInputFile("test_input.txt"); 225 InputsAndOutputs.getInstance().getUserOutput().print(lsm.getString("PROMPT")); 226 sLevel = InputsAndOutputs.getInstance().getUserInput().getLine(); 227 logger.setOutputLevel(java.util.logging.Level.parse(sLevel)); 228 logger.printDetailMessage("Fine"); 229 logger.printMessage("Info"); 230 logger.printError("Error"); 231 logger.printWarning("Warning"); 232 logger.printDebugMessage("Debug"); 233 234 } 235 catch (Exception e) 236 { 237 logger.printExceptionStackTrace(e); 238 } 240 } 241 } 242 | Popular Tags |