1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /*
18  
19   */
20  
21  package org.apache.wsrp4j.log;
22  
23  /**
24   Wrapper class for a log4j logger
25   */
26  public final class LoggerImpl implements Logger {
27      private org.apache.log4j.Logger logger = null;
28  
29      private int level = Logger.ERROR; // default logLevel defined by wsrp4j
30  
31      LoggerImpl(org.apache.log4j.Logger aLogger, int logLevel) {
32          this.logger = aLogger;
33          this.level = logLevel;
34      }
35  
36      public boolean isLogging(int logLevel) {
37          return logLevel <= level;
38      }
39  
40      public void text(int logLevel, String loggingMethod, String text) {
41          text(logLevel, loggingMethod, text, null);
42      }
43  
44      public void text(int logLevel, String loggingMethod, String text,
45              Object parm1) {
46          text(logLevel, loggingMethod, text, new Object[] { parm1 });
47      }
48  
49      public void text(int logLevel, String loggingMethod, String text,
50              Object[] parms) {
51          text(logLevel, loggingMethod, (Throwable) null, text, parms);
52      }
53  
54      public void text(int logLevel, String loggingMethod, Throwable t,
55              String text) {
56          text(logLevel, loggingMethod, t, text, null);
57      }
58  
59      public void text(int logLevel, String loggingMethod, Throwable t,
60              String text, Object[] parms) {
61          //logger.log("text", org.apache.log4j.Level.ERROR, "isLogging return true with level " + logLevel, null);
62  
63          //determine log4j log level
64          org.apache.log4j.Level log4jLevel = determineLog4jLevel(logLevel);
65  
66          //compose message object
67  
68          StringBuffer msgBuffer = new StringBuffer();
69          if (loggingMethod != null) {
70              msgBuffer.append(loggingMethod);
71              msgBuffer.append(" - ");
72          }
73          if (text != null) {
74              msgBuffer.append(text);
75          }
76          if (parms != null) {
77              msgBuffer.append("\nParameters:\n");
78              for (int i = 0; i < parms.length; i++) {
79                  msgBuffer.append(parms[i]);
80              }
81          }
82  
83          //log it all
84          logger.log(log4jLevel, msgBuffer.toString(), t);
85      }
86  
87      public void entry(int logLevel, String loggingMethod) {
88          entry(logLevel, loggingMethod, null);
89      }
90  
91      public void entry(int logLevel, String loggingMethod, Object parm1) {
92          entry(logLevel, loggingMethod, new Object[] { parm1 });
93      }
94  
95      public void entry(int logLevel, String loggingMethod, Object[] parms) {
96          text(logLevel, loggingMethod, "Entering method", parms);
97      }
98  
99      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, boolean retValue) {
132         exit(logLevel, loggingMethod, Boolean.valueOf(retValue));
133     }
134 
135     public void exit(int logLevel, String loggingMethod, Object retValue) {
136         text(logLevel, loggingMethod, "Exiting method. Returned value: {0}",
137                 retValue);
138     }
139 
140     public void stackTrace(int logLevel, String loggingMethod, String text) {
141         text(logLevel, loggingMethod, new Throwable("Stacktrace"), text);
142     }
143 
144     //returns log4j level
145     private org.apache.log4j.Level determineLog4jLevel(int wsrp4jLevel) {
146         if (wsrp4jLevel < Logger.ERROR) {
147             return org.apache.log4j.Level.OFF;
148         }
149         else if (wsrp4jLevel < Logger.WARN) {
150             return org.apache.log4j.Level.ERROR;
151         }
152         else if (wsrp4jLevel < Logger.INFO) {
153             return org.apache.log4j.Level.WARN;
154         }
155         else if (wsrp4jLevel < Logger.TRACE_LOW) {
156             return org.apache.log4j.Level.INFO;
157         }
158         else if (wsrp4jLevel < Logger.TRACE_HIGH) {
159             return org.apache.log4j.Level.DEBUG;
160         }
161         else {
162             return org.apache.log4j.Level.DEBUG;
163         }
164     }
165 }