001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.wsrp4j.log;
018:
019: import java.io.InputStream;
020: import java.util.Properties;
021:
022: /**
023: Logger factory return an appropriate implementation of a Logger
024: */
025: public class LogManager {
026:
027: private static final String LOG_PROPERTIES_FILE = "wsrp-logger.properties";
028:
029: private static final String PROPERTY_PREFIX = "wsrp4j.logger.";
030:
031: private static final String GLOBAL_LOG_LEVEL_PROPERTY = "wsrp4j.logger";
032:
033: private static int globalLogLevel = Logger.ERROR;
034:
035: private static Properties wsrp4jLevels = new Properties();
036:
037: private static LogManager logManager = new LogManager();
038:
039: /**
040: private constructor prohibits sub-classing
041: */
042: private LogManager() {
043: try {
044: InputStream propertiesInputStream;
045: propertiesInputStream = this .getClass().getClassLoader()
046: .getResourceAsStream(LOG_PROPERTIES_FILE);
047: wsrp4jLevels.load(propertiesInputStream);
048: } catch (Exception e) {
049: System.err
050: .println("LogManager: Error while initializing log properties. StackTrace follows.");
051: e.printStackTrace(System.err);
052: }
053: try {
054: //load log4j configuration from properties
055: org.apache.log4j.PropertyConfigurator
056: .configure(wsrp4jLevels);
057:
058: //log used properties file
059: org.apache.log4j.Logger.getLogger(this .getClass()).info(
060: "Logger.properties file: " + LOG_PROPERTIES_FILE);
061:
062: //initialize wsrp4j-logger levels from properties
063: int userDefinedGlobalLevel = getLevelFromString(wsrp4jLevels
064: .getProperty(GLOBAL_LOG_LEVEL_PROPERTY));
065: if (userDefinedGlobalLevel != 0) {
066: globalLogLevel = userDefinedGlobalLevel;
067: }
068:
069: //log global wsrp4jlog level
070: org.apache.log4j.Logger.getLogger(this .getClass()).info(
071: "Global log level: "
072: + this .getLevelAsString(globalLogLevel));
073:
074: } catch (Exception e) {
075: System.err
076: .println("LogManager: Error while initializing Logger.");
077: // StackTrace follows.");
078: //e.printStackTrace(System.err);
079: }
080: }
081:
082: /**
083: Returns the LogManager instance
084:
085: @return LogManager instance
086: */
087: public static LogManager getLogManager() {
088: return logManager;
089: }
090:
091: /**
092: Returns a Logger instance for the calling class
093:
094: @param aClass The class that requests the logger instance.
095: @return instance of <CODE>Logger</CODE>.
096: */
097: public Logger getLogger(Class aClass) {
098: //create a Logger
099: org.apache.log4j.Logger logger = org.apache.log4j.Logger
100: .getLogger(aClass);
101:
102: //determine log level for aClass
103: String propertyName = PROPERTY_PREFIX + aClass.getName();
104: int level = globalLogLevel;
105: int userDefinedLevel = getLevelFromString(wsrp4jLevels
106: .getProperty(propertyName));
107: if (userDefinedLevel != 0) {
108: level = userDefinedLevel;
109: }
110: return new LoggerImpl(logger, level);
111: }
112:
113: /*
114: Returns an int LogLevel defined by a String {ERROR, WARN, INFO, TRACE_HIGH, TRACE_MEDIUM, TRACE_LOW}.
115: Use this method to convert levels defined in a properties file
116: If the string does not represent a correct level this method returns 0
117: */
118: private int getLevelFromString(String s) {
119: if (s != null) {
120: if (s.equalsIgnoreCase("ERROR")) {
121: return Logger.ERROR;
122: } else if (s.equalsIgnoreCase("WARN")) {
123: return Logger.WARN;
124: } else if (s.equalsIgnoreCase("INFO")) {
125: return Logger.INFO;
126: } else if (s.equalsIgnoreCase("TRACE_LOW")) {
127: return Logger.TRACE_LOW;
128: } else if (s.equalsIgnoreCase("TRACE_MEDIUM")) {
129: return Logger.TRACE_MEDIUM;
130: } else if (s.equalsIgnoreCase("TRACE_HIGH")) {
131: return Logger.TRACE_HIGH;
132: } else {
133: return 0;
134: }
135: } else {
136: return 0;
137: }
138: }
139:
140: /*
141: Translates a int logLevel to a String
142: */
143: private String getLevelAsString(int level) {
144: String sLevel;
145: if (level < Logger.WARN) {
146: sLevel = "ERROR";
147: } else if (level < Logger.INFO) {
148: sLevel = "WARN";
149: } else if (level < Logger.TRACE_LOW) {
150: sLevel = "INFO";
151: } else if (level < Logger.TRACE_MEDIUM) {
152: sLevel = "TRACE_LOW";
153: } else if (level < Logger.TRACE_HIGH) {
154: sLevel = "TRACE_MEDIUM";
155: } else {
156: sLevel = "TRACE_HIGH";
157: }
158: return sLevel;
159: }
160: }
|