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: /*
018:
019: */
020:
021: package org.apache.wsrp4j.log;
022:
023: /**
024: Wrapper class for a log4j logger
025: */
026: public final class LoggerImpl implements Logger {
027: private org.apache.log4j.Logger logger = null;
028:
029: private int level = Logger.ERROR; // default logLevel defined by wsrp4j
030:
031: LoggerImpl(org.apache.log4j.Logger aLogger, int logLevel) {
032: this .logger = aLogger;
033: this .level = logLevel;
034: }
035:
036: public boolean isLogging(int logLevel) {
037: return logLevel <= level;
038: }
039:
040: public void text(int logLevel, String loggingMethod, String text) {
041: text(logLevel, loggingMethod, text, null);
042: }
043:
044: public void text(int logLevel, String loggingMethod, String text,
045: Object parm1) {
046: text(logLevel, loggingMethod, text, new Object[] { parm1 });
047: }
048:
049: public void text(int logLevel, String loggingMethod, String text,
050: Object[] parms) {
051: text(logLevel, loggingMethod, (Throwable) null, text, parms);
052: }
053:
054: public void text(int logLevel, String loggingMethod, Throwable t,
055: String text) {
056: text(logLevel, loggingMethod, t, text, null);
057: }
058:
059: public void text(int logLevel, String loggingMethod, Throwable t,
060: String text, Object[] parms) {
061: //logger.log("text", org.apache.log4j.Level.ERROR, "isLogging return true with level " + logLevel, null);
062:
063: //determine log4j log level
064: org.apache.log4j.Level log4jLevel = determineLog4jLevel(logLevel);
065:
066: //compose message object
067:
068: StringBuffer msgBuffer = new StringBuffer();
069: if (loggingMethod != null) {
070: msgBuffer.append(loggingMethod);
071: msgBuffer.append(" - ");
072: }
073: if (text != null) {
074: msgBuffer.append(text);
075: }
076: if (parms != null) {
077: msgBuffer.append("\nParameters:\n");
078: for (int i = 0; i < parms.length; i++) {
079: msgBuffer.append(parms[i]);
080: }
081: }
082:
083: //log it all
084: logger.log(log4jLevel, msgBuffer.toString(), t);
085: }
086:
087: public void entry(int logLevel, String loggingMethod) {
088: entry(logLevel, loggingMethod, null);
089: }
090:
091: public void entry(int logLevel, String loggingMethod, Object parm1) {
092: entry(logLevel, loggingMethod, new Object[] { parm1 });
093: }
094:
095: public void entry(int logLevel, String loggingMethod, Object[] parms) {
096: text(logLevel, loggingMethod, "Entering method", parms);
097: }
098:
099: public void exit(int logLevel, String loggingMethod) {
100: text(logLevel, loggingMethod, "Exiting method.");
101: }
102:
103: public void exit(int logLevel, String loggingMethod, byte retValue) {
104: exit(logLevel, loggingMethod, new Byte(retValue));
105: }
106:
107: public void exit(int logLevel, String loggingMethod, short retValue) {
108: exit(logLevel, loggingMethod, new Short(retValue));
109: }
110:
111: public void exit(int logLevel, String loggingMethod, int retValue) {
112: exit(logLevel, loggingMethod, new Integer(retValue));
113: }
114:
115: public void exit(int logLevel, String loggingMethod, long retValue) {
116: exit(logLevel, loggingMethod, new Long(retValue));
117: }
118:
119: public void exit(int logLevel, String loggingMethod, float retValue) {
120: exit(logLevel, loggingMethod, new Float(retValue));
121: }
122:
123: public void exit(int logLevel, String loggingMethod, double retValue) {
124: exit(logLevel, loggingMethod, new Double(retValue));
125: }
126:
127: public void exit(int logLevel, String loggingMethod, char retValue) {
128: exit(logLevel, loggingMethod, new Character(retValue));
129: }
130:
131: public void exit(int logLevel, String loggingMethod,
132: boolean retValue) {
133: exit(logLevel, loggingMethod, Boolean.valueOf(retValue));
134: }
135:
136: public void exit(int logLevel, String loggingMethod, Object retValue) {
137: text(logLevel, loggingMethod,
138: "Exiting method. Returned value: {0}", retValue);
139: }
140:
141: public void stackTrace(int logLevel, String loggingMethod,
142: String text) {
143: text(logLevel, loggingMethod, new Throwable("Stacktrace"), text);
144: }
145:
146: //returns log4j level
147: private org.apache.log4j.Level determineLog4jLevel(int wsrp4jLevel) {
148: if (wsrp4jLevel < Logger.ERROR) {
149: return org.apache.log4j.Level.OFF;
150: } else if (wsrp4jLevel < Logger.WARN) {
151: return org.apache.log4j.Level.ERROR;
152: } else if (wsrp4jLevel < Logger.INFO) {
153: return org.apache.log4j.Level.WARN;
154: } else if (wsrp4jLevel < Logger.TRACE_LOW) {
155: return org.apache.log4j.Level.INFO;
156: } else if (wsrp4jLevel < Logger.TRACE_HIGH) {
157: return org.apache.log4j.Level.DEBUG;
158: } else {
159: return org.apache.log4j.Level.DEBUG;
160: }
161: }
162: }
|