1 23 24 package com.sun.enterprise.tools.admingui; 25 26 27 import java.util.logging.Formatter ; 28 import java.util.logging.LogRecord ; 29 import java.util.logging.LogManager ; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 import java.util.logging.ErrorManager ; 33 import java.util.ResourceBundle ; 34 import java.util.HashMap ; 35 import java.util.Date ; 36 import java.util.Map ; 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 63 64 public class PlainFormatter extends Formatter { 65 private HashMap loggerResourceBundleTable; 68 private LogManager logManager; 69 private Date date = new Date ( ); 71 private static String PRODUCTID_CONTEXTID = null; 72 private static final String PRODUCT_VERSION = 76 com.sun.appserv.server.util.Version.getAbbreviatedVersion(); 77 private static final int FINE_LEVEL_INT_VALUE = Level.FINE.intValue(); 78 79 80 81 private static final String LINE_SEPARATOR = 82 (String ) java.security.AccessController.doPrivileged( 83 new sun.security.action.GetPropertyAction("line.separator")); 84 85 private static final String RECORD_BEGIN_MARKER = "[#|"; 86 private static final String RECORD_END_MARKER = "|#]" + LINE_SEPARATOR + 87 LINE_SEPARATOR; 88 private static final String FIELD_SEPARATOR = "|"; 89 private static final String NVPAIR_SEPARATOR = ";"; 90 91 private static final String RFC_3339_DATE_FORMAT = 92 "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; 93 94 private static final SimpleDateFormat dateFormatter = 95 new SimpleDateFormat ( RFC_3339_DATE_FORMAT ); 96 97 public PlainFormatter() { 98 super( ); 99 loggerResourceBundleTable = new HashMap ( ); 100 logManager = LogManager.getLogManager( ); 101 } 102 103 104 109 110 111 public String format( LogRecord record ) { 112 return uniformLogFormat( record ); 113 } 114 115 public String formatMessage( LogRecord record ) { 116 return uniformLogFormat( record ); 117 } 118 119 120 123 protected String getProductId( ) { 124 return PRODUCT_VERSION; 125 } 126 127 128 132 protected StringBuffer getNameValuePairs( LogRecord record ) { 133 Object [] parameters = record.getParameters( ); 134 StringBuffer namevaluePairs = new StringBuffer (""); 135 try { 136 if( ( parameters == null ) 137 ||( parameters.length == 0 ) ) 138 { 139 return namevaluePairs; 140 } 141 int lastElement = parameters.length - 1; 142 if( parameters[lastElement] instanceof java.util.Map ) { 143 Iterator iterator = ((Map )(parameters[lastElement])).entrySet( 144 ).iterator( ); 145 while( iterator.hasNext( ) ) { 146 Map.Entry entry = (Map.Entry ) iterator.next(); 147 namevaluePairs.append( entry.getKey() + "=" + 148 entry.getValue() + NVPAIR_SEPARATOR ); 149 } 150 } 151 } catch( Exception e ) { 152 new ErrorManager ().error( 153 "Error in extracting Name Value Pairs", e, 154 ErrorManager.FORMAT_FAILURE ); 155 } 156 return namevaluePairs; 157 } 158 159 163 private String uniformLogFormat( LogRecord record ) { 164 try { 165 StringBuffer recordBuffer = new StringBuffer (); 166 173 String logMessage = record.getMessage(); 182 ResourceBundle rb = getResourceBundle( record.getLoggerName( ) ); 183 if( rb != null ) 184 { 185 try { 186 logMessage = MessageFormat.format( 187 rb.getString( logMessage ), 188 record.getParameters( ) ); 189 } catch ( java.util.MissingResourceException e ) { 190 } 193 } 194 recordBuffer.append( logMessage ); 195 196 if (record.getThrown() != null) { 197 recordBuffer.append( LINE_SEPARATOR ); 198 StringWriter sw = new StringWriter (); 199 PrintWriter pw = new PrintWriter (sw); 200 record.getThrown().printStackTrace(pw); 201 pw.close(); 202 recordBuffer.append(sw.toString()); 203 } 204 recordBuffer.append( LINE_SEPARATOR ); 205 206 return recordBuffer.toString( ); 207 } catch( Exception ex ) { 208 new ErrorManager ().error( 209 "Error in formatting Logrecord", ex, 210 ErrorManager.FORMAT_FAILURE ); 211 return new String (""); 214 } 215 } 216 217 private synchronized ResourceBundle getResourceBundle( String loggerName ) { 218 if( loggerName == null ) { 219 return null; 220 } 221 ResourceBundle rb = (ResourceBundle ) loggerResourceBundleTable.get( 222 loggerName ); 223 224 if( rb == null ) { 225 rb = logManager.getLogger( loggerName ).getResourceBundle( ); 226 loggerResourceBundleTable.put( loggerName, rb ); 227 } 228 return rb; 229 } 230 } 231 232 233 | Popular Tags |