KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > LogLog


1 /*
2  * Copyright 1999-2005 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 package org.apache.log4j.helpers;
18
19 /**
20    This class used to output log statements from within the log4j package.
21
22    <p>Log4j components cannot make log4j logging calls. However, it is
23    sometimes useful for the user to learn about what log4j is
24    doing. You can enable log4j internal logging by defining the
25    <b>log4j.configDebug</b> variable.
26
27    <p>All log4j internal debug calls go to <code>System.out</code>
28    where as internal error messages are sent to
29    <code>System.err</code>. All internal messages are prepended with
30    the string "log4j: ".
31    
32    @since 0.8.2
33    @author Ceki G&uuml;lc&uuml;
34 */

35 public class LogLog {
36
37   /**
38      Defining this value makes log4j print log4j-internal debug
39      statements to <code>System.out</code>.
40      
41     <p> The value of this string is <b>log4j.debug</b>.
42     
43     <p>Note that the search for all option names is case sensitive. */

44   public static final String JavaDoc DEBUG_KEY="log4j.debug";
45
46  
47   /**
48      Defining this value makes log4j components print log4j-internal
49      debug statements to <code>System.out</code>.
50      
51     <p> The value of this string is <b>log4j.configDebug</b>.
52     
53     <p>Note that the search for all option names is case sensitive.
54
55     @deprecated Use {@link #DEBUG_KEY} instead.
56   */

57   public static final String JavaDoc CONFIG_DEBUG_KEY="log4j.configDebug";
58
59   protected static boolean debugEnabled = false;
60
61   /**
62      In quietMode not even errors generate any output.
63    */

64   private static boolean quietMode = false;
65
66   private static final String JavaDoc PREFIX = "log4j: ";
67   private static final String JavaDoc ERR_PREFIX = "log4j:ERROR ";
68   private static final String JavaDoc WARN_PREFIX = "log4j:WARN ";
69
70   static {
71     String JavaDoc key = OptionConverter.getSystemProperty(DEBUG_KEY, null);
72
73     if(key == null) {
74       key = OptionConverter.getSystemProperty(CONFIG_DEBUG_KEY, null);
75     }
76
77     if(key != null) {
78       debugEnabled = OptionConverter.toBoolean(key, true);
79     }
80   }
81
82   /**
83      Allows to enable/disable log4j internal logging.
84    */

85   static
86   public
87   void setInternalDebugging(boolean enabled) {
88     debugEnabled = enabled;
89   }
90
91   /**
92      This method is used to output log4j internal debug
93      statements. Output goes to <code>System.out</code>.
94   */

95   public
96   static
97   void debug(String JavaDoc msg) {
98     if(debugEnabled && !quietMode) {
99       System.out.println(PREFIX+msg);
100     }
101   }
102
103   /**
104      This method is used to output log4j internal debug
105      statements. Output goes to <code>System.out</code>.
106   */

107   public
108   static
109   void debug(String JavaDoc msg, Throwable JavaDoc t) {
110     if(debugEnabled && !quietMode) {
111       System.out.println(PREFIX+msg);
112       if(t != null)
113     t.printStackTrace(System.out);
114     }
115   }
116   
117
118   /**
119      This method is used to output log4j internal error
120      statements. There is no way to disable error statements.
121      Output goes to <code>System.err</code>.
122   */

123   public
124   static
125   void error(String JavaDoc msg) {
126     if(quietMode)
127       return;
128     System.err.println(ERR_PREFIX+msg);
129   }
130
131   /**
132      This method is used to output log4j internal error
133      statements. There is no way to disable error statements.
134      Output goes to <code>System.err</code>.
135   */

136   public
137   static
138   void error(String JavaDoc msg, Throwable JavaDoc t) {
139     if(quietMode)
140       return;
141
142     System.err.println(ERR_PREFIX+msg);
143     if(t != null) {
144       t.printStackTrace();
145     }
146   }
147
148   /**
149      In quite mode no LogLog generates strictly no output, not even
150      for errors.
151
152      @param quietMode A true for not
153   */

154   public
155   static
156   void setQuietMode(boolean quietMode) {
157     LogLog.quietMode = quietMode;
158   }
159
160   /**
161      This method is used to output log4j internal warning
162      statements. There is no way to disable warning statements.
163      Output goes to <code>System.err</code>. */

164   public
165   static
166   void warn(String JavaDoc msg) {
167     if(quietMode)
168       return;
169
170     System.err.println(WARN_PREFIX+msg);
171   }
172
173   /**
174      This method is used to output log4j internal warnings. There is
175      no way to disable warning statements. Output goes to
176      <code>System.err</code>. */

177   public
178   static
179   void warn(String JavaDoc msg, Throwable JavaDoc t) {
180     if(quietMode)
181       return;
182
183     System.err.println(WARN_PREFIX+msg);
184     if(t != null) {
185       t.printStackTrace();
186     }
187   }
188 }
189
Popular Tags