1 23 24 package com.sun.enterprise.server.logging; 25 26 import java.util.logging.Formatter ; 27 import java.util.logging.LogRecord ; 28 import java.util.logging.LogManager ; 29 import java.util.logging.Logger ; 30 import java.util.logging.Level ; 31 import java.util.logging.ErrorManager ; 32 import java.util.ResourceBundle ; 33 import java.util.HashMap ; 34 import java.util.Date ; 35 import java.util.Map ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.text.SimpleDateFormat ; 39 import java.text.MessageFormat ; 40 import java.text.FieldPosition ; 41 import java.io.StringWriter ; 42 import java.io.PrintWriter ; 43 44 import com.sun.enterprise.Switch; 45 import com.sun.enterprise.admin.monitor.callflow.Agent; 46 import com.sun.enterprise.admin.monitor.callflow.ThreadLocalData; 47 48 67 public class UniformLogFormatter extends Formatter { 68 private HashMap loggerResourceBundleTable; 71 private LogManager logManager; 72 private Date date = new Date ( ); 74 private static String PRODUCTID_CONTEXTID = null; 75 private static final String PRODUCT_VERSION = 79 com.sun.appserv.server.util.Version.getAbbreviatedVersion(); 80 private static final int FINE_LEVEL_INT_VALUE = Level.FINE.intValue(); 81 82 private static boolean LOG_SOURCE_IN_KEY_VALUE = false; 83 84 private static boolean RECORD_NUMBER_IN_KEY_VALUE = false; 85 86 static { 87 String logSource = System.getProperty( 88 "com.sun.aas.logging.keyvalue.logsource"); 89 if( (logSource != null ) 90 &&(logSource.equals( "true" ) ) ) 91 { 92 LOG_SOURCE_IN_KEY_VALUE = true; 93 } 94 95 String recordCount = System.getProperty( 96 "com.sun.aas.logging.keyvalue.recordnumber"); 97 if( (recordCount != null ) 98 &&(recordCount.equals( "true" ) ) ) 99 { 100 RECORD_NUMBER_IN_KEY_VALUE = true; 101 } 102 } 103 104 private long recordNumber = 0; 105 106 private static final String LINE_SEPARATOR = 107 (String ) java.security.AccessController.doPrivileged( 108 new sun.security.action.GetPropertyAction("line.separator")); 109 110 private static final String RECORD_BEGIN_MARKER = "[#|"; 111 private static final String RECORD_END_MARKER = "|#]" + LINE_SEPARATOR + 112 LINE_SEPARATOR; 113 private static final char FIELD_SEPARATOR = '|'; 114 private static final char NVPAIR_SEPARATOR = ';'; 115 private static final char NV_SEPARATOR = '='; 116 117 private static final String RFC_3339_DATE_FORMAT = 118 "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; 119 120 private static final SimpleDateFormat dateFormatter = 121 new SimpleDateFormat ( RFC_3339_DATE_FORMAT ); 122 123 public UniformLogFormatter( ) { 124 super( ); 125 loggerResourceBundleTable = new HashMap ( ); 126 logManager = LogManager.getLogManager( ); 127 } 128 129 130 135 136 137 public String format( LogRecord record ) { 138 return uniformLogFormat( record ); 139 } 140 141 public String formatMessage( LogRecord record ) { 142 return uniformLogFormat( record ); 143 } 144 145 146 149 protected String getProductId( ) { 150 return PRODUCT_VERSION; 151 } 152 153 154 158 protected void getNameValuePairs(StringBuilder buf, LogRecord record) { 159 160 Object [] parameters = record.getParameters(); 161 if ((parameters == null) || (parameters.length == 0)) { 162 return; 163 } 164 165 try { 166 for (Object obj : parameters) { 167 if (obj == null) { 168 continue; 169 } 170 if (obj instanceof Map ) { 171 Map map = (Map ) obj; 172 for (Object key : map.keySet()) { 173 buf.append(key.toString()).append(NV_SEPARATOR). 174 append(map.get(key).toString()). 175 append(NVPAIR_SEPARATOR); 176 } 177 } else if (obj instanceof java.util.Collection ) { 178 for (Object entry : ((Collection )obj)) { 179 buf.append(entry.toString()).append(NVPAIR_SEPARATOR); 180 } 181 } else { 182 buf.append(obj.toString()).append(NVPAIR_SEPARATOR); 183 } 184 } 185 } catch( Exception e ) { 186 new ErrorManager ().error( 187 "Error in extracting Name Value Pairs", e, 188 ErrorManager.FORMAT_FAILURE ); 189 } 190 } 191 192 private void uniformLogFormat( 193 StringBuilder buf, ThreadLocalData tld, Level level) { 194 195 if (level.equals(Level.INFO) || level.equals(Level.CONFIG)) { 196 197 if (tld.getApplicationName() != null) { 198 buf.append("_ApplicationName").append(NV_SEPARATOR). 199 append(tld.getApplicationName()). 200 append(NVPAIR_SEPARATOR); 201 } 202 203 } else { 204 205 if (tld.getRequestId() != null) { 206 buf.append("_RequestID").append(NV_SEPARATOR). 207 append(tld.getRequestId()).append(NVPAIR_SEPARATOR); 208 } 209 210 if (tld.getApplicationName() != null) { 211 buf.append("_ApplicationName").append(NV_SEPARATOR). 212 append(tld.getApplicationName()). 213 append(NVPAIR_SEPARATOR); 214 } 215 216 if (tld.getModuleName() != null) { 217 buf.append("_ModuleName").append(NV_SEPARATOR). 218 append(tld.getModuleName()).append(NVPAIR_SEPARATOR); 219 } 220 221 if (tld.getComponentName() != null) { 222 buf.append("_ComponentName").append(NV_SEPARATOR). 223 append(tld.getComponentName()).append(NVPAIR_SEPARATOR); 224 } 225 226 if (tld.getComponentType() != null) { 227 buf.append("_ComponentType").append(NV_SEPARATOR). 228 append(tld.getComponentType()).append(NVPAIR_SEPARATOR); 229 } 230 231 if (tld.getMethodName() != null) { 232 buf.append("_MethodName").append(NV_SEPARATOR). 233 append(tld.getMethodName()).append(NVPAIR_SEPARATOR); 234 } 235 236 if (tld.getTransactionId() != null) { 237 buf.append("_TransactionId").append(NV_SEPARATOR). 238 append(tld.getTransactionId()).append(NVPAIR_SEPARATOR); 239 } 240 241 if (tld.getSecurityId() != null) { 242 buf.append("_CallerId").append(NV_SEPARATOR). 243 append(tld.getSecurityId()).append(NVPAIR_SEPARATOR); 244 } 245 } 246 } 247 248 252 private String uniformLogFormat(LogRecord record) { 253 254 try { 255 256 StringBuilder recordBuffer = new StringBuilder (RECORD_BEGIN_MARKER); 257 date.setTime(record.getMillis()); 262 recordBuffer.append(dateFormatter.format(date)); 263 recordBuffer.append(FIELD_SEPARATOR); 264 265 recordBuffer.append(record.getLevel()).append(FIELD_SEPARATOR); 266 recordBuffer.append(getProductId()).append(FIELD_SEPARATOR); 267 recordBuffer.append(record.getLoggerName()).append(FIELD_SEPARATOR); 268 269 recordBuffer.append("_ThreadID").append(NV_SEPARATOR); 270 recordBuffer.append(record.getThreadID()).append(NVPAIR_SEPARATOR); 271 272 recordBuffer.append("_ThreadName").append(NV_SEPARATOR); 273 recordBuffer.append(Thread.currentThread().getName()); 274 recordBuffer.append(NVPAIR_SEPARATOR); 275 276 Level level = record.getLevel(); 279 if (LOG_SOURCE_IN_KEY_VALUE || 280 (level.intValue() <= Level.FINE.intValue())) { 281 recordBuffer.append("ClassName").append(NV_SEPARATOR); 282 recordBuffer.append(record.getSourceClassName()); 283 recordBuffer.append(NVPAIR_SEPARATOR); 284 recordBuffer.append("MethodName").append(NV_SEPARATOR); 285 recordBuffer.append(record.getSourceMethodName()); 286 recordBuffer.append(NVPAIR_SEPARATOR); 287 } 288 289 if (RECORD_NUMBER_IN_KEY_VALUE) { 290 recordBuffer.append("RecordNumber").append(NV_SEPARATOR); 291 recordBuffer.append(recordNumber++).append(NVPAIR_SEPARATOR); 292 } 293 294 getNameValuePairs(recordBuffer, record); 295 296 Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent(); 297 if (callFlowAgent != null) { 298 ThreadLocalData tld = callFlowAgent.getThreadLocalData(); 302 if (tld != null) { 303 uniformLogFormat(recordBuffer, tld, level); 304 } 305 } 306 307 recordBuffer.append(FIELD_SEPARATOR); 308 309 String logMessage = record.getMessage(); 310 if (logMessage == null) { 311 logMessage = "The log message is null."; 312 } 313 if( logMessage.indexOf("{0}") >= 0 ) { 314 logMessage = java.text.MessageFormat.format( 317 logMessage, record.getParameters() ); 318 } else { 319 ResourceBundle rb = getResourceBundle(record.getLoggerName( ) ); 320 if( rb != null ) { 321 try { 322 logMessage = MessageFormat.format( 323 rb.getString( logMessage ), 324 record.getParameters( ) ); 325 } catch ( java.util.MissingResourceException e ) { 326 } 329 } 330 } 331 recordBuffer.append(logMessage); 332 333 if (record.getThrown() != null) { 334 recordBuffer.append(LINE_SEPARATOR); 335 StringWriter sw = new StringWriter (); 336 PrintWriter pw = new PrintWriter (sw); 337 record.getThrown().printStackTrace(pw); 338 pw.close(); 339 recordBuffer.append(sw.toString()); 340 } 341 342 recordBuffer.append(RECORD_END_MARKER); 343 return recordBuffer.toString(); 344 345 } catch( Exception ex ) { 346 new ErrorManager ().error( 347 "Error in formatting Logrecord", ex, 348 ErrorManager.FORMAT_FAILURE ); 349 return new String (""); 352 } 353 } 354 355 private synchronized ResourceBundle getResourceBundle( String loggerName ) { 356 if( loggerName == null ) { 357 return null; 358 } 359 ResourceBundle rb = (ResourceBundle ) loggerResourceBundleTable.get( 360 loggerName ); 361 362 if( rb == null ) { 363 rb = logManager.getLogger( loggerName ).getResourceBundle( ); 364 loggerResourceBundleTable.put( loggerName, rb ); 365 } 366 return rb; 367 } 368 } 369 | Popular Tags |