1 36 37 package jnlp.sample.servlet; 38 import java.text.MessageFormat ; 39 import java.util.*; 40 import java.io.*; 41 import javax.servlet.*; 42 43 44 public class Logger { 45 public final static int NONE = 0; 47 public static final String NONE_KEY = "NONE"; 48 public final static int FATAL = 1; 49 public static final String FATAL_KEY = "FATAL"; 50 public final static int WARNING = 2; 51 public static final String WARNING_KEY = "WARNING"; 52 public final static int INFORMATIONAL = 3; 53 public static final String INFORMATIONAL_KEY = "INFORMATIONAL"; 54 public final static int DEBUG = 4; 55 public static final String DEBUG_KEY = "DEBUG"; 56 57 private final static String LOG_LEVEL = "logLevel"; 59 private final static String LOG_PATH = "logPath"; 60 61 private int _loggingLevel = FATAL; 62 private ServletContext _servletContext = null; 63 private String _logFile = null; 64 private String _servletName = null; 65 66 ResourceBundle _resources = null; 68 69 70 73 public Logger(ServletConfig config, ResourceBundle resources) { 74 _resources = resources; 75 _servletContext = config.getServletContext(); 76 _servletName = config.getServletName(); 77 _logFile = config.getInitParameter(LOG_PATH); 78 if (_logFile != null) { 79 _logFile = _logFile.trim(); 80 if (_logFile.length() == 0) _logFile = null; 81 } 82 String level = config.getInitParameter(LOG_LEVEL); 83 if (level != null) { 84 level = level.trim().toUpperCase(); 85 if (level.equals(NONE_KEY)) _loggingLevel = NONE; 86 if (level.equals(FATAL_KEY)) _loggingLevel = FATAL; 87 if (level.equals(WARNING_KEY)) _loggingLevel = WARNING; 88 if (level.equals(INFORMATIONAL_KEY)) _loggingLevel = INFORMATIONAL; 89 if (level.equals(DEBUG_KEY)) _loggingLevel = DEBUG; 90 } 91 } 92 93 public void addFatal(String key, Throwable throwable) { 95 logEvent(FATAL, getString(key), throwable); 96 } 97 98 public void addWarning(String key, String arg) { 99 logL10N(WARNING, key, arg, (Throwable )null); 100 } 101 102 public void addWarning(String key, String arg, Throwable t) { 103 logL10N(WARNING, key, arg, t); 104 } 105 106 public void addWarning(String key, String arg1, String arg2) { 107 logL10N(WARNING, key, arg1, arg2); 108 } 109 110 public void addWarning(String key, String arg1, String arg2, String arg3) { 111 logL10N(WARNING, key, arg1, arg2, arg3); 112 } 113 114 public void addInformational(String key) { 115 logEvent(INFORMATIONAL, getString(key), (Throwable )null); 116 } 117 118 public void addInformational(String key, String arg) { 119 logL10N(INFORMATIONAL, key, arg, (Throwable )null); 120 } 121 122 public void addInformational(String key, String arg1, String arg2, String arg3) { 123 logL10N(INFORMATIONAL, key, arg1, arg2, arg3); 124 } 125 126 public void addDebug(String msg) { logEvent(DEBUG, msg, null); } 128 129 public void addDebug(String msg, Throwable throwable) { 130 logEvent(DEBUG, msg, throwable); 131 } 132 133 boolean isNoneLevel() { return _loggingLevel >= NONE; } 135 boolean isFatalevel() { return _loggingLevel >= FATAL; } 136 boolean isWarningLevel() { return _loggingLevel >= WARNING; } 137 boolean isInformationalLevel() { return _loggingLevel >= INFORMATIONAL; } 138 boolean isDebugLevel() { return _loggingLevel >= DEBUG; } 139 140 private String getString(String key) { 142 try { 143 return _resources.getString(key); 144 } catch (MissingResourceException mre) { 145 return "Missing resource for: " + key; 146 } 147 } 148 149 private void logL10N(int level, String key, String arg, Throwable e) { 150 Object [] messageArguments = { arg }; 151 logEvent(level, applyPattern(key, messageArguments), e); 152 } 153 154 private void logL10N(int level, String key, String arg1, String arg2) { 155 Object [] messageArguments = { arg1, arg2 }; 156 logEvent(level, applyPattern(key, messageArguments), null); 157 } 158 159 private void logL10N(int level, String key, String arg1, String arg2, String arg3) { 160 Object [] messageArguments = { arg1, arg2, arg3 }; 161 logEvent(level, applyPattern(key, messageArguments), null); 162 } 163 164 165 private String applyPattern(String key, Object [] messageArguments) { 166 String message = getString(key); 167 MessageFormat formatter = new MessageFormat (message); 168 String output = formatter.format(message, messageArguments); 169 return output; 170 } 171 172 private synchronized void logEvent(int level, String string, Throwable throwable) { 174 if (level > _loggingLevel) return; 176 177 if (_logFile != null) { 178 PrintWriter pw = null; 180 try { 181 pw = new PrintWriter(new FileWriter(_logFile, true)); 182 pw.println(_servletName + "(" + level + "): " + string); 183 if (throwable != null) { 184 throwable.printStackTrace(pw); 185 } 186 pw.close(); 187 return; 190 } catch (IOException ioe) { 191 192 } 193 } 194 195 if (throwable == null) { 197 _servletContext.log(string); 198 } else { 199 _servletContext.log(string, throwable); 200 } 201 } 202 } 203 204 | Popular Tags |