001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import java.io.FileInputStream;
023: import java.util.logging.LogManager;
024:
025: import com.salmonllc.properties.Props;
026: import com.salmonllc.sql.DBStatement;
027:
028: /**
029: * Concrete implementation of the logger class that logs using the JDK built in logging<br>
030: * To have the framework use JDK built in logging instead of SOFIA standard:<br>
031: * 1) Make sure you are using JDK 1.4 or above.<br>
032: * 2) Import this class into the com.salmonllc.util package<br>
033: * 3) Set the property in your System.properties: MessageLogLoggerClass=com.salmonllc.util.LoggerJDK<br>
034: * 4) Optional, if you want to have the JDK Logger use something other then the default configuration set the property in your System.properties: MessageLogLoggerConfigFile=JDK 1.4 Logging Config File (see the docs on JDK 1.4 logging for information on what to put in this file)
035: */
036:
037: public class LoggerJDK implements com.salmonllc.util.Logger {
038:
039: /**
040: * Log a message to the appropriate message logging package
041: * @param message The message to log
042: * @param type The type, see TYPE constants at the top of the page
043: * @param level The error level
044: * @param e an error message
045: * @param o the object logging the message
046: */
047:
048: private java.util.logging.Logger[] _typeToLoggerMap = new java.util.logging.Logger[5];
049: private java.util.logging.Level[] _typeToLevelMap = new java.util.logging.Level[5];
050:
051: public LoggerJDK() {
052: Props p = Props.getSystemProps();
053: String configFile = p
054: .getProperty(Props.SYS_MESSAGELOG_LOGGER_CONFIG_FILE);
055: if (configFile != null) {
056: FileInputStream fin;
057: try {
058: fin = new FileInputStream(configFile);
059: LogManager.getLogManager().readConfiguration(fin);
060: fin.close();
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064: }
065:
066: _typeToLoggerMap[TYPE_ASSERTION] = java.util.logging.Logger
067: .getLogger("com.salmonllc.util.MessageLog.Assertion");
068: _typeToLoggerMap[TYPE_ERROR] = java.util.logging.Logger
069: .getLogger("com.salmonllc.util.MessageLog.Error");
070: _typeToLoggerMap[TYPE_INFO] = java.util.logging.Logger
071: .getLogger("com.salmonllc.util.MessageLog.Info");
072: _typeToLoggerMap[TYPE_SQL] = java.util.logging.Logger
073: .getLogger("com.salmonllc.util.MessageLog.SQL");
074: _typeToLoggerMap[TYPE_DEBUG] = java.util.logging.Logger
075: .getLogger("com.salmonllc.util.MessageLog.Debug");
076:
077: _typeToLevelMap[TYPE_ASSERTION] = java.util.logging.Level.SEVERE;
078: _typeToLevelMap[TYPE_ERROR] = java.util.logging.Level.SEVERE;
079: _typeToLevelMap[TYPE_INFO] = java.util.logging.Level.INFO;
080: _typeToLevelMap[TYPE_SQL] = java.util.logging.Level.INFO;
081: _typeToLevelMap[TYPE_DEBUG] = java.util.logging.Level.FINE;
082: }
083:
084: public void log(String message, int type, int level, Throwable e,
085: Object o) {
086: java.util.logging.Logger jdkLogger = _typeToLoggerMap[type];
087: java.util.logging.Level jdkLevel = _typeToLevelMap[type];
088:
089: if (jdkLogger.isLoggable(jdkLevel)) {
090: //find the calling class and method
091: Throwable dummyException = new Throwable();
092: StackTraceElement locations[] = dummyException
093: .getStackTrace();
094: String cname = "unknown";
095: String method = "unknown";
096: if (locations != null) {
097: String className = MessageLog.class.getName() + ";"
098: + DBStatement.class.getName();
099: boolean next = false;
100: for (int i = 0; i < locations.length; i++) {
101: if (next) {
102: if (className.indexOf(locations[i]
103: .getClassName()) == -1) {
104: cname = locations[i].getClassName();
105: method = locations[i].getMethodName();
106: break;
107: }
108: } else if (className.indexOf(locations[i]
109: .getClassName()) != -1)
110: next = true;
111: }
112: }
113: //call the JDK logger and log the message
114: if (e == null)
115: jdkLogger.logp(jdkLevel, cname, method, message);
116: else
117: jdkLogger.logp(jdkLevel, cname, method, message, e);
118: }
119: }
120: }
|