1 57 58 package org.apache.wsif.logging; 59 60 import java.util.Locale ; 61 import java.text.MessageFormat ; 62 import java.util.MissingResourceException ; 63 import java.util.ResourceBundle ; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 67 70 public class MessageLogger { 71 private static Log log = LogFactory.getLog("wsif"); 72 73 76 private static ResourceBundle messages = null; 77 78 81 private MessageLogger() { 82 Trc.entry(this); 83 Trc.exit(); 84 } 85 86 public static boolean isInfoEnabled() { 87 return log.isInfoEnabled(); 88 } 89 90 public static boolean isWarnEnabled() { 91 return log.isWarnEnabled(); 92 } 93 94 public static boolean isErrorEnabled() { 95 return log.isErrorEnabled(); 96 } 97 98 public static boolean isFatalEnabled() { 99 return log.isFatalEnabled(); 100 } 101 102 public static void log(String key) { 103 Trc.entry(null,key); 104 try { 105 logIt(key.charAt(key.length() - 1), getMessage(key)); 106 } catch (MissingResourceException mre) { 107 Trc.exception(mre); 108 handleMissingResourceException(mre, key, null); 109 } 110 Trc.exit(); 111 } 112 113 118 public static void log(String key, Object var) { 119 Trc.entry(null,key,var); 120 String [] args = { var == null ? "<null>" : var.toString()}; 121 try { 122 logIt( 123 key.charAt(key.length() - 1), 124 MessageFormat.format(getMessage(key), args)); 125 } catch (MissingResourceException mre) { 126 Trc.exception(mre); 127 handleMissingResourceException(mre, key, args); 128 } 129 Trc.exit(); 130 } 131 132 137 public static void log(String key, Object var1, Object var2) { 138 Trc.entry(null,key,var1,var2); 139 String [] args = { 140 var1 == null ? "<null>" : var1.toString(), 141 var2 == null ? "<null>" : var2.toString()}; 142 try { 143 logIt( 144 key.charAt(key.length() - 1), 145 MessageFormat.format(getMessage(key), args)); 146 } catch (MissingResourceException mre) { 147 Trc.exception(mre); 148 handleMissingResourceException(mre, key, args); 149 } 150 Trc.exit(); 151 } 152 153 160 public static void log(String key, Object [] vars) { 161 Trc.entry(null,key,vars); 162 String [] args; 163 if (vars == null) { 164 args = new String [1]; 165 args[0] = "<null>"; 166 } else { 167 args = new String [vars.length]; 168 for (int i = 0; i < vars.length; i++) 169 args[i] = (vars[i] == null ? "<null>" : vars[i].toString()); 170 } 171 172 try { 173 logIt( 174 key.charAt(key.length() - 1), 175 MessageFormat.format(getMessage(key), args)); 176 } catch (MissingResourceException mre) { 177 Trc.exception(mre); 178 handleMissingResourceException(mre, key, args); 179 } 180 Trc.exit(); 181 } 182 183 188 private static String getMessage(String key) { 189 if (messages == null) 190 messages = ResourceBundle.getBundle("org.apache.wsif.catalog.Messages"); 191 192 return messages.getString(key); 193 } 194 195 private static void logIt(char severity, String text) { 196 if (severity == 'I') { 197 if (isInfoEnabled()) 198 log.info(text); 199 } else if (severity == 'W') { 200 if (isWarnEnabled()) 201 log.warn(text); 202 } else if (severity == 'F') { 203 if (isFatalEnabled()) 204 log.fatal(text); 205 } else { 206 if (isErrorEnabled()) 208 log.error(text); 209 } 210 } 211 212 private static void handleMissingResourceException( 213 MissingResourceException mre, 214 String key, 215 String [] args) { 216 217 StringBuffer sb = new StringBuffer ("WSIF: Unable to display message "); 218 sb.append(key); 219 if (args != null && args.length > 0) { 220 sb.append(" with arguments "); 221 for (int i = 0; i < args.length; i++) { 222 if (i != 0) 223 sb.append(", "); 224 sb.append(args[i]); 225 } 226 } 227 sb.append(" because WSIF MessageLogger caught "); 228 sb.append(mre.toString()); 229 log.error(sb.toString()); 230 } 231 } 232 | Popular Tags |