001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: LogRecord.java,v 1.7 2009/03/05 22:55:37 veiming Exp $ 026 * 027 */ 028 029package com.sun.identity.log; 030 031import com.iplanet.sso.SSOException; 032import com.sun.identity.log.spi.Debug; 033import java.text.SimpleDateFormat; 034import java.util.Date; 035import java.util.HashMap; 036import java.util.Hashtable; 037import java.util.Map; 038import java.util.logging.Level; 039 040/** 041 * Extension to the JDK1.4 <code>LogRecord</code> to include the 042 * <code>logInfo</code> <code>HashMap</code> and methods to store and retrieve 043 * data from this <code>logInfo</code> Map. The <code>logInfo</code> Map is 044 * supposed to be used by the client to fill in log-details which 045 * will be used by the Formatter to construct the actual log string. 046 * 047 * For <code>JDK1.4</code> <code>LogRecord</code> please refer to 048 * <pre> 049 * http://java.sun.com/j2se/1.4.1/docs/api/java/util/logging/LogRecord.html 050 * </pre> 051 * @supported.api 052 */ 053public class LogRecord extends java.util.logging.LogRecord 054 implements ILogRecord 055{ 056 private Map logInfoMap = new HashMap(); 057 private Object token; 058 059 /** 060 * Construct the <code>LogRecord</code> with the given Level and message 061 * values. 062 * 063 * @param level The log Level 064 * @param msg The message string 065 * 066 * @supported.api 067 */ 068 public LogRecord(Level level, String msg) { 069 super(level,msg); 070 } 071 072 /** 073 * Construct the <code>LogRecord</code> with the given Level and message 074 * values. 075 * 076 * @param level The log Level. 077 * @param msg The message string. 078 * @param token The single sign-on token which will be used to fill in 079 * details like client IP address into the <code>LogRecord</code>. 080 * @supported.api 081 */ 082 public LogRecord(Level level, String msg, Object token) { 083 this(level,msg); 084 this.token = token; 085 086 try { 087 Logger.extractInfoFromLogFor(this); 088 } catch (SSOException se) { 089 /* 090 * internal auth session doesn't have IPaddr, so stacktrace 091 * was filling up amLog debug file. 092 */ 093 Debug.error("LogRecord:LogRecord:SSOException: " + se.getMessage()); 094 } 095 } 096 097 /** 098 * Constructor for auth logging 099 * @param level The log Level. 100 * @param msg The message string. 101 * @param properties The Hashtable containing the properties 102 * for the LogRecord. 103 */ 104 105 public LogRecord(Level level, String msg, Hashtable properties) { 106 this(level,msg); 107 String clientDomain = (String)properties.get(LogConstants.DOMAIN); 108 String clientID = (String)properties.get(LogConstants.LOGIN_ID); 109 String ipAddress = (String)properties.get(LogConstants.IP_ADDR); 110 String loginIDSid = (String)properties.get(LogConstants.LOGIN_ID_SID); 111 String moduleName = (String)properties.get(LogConstants.MODULE_NAME); 112 String contextID = (String)properties.get(LogConstants.CONTEXT_ID); 113 String messageID = (String)properties.get(LogConstants.MESSAGE_ID); 114 String nameID = (String)properties.get(LogConstants.NAME_ID); 115 String hostName = ipAddress; 116 if (ipAddress != null) { 117 try { 118 if (Logger.resolveHostName) { 119 hostName = 120 java.net.InetAddress.getByName(ipAddress).getHostName(); 121 } else { 122 hostName = ipAddress; 123 } 124 } catch (Exception e) { 125 Debug.error("LogRecord:LogRecord:Unable to get Host for:" + 126 ipAddress); 127 } 128 } 129 Date date = new Date(); 130 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 131 /* 132 * these are the compulsory fields ... to be logged even if there are 133 * exceptions while getting domain, loginid, ipaddr, hostname 134 */ 135 addLogInfo(LogConstants.TIME, sdf.format(date)); 136 addLogInfo(LogConstants.DATA, getMessage()); 137 addLogInfo(LogConstants.LOG_LEVEL, getLevel().toString()); 138 addLogInfo(LogConstants.DOMAIN, clientDomain); 139 addLogInfo(LogConstants.LOGIN_ID, clientID); 140 addLogInfo(LogConstants.IP_ADDR, ipAddress); 141 addLogInfo(LogConstants.HOST_NAME, hostName); 142 addLogInfo(LogConstants.LOGIN_ID_SID, loginIDSid); 143 addLogInfo(LogConstants.MODULE_NAME, moduleName); 144 /* if they're implemented... */ 145 if ((messageID != null) && (messageID.length() > 0)) { 146 addLogInfo(LogConstants.MESSAGE_ID, messageID); 147 } 148 if ((contextID != null) && (contextID.length() > 0)) { 149 addLogInfo(LogConstants.CONTEXT_ID, contextID); 150 } 151 if ((nameID != null) && (nameID.length() > 0)) { 152 addLogInfo(LogConstants.NAME_ID, nameID); 153 } 154 } 155 156 /** 157 * Adds to the log information map, the field key and its corresponding 158 * value. 159 * 160 * @param key The key which will be used by the formatter to determine if 161 * this piece of info is supposed to be added to the log string 162 * according to the selected log fields. 163 * @param value The value which may form a part of the actual log-string. 164 * @supported.api 165 */ 166 public void addLogInfo(String key,Object value) { 167 logInfoMap.put(key,value); 168 } 169 170 /** 171 * Convenience method to set the log information map. 172 * 173 * @param logInfoMap Handler to the map which contains the log info 174 * @supported.api 175 */ 176 public void setLogInfoMap(Map logInfoMap) { 177 this.logInfoMap = logInfoMap; 178 } 179 /** 180 * Returns the log information map which contains the set of fields and 181 * their corresponding values. 182 * 183 * @return The log information map. 184 * @supported.api 185 */ 186 public Map getLogInfoMap() { 187 return logInfoMap; 188 } 189 190 /** 191 * Returns log by subject. 192 * 193 * @return log by subject. 194 */ 195 public Object getLogBy() { 196 return null; 197 } 198 199 /** 200 * Returns log for subject. 201 * 202 * @return log for subject. 203 */ 204 public Object getLogFor() { 205 return token; 206 } 207 208}