KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > Logger


1 /*
2  * Copyright 2005 Joe Walker
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 package org.directwebremoting.util;
17
18 /**
19  * A very quick and dirty logging implementation.
20  * <code>java.util.logging</code> is out because we work with JDK 1.3 and we
21  * don't want to force users to import log4j or commons-logging.
22  * Don't use this outside of DWR - it's just a quick hack to keep things simple.
23  * @author Joe Walker [joe at getahead dot ltd dot uk]
24  */

25 public final class Logger
26 {
27     /**
28      * @param base The class to log against
29      * @return A new logger
30      */

31     public static Logger getLogger(Class JavaDoc base)
32     {
33         return new Logger(base);
34     }
35
36     /**
37      * Prevent instansiation
38      * @param base The class to log against
39      */

40     private Logger(Class JavaDoc base)
41     {
42         if (!commonsLoggingTried)
43         {
44             try
45             {
46                 LoggingOutput internal = new CommonsLoggingOutput(Logger.class);
47                 internal.debug("Logging using commons-logging.");
48                 commonsLoggingAvailable = true;
49             }
50             catch (NoClassDefFoundError JavaDoc ex)
51             {
52                 LoggingOutput internal = new ServletLoggingOutput();
53                 internal.debug("Logging using servlet.log.");
54                 commonsLoggingAvailable = false;
55             }
56
57             commonsLoggingTried = true;
58         }
59
60         if (commonsLoggingAvailable)
61         {
62             output = new CommonsLoggingOutput(base);
63         }
64         else
65         {
66             output = new ServletLoggingOutput();
67         }
68     }
69
70     /**
71      * The logging implementation
72      */

73     private LoggingOutput output;
74
75     private static boolean commonsLoggingTried = false;
76
77     private static boolean commonsLoggingAvailable = false;
78
79     /**
80      * Logger a debug message
81      * @param message The text to log
82      */

83     public void debug(String JavaDoc message)
84     {
85         output.debug(message);
86     }
87
88     /**
89      * Logger an info message
90      * @param message The text to log
91      */

92     public void info(String JavaDoc message)
93     {
94         output.info(message);
95     }
96
97     /**
98      * Logger a warning message
99      * @param message The text to log
100      */

101     public void warn(String JavaDoc message)
102     {
103         output.warn(message);
104     }
105
106     /**
107      * Logger a warning message
108      * @param message The text to log
109      * @param th An optional stack trace
110      */

111     public void warn(String JavaDoc message, Throwable JavaDoc th)
112     {
113         output.warn(message, th);
114     }
115
116     /**
117      * Logger an error message
118      * @param message The text to log
119      */

120     public void error(String JavaDoc message)
121     {
122         output.error(message);
123     }
124
125     /**
126      * Logger an error message
127      * @param message The text to log
128      * @param th An optional stack trace
129      */

130     public void error(String JavaDoc message, Throwable JavaDoc th)
131     {
132         output.error(message, th);
133     }
134
135     /**
136      * Logger a fatal error message
137      * @param message The text to log
138      */

139     public void fatal(String JavaDoc message)
140     {
141         output.fatal(message);
142     }
143
144     /**
145      * Logger a fatal error message
146      * @param message The text to log
147      * @param th An optional stack trace
148      */

149     public void fatal(String JavaDoc message, Throwable JavaDoc th)
150     {
151         output.fatal(message, th);
152     }
153
154     /**
155      * Save CPU time when we are not debugging
156      * @return true if debugging is enabled
157      */

158     public boolean isDebugEnabled()
159     {
160         return output.isDebugEnabled();
161     }
162 }
163
Popular Tags