package com.digi.config.util; import com.digi.config.core.*; import java.io.*; import java.util.Date; /** * Defines the operations and constants associated with the System Log. The * system log maintains a collection of log entries on either the client or * server system and helps manage their life-cycle. The controller also serves * as a factory for new system log entries. * * Defines a Log facility that can be used in both the config client and server * to record error or user messages. Users can register interest in the log * activity. * * @see SystemLog */ public class SystemLogCtlrImpl extends ControllerImpl implements SystemLogCtlr { //========================================================================== //= Class Fields //========================================================================== //========================================================================== //= Instance Fields //========================================================================== /** * Do you want to log the data in a file? (otherwise it is only in the log * area of the gui.) */ private boolean logToFile; /** * The name of the file where the log data is stored. */ private String logFileName; /** * Should logging include debug messages? (All other severities are always * logged.) */ private boolean logDebugMsg; private PrintWriter printWriter; //========================================================================== //= Constructors //========================================================================== /** * Create the System Log * * Note: This constructor is package protected because it should be * constructed by and access through the SystemLog class. The SystemLog * class assures a singleton instance of the controller. * * @see SystemLog */ SystemLogCtlrImpl(String resourceKey) { // Get information about the options logToFile=new Boolean(ConfigSettings.getProperty("LogSettings", LOG_TO_FILE_KEY)).booleanValue(); try { logFileName=ConfigSettings.getProperty("LogSettings", resourceKey); } catch (Exception e) { String prefix = ""; prefix = resourceKey.substring(0,resourceKey.lastIndexOf("LogFileName")); logFileName = prefix+"log.txt"; } logDebugMsg=new Boolean(ConfigSettings.getProperty("LogSettings", LOG_DEBUG_MSG_KEY)).booleanValue(); logDebugMsg=true; // // Get log to file. First look in the setting object. If not // // found there then get the default from the ConfigSettings // BooleanSetting logToFileSetting= // (BooleanSetting)getSetting(LOG_TO_FILE_KEY); // if (logToFileSetting!=null) { // logToFile=logToFileSetting.getValue().booleanValue(); // } // else { // logToFile=new Boolean(ConfigSettings.getProperty("LogSettings", // LOG_TO_FILE_KEY)).booleanValue(); // addSetting( // new BooleanSetting(LOG_TO_FILE_KEY, new Boolean(logToFile))); // } // // // Get the name of the log file. // StringSetting nameSetting= // (StringSetting)getSetting(LOG_FILE_NAME_KEY); // if (nameSetting!=null) { // logFileName=nameSetting.getValue(); // } // else { // try { // logFileName=ConfigSettings.getProperty("LogSettings", // resourceKey); // } // catch (Exception e) { // String prefix = ""; // prefix = resourceKey. // substring(0,resourceKey.lastIndexOf("LogFileName")); // logFileName = prefix+"log.txt"; // } // addSetting(new StringSetting(LOG_FILE_NAME_KEY, logFileName)); // } // // // Get log debug msg // BooleanSetting debugMsgSetting= // (BooleanSetting)getSetting(LOG_DEBUG_MSG_KEY); // if (debugMsgSetting!=null) { // logDebugMsg=debugMsgSetting.getValue().booleanValue(); // } // else { // logDebugMsg=new Boolean(ConfigSettings.getProperty("LogSettings", // LOG_DEBUG_MSG_KEY)).booleanValue(); // addSetting( // new BooleanSetting(LOG_DEBUG_MSG_KEY, new Boolean(logDebugMsg))); // } } //========================================================================== //= Instance Methods //= Note: Since the singleton is private, end users will have no way of //= calling the following instance methods directly. They must call through //= the static helper methods. //========================================================================== /** * Adds a LogEntry to the SystemLog. This method will also write an entry to * the log file if the logToFile flag is enabled. */ public void add(SystemLogEntry item) throws Exception { SystemLogEntry entry=(SystemLogEntry)item; // If it is a debug message, check to see if they are being ignored. if (!logDebugMsg&&entry.getSeverity()==SystemLogEntry.MSG_LEVEL_DEBUG) { return; } // if logDebugMsg is true, then save to file but not on console // TODO more options??? // if (entry.getSeverity()!=SystemLogEntry.MSG_LEVEL_DEBUG) { super.add(item); // } // If this is a proxy to a server logEntry don't log it since it // is already in the server log PrintWriter pw=getPrintWriter(); if (pw!=null) { pw.println(new Date()+"::" +entry.getServiceId()+" - " +entry.getMsg()); pw.println(); if (entry.hasDetailedMsg()) { pw.println(" " +entry.getDetailedMsg()); } SystemLogEntry.ExceptionInfo excInfo = entry.getExceptionInfo(); if (excInfo!=null) { pw.println(" " +excInfo.name); pw.println(" " +excInfo.message); pw.println(" " +excInfo.callStack); } } } /** * Returns a flag indicating if debug messages are currently being * logged */ public boolean isDebugLoggingEnabled() { return this.logDebugMsg; } /** * Returns a reference to the print writer, creating it if necessary. * * Note: if logToFile is false, this method will return null */ private PrintWriter getPrintWriter() { if (!logToFile) { return null; } if (printWriter==null) { // Log has not been setup yet... initialize it now try { // Create the file if necessary and open a print writer to it. String logDirName=System.getProperty("user.home")+File.separatorChar+"logs"; System.out.println("Logging to dir: "+logDirName); File logDir=new File(logDirName); logDir.mkdir(); File logFile=new File(logDir, logFileName); if (logFile.exists()) { logFile.delete(); // only keep log info on latest run or log will get too big } logFile.createNewFile(); printWriter=new PrintWriter( new FileWriter(logDirName+java.io.File.separator+ logFileName, true), true); //Keep appending to the file } catch (IOException e) { // If we get an error trying to open the file, set the // printWriter to the console & spit out an error msg. printWriter=new PrintWriter(System.out); printWriter.println("Unable to open log file '" + logFileName+"'. Sending log data to console instead"); } } return printWriter; } }