package com.digi.config.util; import com.digi.config.core.*; import java.io.Serializable; /** * This is a helper class for logging messages in the Config system. This class * includes a variety of logging methods to simplify accessing the system log * controller and logging a message. * * The helper methods in this class create and manage a singleton instance of the * SystemLogCtlr. There will be one instance running on each system in the network. * * Since the helper methods are all static, users do not have to have a reference to * the SystemLogCtlr, the static methods grab the singleton instance and use it * directly. * * This class primarily exports simple log methods. For more intricate controller * methods use the getSystemLogCtlr() method to access the singleton. * * For more detailed information on log messages and their use see: * @see SystemLogEntry * */ public class SystemLog { //============================================================================== //= Class Fields //============================================================================== private static SystemLogCtlrImpl systemLogCtlr; // A singleton instance of the log static String defaultRBName= "com.digi.config.resources.ConfigResourceBundle"; //============================================================================== //= Static Methods //============================================================================== /** * Helper to send a simple message to the system log. * * Uses default resource bundle and severity of MSG_LEVEL_INFORMATION. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log */ public static SystemLogEntry log(String msgId) { return log(msgId, (Serializable[]) null); } /** * Helper to send a simple message & associated exception to the system log. * * Uses default resource bundle and severity of MSG_LEVEL_ERROR. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param t the error that occured. The exception information will be attached to the log entry. */ public static SystemLogEntry log(String msgId, Throwable t) { return log(msgId, (Serializable[]) null, t); } /** * Helper to send a parameterized message to the system log. * * Uses default resource bundle and severity of MSG_LEVEL_INFORMATION. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param msgParams parameters available for both the message & the detailed message */ public static SystemLogEntry log(String msgId, Serializable[] msgParams) { // by default, pass the msgParams through as the params for the 2nd level msg as well return log(msgId, msgParams, msgParams); } /** * Helper to send a parameterized message & associated exception to the system log. * * Uses default resource bundle and severity of MSG_LEVEL_ERROR. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param msgParams parameters available for both the message & the detailed message * @param t the error that occured. The exception information will be attached to the log entry. */ public static SystemLogEntry log(String msgId, Serializable[] msgParams, Throwable t) { // by default, pass the msgParams through as the params for the 2nd level msg as well return log(msgId, msgParams, msgParams, t); } /** * Helper to send a parameterized message with detailed text to the system log. * Note: The detailed message is another entry in the message file using the * key of msgId+"_DETAIL" * * Uses default resource bundle and severity of MSG_LEVEL_INFORMATION. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param msgParams parameters for the message * @param detailMsgParams parameters for the detailed message */ public static SystemLogEntry log(String msgId, Serializable[] msgParams, Serializable[] detailMsgParams) { return log(msgId, msgParams, detailMsgParams, SystemLogEntry.MSG_LEVEL_INFORMATION); } /** * Helper to send a parameterized message & associated exception with detailed text to the system log. * Note: The detailed message is another entry in the message file using the * key of msgId+"_DETAIL" * * Uses default resource bundle and severity of MSG_LEVEL_ERROR. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param msgParams parameters for the message * @param detailMsgParams parameters for the detailed message * @param t the error that occured. The exception information will be attached to the log entry. */ public static SystemLogEntry log(String msgId, Serializable[] msgParams, Serializable[] detailMsgParams, Throwable t) { return log(msgId, msgParams, detailMsgParams, SystemLogEntry.MSG_LEVEL_ERROR, t); } /** * Helper to send a parameterized message with detailed text to the system log. * Note: The detailed message is another entry in the message file using the * key of msgId+"_DETAIL" * * Uses default resource bundle and specified severity. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * * @param msgId The Id of the message to send to the log * @param msgParams parameters for the message * @param detailMsgParams parameters for the detailed message * @param severity the severity to assign to this message */ public static SystemLogEntry log(String msgId, Serializable[] msgParams, Serializable[] detailMsgParams, int severity) { return log(msgId, defaultRBName, msgParams, detailMsgParams, severity, null); } /** * Helper to send a parameterized message with detailed text & associated exception to the system log. * Note: The detailed message is another entry in the message file using the * key of msgId+"_DETAIL" * * Uses default resource bundle and specified severity. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * * @param msgId The Id of the message to send to the log * @param msgParams parameters for the message * @param detailMsgParams parameters for the detailed message * @param severity the severity to assign to this message * @param t the error that occured. The exception information will be attached to the log entry. */ public static SystemLogEntry log(String msgId, Serializable[] msgParams, Serializable[] detailMsgParams, int severity, Throwable t) { return log(msgId, defaultRBName, msgParams, detailMsgParams, severity, t); } /** * Helper to send a parameterized message with detailed text to the system log. * Note: The detailed message is another entry in the message file using the * key of msgId+"_DETAIL" * * Uses specified resource bundle and severity. * * Note: If Severity is specified in the message file using the key * of msgId+"_SEVERITY" it will be assigned to the message. * * @param msgId The Id of the message to send to the log * @param msgRbName The name of the resource bundle to locate the message in * @param msgParams parameters for the message * @param detailMsgParams parameters for the detailed message * @param severity the severity to assign to this message */ public static SystemLogEntry log(String msgId, String msgRbName, Serializable[] msgParams, Serializable[] detailMsgParams, int severity, Throwable t) { // If this is a debug message & debug logging is not enabled then do nothing. if ((severity==SystemLogEntry.MSG_LEVEL_DEBUG)&& (!getSystemLogCtlr().isDebugLoggingEnabled())) { return null; } // otherwise, create the entry and log it! SystemLogEntry entry= new SystemLogEntry(msgId, msgParams, detailMsgParams, severity, t); return log(entry); } /** * Helper to send an Exception message to the system log. This message * will be assigned a severity of MSG_LEVEL_ERROR * * @param t The exception or throwable to send to the log */ public static SystemLogEntry log(Throwable t) { return log("GeneralException", new Serializable[] { t.getClass().getName() }, t); } /** * Helper to send a SystemLogEntry to the SystemLogCtlr * * Note that all the other helper methods end up calling this one. It in turn * looks up the SystemLogCtlr singleton and makes to log call on it. * * @param entry The entry to send to the system log */ public static SystemLogEntry log(SystemLogEntry entry) { try { getSystemLogCtlr().add(entry); } catch (Exception e) { System.out.println("Error sending a message to the System Log. Here is the message we were trying to log:"); System.out.println(entry.getMsg()); if (entry.hasDetailedMsg()) { System.out.println(entry.getDetailedMsg()); } System.out.println("Here are the details of the error that prevented us from logging the message:"); e.printStackTrace(); } return entry; } /** * Helper to send a debug message to the system log. * * Uses default resource bundle and severity of DEBUG. * * @param msg The text of the message to include in the debug message. */ public static SystemLogEntry debug(String msg) { Serializable[] msgParams= {"DEBUG["+Thread.currentThread().getName()+"]: "+msg}; SystemLogEntry entry=log("DebugMsg", msgParams); if (ConfigSettings.getBoolean("DebugSettings", "GeneralDebug")) { System.out.println(msgParams[0]); } return entry; } /** * Helper to send a debug message & associated exception to the system log. * * Uses default resource bundle and severity of DEBUG. * * @param msg The text of the message to include in the debug message. * @param t The exception or throwable to send to the log */ public static SystemLogEntry debug(String msg, Throwable t) { Serializable[] msgParams= {"DEBUG["+Thread.currentThread().getName()+"]: "+msg}; SystemLogEntry entry=log("DebugMsg", msgParams, t); if (ConfigSettings.getBoolean("DebugSettings", "GeneralDebug")) { System.out.println(msgParams[0]); if (t!=null) { System.out.println("Detail Message:"+t.getMessage()); t.printStackTrace(); } } return entry; } // /** // * Clears all entries in the SystemLogCtlr // */ // public static void clearLog() throws Exception { // getSystemLogCtlr().removeAll(); // } // // // /** // * Add a ControllerMembershipListener to the listener list. This listener is // * called when items are added or removed from the SystemLogCtlr // * // * @param listener - The ControllerMembershipListener to be added // */ // public static void addLogListener(ControllerMembershipListener listener) { // getSystemLogCtlr().addControllerMembershipListener(listener); // } // // /** // * Remove a ControllerMembershipListener from the listener list. // * // * @param listener - The ControllerMembershipListener to be removed // */ // public static void removeLogListener(ControllerMembershipListener // listener) { // getSystemLogCtlr().removeControllerMembershipListener(listener); // } // // // // /** // * Returns the name of the log file // * // */ // public static String getLogFileName() { // StringSetting logName=(StringSetting)getSystemLogCtlr().getSetting(SystemLogCtlrImpl.LOG_FILE_NAME_KEY); // return logName.getValue(); // } // // /** // * Updates the name of the log file. Closes the old log, creates a new log under // * the new name, and deletes the old log file. // * // * @param fileName The new name for the log file. // */ // public static void setLogFileName(String fileName) { // getSystemLogCtlr().addSetting( // new StringSetting(SystemLogCtlrImpl.LOG_FILE_NAME_KEY, fileName)); // // Finish this... we need to close the old log and create the new... // } // // /** * Returns the SystemLogCtlr singleton, creating it if necessary. * Assumes a server system log is to be created if it does not exist. */ public static SystemLogCtlrImpl getSystemLogCtlr() { if (systemLogCtlr==null) { try { systemLogCtlr=new SystemLogCtlrImpl(SystemLogCtlr.CONFIG_LOG_FILE_NAME_KEY); } catch (Exception e) { System.out.println("Error creating singleton instance of SystemLogCtlr"); e.printStackTrace(); } } return systemLogCtlr; } /** * Returns the SystemLogCtlr singleton, creating it if necessary. * @param resourceKey the resource key that defines the name of the system log file */ public static SystemLogCtlrImpl getSystemLogCtlr(String resourceKey) { if (systemLogCtlr==null) { try { systemLogCtlr=new SystemLogCtlrImpl(resourceKey); } catch (Exception e) { System.out.println("Error creating singleton instance of SystemLogCtlr"); e.printStackTrace(); } } return systemLogCtlr; } }