package com.digi.config.util; import com.digi.config.core.ControlledItem; import com.digi.config.core.ControlledItemImpl; import java.io.*; import java.util.*; import java.text.*; /** * Defines an entry in the system log. Log entries are compound messages. At a minimum, * they include a primary text message. They can optionally include a detail message * a severity, and a service ID. * * Although it is possible for users to create SystemLogEntry's directly, typically * users will use the static helper methods found in the SystemLog class. See the * SystemLog class for details on how to do logging from a program. * * When SystemLogEntry objects are created users pass in a msg key. This key is used * to look up the primary text from the message property file. The msg key is also * used to look up the associated detail text, severity and service ID. The * associated field keys are produced by appending the following tags to the primary * msg key: * _DETAILS - key to the second level detailed text message * _SEVERITY - key to the message severity (see below for valid values) * _ID - key to the message service id. * * The associated fields are optional, if they are not found in the message file, * then they will default to empty or (in the case of severity ) to the value passed * in on the constructor. * * Here is an example of a message stored in the Message file: * MyMsg = My normal parameterized {0} message. * MyMsg_DETAILS = My detailed parameterized {0} message that includes all the gory details \nand potential user actions. * MyMsg_SEVERITY = 1 * MyMsg_ID = common_023 * * * The potential severity values are: * 0 DEBUG * 1 INFORMATION * 2 WARNING * 3 ERROR * 4 FATAL * * Detail messages can be multi-line with the \n seperator char delineating lines. * * * See the following classes for more information: * SystemLog * SystemLogEntry * SystemLogCtlr * */ public class SystemLogEntry extends ControlledItemImpl implements ControlledItem { //============================================================================== //= Severity Constants //============================================================================== public final static int MSG_LEVEL_DEBUG =0; public final static int MSG_LEVEL_INFORMATION=1; public final static int MSG_LEVEL_WARNING =2; public final static int MSG_LEVEL_ERROR =3; public final static int MSG_LEVEL_FATAL =4; private final static long serialVersionUID=4795284070894478266L; //============================================================================== //= Instance Fields //============================================================================== /** The first level message text */ private String baseMsg; /** The second level message */ private String detailedMsg; /** The message severity */ private int severity; /** The message service ID */ private String serviceID; /** Exception Information */ private ExceptionInfo exceptionInfo; /** Time that the Entry was created */ private Calendar creationTime; /** The system that the Entry was created on */ private String systemName; /** * Constructs a SystemLogEntry from a resource message and the specified * message parameters. * * If the resource bundle contains the optional detailed message then * that message will be created and attached to this entry. * * If the resource bundle contains the optional severity message then * it will be extracted and will override an passed in severity for this entry. * * @param msgId The id of the message in the resource bundle * @param msgParams The parameters of the message * @param detailedMsgParams The parameters of the optional detailed message. * @param severity The severity assigned to the entry */ public SystemLogEntry(String msgId, Serializable[] msgParams, Serializable[] detailedMsgParams, int severity) throws MissingResourceException { this(msgId, msgParams, detailedMsgParams, severity, null); } /** * Constructs a SystemLogEntry from a resource message and the specified * message parameters. * * If the resource bundle contains the optional detailed message then * that message will be created and attached to this entry. * * If the resource bundle contains the optional severity message then * it will be extracted and will override an passed in severity for this entry. * * @param msgId The id of the message in the resource bundle * @param msgParams The parameters of the message * @param detailedMsgParams The parameters of the optional detailed message. * @param severity The severity assigned to the entry * @param t the exception info to be attached to the log entry (may be null) */ public SystemLogEntry(String msgId, Serializable[] msgParams, Serializable[] detailedMsgParams, int severity, Throwable t) throws MissingResourceException { // Get the base message form the msgRb baseMsg = getLocalizedMessage(msgId, msgParams, null); // Get the severity from the msgRb try { severity=Integer.parseInt(ConfigResource.getMsgRbStringForce(msgId+"_SEVERITY")); } catch (MissingResourceException e) { // no severity in the msgRb... default to passed in severity } catch (Exception e) { // bad value in the resource file, warn and then default to passed in severity SystemLog.log("InvalidResource", new Serializable[] { msgId+"_SEVERITY"}); } this.severity=severity; // Get the serviceID from the msgRb try { serviceID=ConfigResource.getMsgRbStringForce(msgId+"_ID"); } catch (MissingResourceException e) { // no serviceID in the msgRb... default to blank serviceID=""; } // Get the detailed message from the msgRb try { if (ConfigResource.getMsgRbStringForce(msgId+"_DETAILS")!=null) { detailedMsg=getLocalizedMessage(msgId+"_DETAILS", detailedMsgParams, null); } } catch (MissingResourceException e) { // If it doesn't exist thats OK... this is an optional message. detailedMsg=null; } // If exception information was passed in, then record it if (t!=null) { exceptionInfo=new ExceptionInfo(t); } // Record the creation time creationTime=Calendar.getInstance(); // Record the system name try { systemName=java.net.InetAddress.getLocalHost().getHostName(); } catch (java.net.UnknownHostException e) { systemName="Unknown"; } } static private String getLocalizedMessage(String msgId, Serializable[] msgParams, Locale locale) { String formatString = ConfigResource.getMsgRbString(msgId); MessageFormat messageFormat = new MessageFormat(formatString); StringBuffer result = new StringBuffer(); if (locale==null) { locale = Locale.getDefault(); } messageFormat.setLocale(locale); messageFormat.format(msgParams, result, new FieldPosition(0)); return result.toString(); } /** * Returns the localized primary message text for this entry */ public String getMsg() { // String name = systemName+" "; // return name.substring(0,11)+" "+baseMsg.getLocalizedText(null); return baseMsg; } /** * Returns the localized 2nd level message text for this entry */ public String getDetailedMsg() { if (detailedMsg!=null) { return detailedMsg; } return null; } /** * Returns the severity associated with this entry */ public int getSeverity() { return severity; } /** * Indicates if there is a detailed messages within this entry */ public boolean hasDetailedMsg() { return(detailedMsg!=null); } /** * Return the SystemLogEntry text */ public String toString() { return "SystemLogEntry = " +getMsg()+"::" +getDetailedMsg(); } /** * Returns any associated exception information */ public ExceptionInfo getExceptionInfo() { return exceptionInfo; } /** * Returns the time the log entry was created */ public Calendar getCreationTime() { return creationTime; } /** * Returns the name of the system the log entry was created on */ public String getSystemName() { return systemName; } /** * Returns the service ID associated with the log message */ public String getServiceId() { return this.serviceID; } /** * Contains localized and serializable information about an exception that occured */ public class ExceptionInfo implements Serializable { public String name; public String message; public String callStack; public ExceptionInfo(Throwable t) { name=t.getClass().getName(); message=t.getMessage(); StringWriter sw=new StringWriter(); PrintWriter pw=new PrintWriter(sw); t.printStackTrace(pw); callStack=sw.getBuffer().toString(); } } }