1 19 package org.openharmonise.rm.logging; 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 import java.util.logging.*; 25 26 import org.openharmonise.rm.config.*; 27 28 29 38 public class XMLEventLogger 39 extends AbstractEventLogger 40 implements EventLogger { 41 42 45 private static Logger m_logger = 46 Logger.getLogger(XMLEventLogger.class.getName()); 47 48 51 private static FileHandler fh = null; 52 53 57 private static String PNAME_LOG_FILENAME = "EVENT_LOG_FILE"; 58 59 62 public static final String TAG_USER_ID = "UserId"; 63 64 67 public static final String TAG_SESSION_ID = "SessionId"; 68 69 72 public static final String TAG_TIMESTAMP = "Timestamp"; 73 74 77 public static final String TAG_OBJECT = "EventObject"; 78 79 82 public static final String ATTRIB_OBJECT_ID = "object_id"; 83 84 87 public static final String TAG_LABEL = "Label"; 88 89 92 public static final String TAG_IP_ADDRESS = "IPAddress"; 93 94 97 public static final String TAG_HTTP_HEADERS = "HTTPHeaders"; 98 99 102 public static final String ATTRIB_HEADER_NAME = "name"; 103 104 107 public static final String TAG_HTTP_PARAMETERS = "HTTPParameter"; 108 109 112 private SimpleDateFormat date_formatter = 113 new SimpleDateFormat("MM-dd-yyyy HH:mm:ss.SSS"); 114 private static final String TAG_ADDITIONAL = "AdditionalData"; 115 116 119 public XMLEventLogger() throws LogException { 120 super(); 121 122 try { 123 fh = 124 new FileHandler( 125 ConfigSettings.getProperty( 126 PNAME_LOG_FILENAME)); 127 128 fh.setFormatter(new HarmoniseFormatter()); 129 130 m_logger.addHandler(fh); 131 132 m_logger.setLevel(LogLevel.HARMONISE_EVENT); 133 134 } catch (SecurityException e) { 135 throw new LogException( 136 "security exception initialising log file", 137 e); 138 } catch (ConfigException e) { 139 throw new LogException("config exception initialising log file", e); 140 } catch (IOException e) { 141 throw new LogException("io exception initialising log file", e); 142 } 143 144 } 145 146 149 protected void saveData( 150 int nUserId, 151 String sSessionId, 152 int nObjectId, 153 String sObjectType, 154 String sAction, 155 Date timestamp, 156 String sIP, 157 Map headers) 158 throws LogException { 159 StringBuffer strbuf = new StringBuffer (); 160 161 addTag(strbuf, TAG_USER_ID, nUserId, null); 162 addTag(strbuf, TAG_SESSION_ID, sSessionId, null); 163 addTag(strbuf, TAG_TIMESTAMP, timestamp, null); 164 Hashtable attrs = new Hashtable(); 165 attrs.put(ATTRIB_OBJECT_ID, new Integer (nObjectId)); 166 addTag(strbuf, TAG_OBJECT, sObjectType, attrs); 167 addTag(strbuf, TAG_LABEL, sAction, null); 168 addTag(strbuf, TAG_IP_ADDRESS, sIP, null); 169 addHeaderTags(strbuf, headers); 170 171 m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString()); 172 } 173 174 185 private void addTag( 186 StringBuffer strbuf, 187 String sTagname, 188 int nTagContent, 189 Map attrs) { 190 addTag(strbuf, sTagname, String.valueOf(nTagContent), attrs); 191 } 192 193 203 private void addTag( 204 StringBuffer strbuf, 205 String sTagname, 206 Date date, 207 Map attrs) { 208 String sDate = date_formatter.format(date); 209 210 addTag(strbuf, sTagname, sDate, attrs); 211 } 212 213 223 private void addTag( 224 StringBuffer strbuf, 225 String sTagname, 226 String sTagContent, 227 Map attrs) { 228 strbuf.append(" <").append(sTagname); 229 230 if (attrs != null) { 231 Iterator iter = attrs.keySet().iterator(); 232 233 while (iter.hasNext()) { 234 String sKey = (String ) iter.next(); 235 236 strbuf.append(" ").append(sKey).append("=\""); 237 strbuf.append(attrs.get(sKey)).append("\""); 238 } 239 } 240 241 strbuf.append(">"); 242 strbuf.append(sTagContent); 243 strbuf.append("</").append(sTagname).append(">\n"); 244 } 245 246 255 private void addHeaderTags(StringBuffer strbuf, Map headers) { 256 StringBuffer headerTags = new StringBuffer (); 257 258 if (headers != null) { 259 260 Iterator iter = headers.keySet().iterator(); 261 262 while (iter.hasNext()) { 263 String sHeaderName = (String ) iter.next(); 264 String sHeaderValue = (String ) headers.get(sHeaderName); 265 Hashtable attrs = new Hashtable(); 266 attrs.put(ATTRIB_HEADER_NAME, sHeaderName); 267 addTag(headerTags, TAG_HTTP_PARAMETERS, sHeaderValue, attrs); 268 } 269 } 270 271 addTag( strbuf, TAG_HTTP_PARAMETERS, headerTags.toString(), null); 272 } 273 274 277 protected void saveData(int nUserId, String sSessionId, int nObjectId, String sObjectType, String sAction, Date timestamp, String sIP, String sAdditional) throws LogException { 278 StringBuffer strbuf = new StringBuffer (); 279 280 addTag(strbuf, TAG_USER_ID, nUserId, null); 281 addTag(strbuf, TAG_SESSION_ID, sSessionId, null); 282 addTag(strbuf, TAG_TIMESTAMP, timestamp, null); 283 Hashtable attrs = new Hashtable(); 284 attrs.put(ATTRIB_OBJECT_ID, new Integer (nObjectId)); 285 addTag(strbuf, TAG_OBJECT, sObjectType, attrs); 286 addTag(strbuf, TAG_LABEL, sAction, null); 287 addTag(strbuf, TAG_IP_ADDRESS, sIP, null); 288 addTag(strbuf, TAG_ADDITIONAL, sAdditional,null); 289 290 m_logger.log(LogLevel.HARMONISE_EVENT, strbuf.toString()); 291 292 } 293 294 } 295 | Popular Tags |