View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.helpers;
19  
20  /**
21     This class used to output log statements from within the log4j package.
22  
23     <p>Log4j components cannot make log4j logging calls. However, it is
24     sometimes useful for the user to learn about what log4j is
25     doing. You can enable log4j internal logging by defining the
26     <b>log4j.configDebug</b> variable.
27  
28     <p>All log4j internal debug calls go to <code>System.out</code>
29     where as internal error messages are sent to
30     <code>System.err</code>. All internal messages are prepended with
31     the string "log4j: ".
32     
33     @since 0.8.2
34     @author Ceki G&uuml;lc&uuml;
35  */
36  public class LogLog {
37  
38    /**
39       Defining this value makes log4j print log4j-internal debug
40       statements to <code>System.out</code>.
41       
42      <p> The value of this string is <b>log4j.debug</b>.
43      
44      <p>Note that the search for all option names is case sensitive.  */
45    public static final String DEBUG_KEY="log4j.debug";
46  
47   
48    /**
49       Defining this value makes log4j components print log4j-internal
50       debug statements to <code>System.out</code>.
51       
52      <p> The value of this string is <b>log4j.configDebug</b>.
53      
54      <p>Note that the search for all option names is case sensitive.  
55  
56      @deprecated Use {@link #DEBUG_KEY} instead.
57    */
58    public static final String CONFIG_DEBUG_KEY="log4j.configDebug";
59  
60    protected static boolean debugEnabled = false;  
61  
62    /**
63       In quietMode not even errors generate any output.
64     */
65    private static boolean quietMode = false;
66  
67    private static final String PREFIX = "log4j: ";
68    private static final String ERR_PREFIX = "log4j:ERROR ";
69    private static final String WARN_PREFIX = "log4j:WARN ";
70  
71    static {
72      String key = OptionConverter.getSystemProperty(DEBUG_KEY, null);
73  
74      if(key == null) {
75        key = OptionConverter.getSystemProperty(CONFIG_DEBUG_KEY, null);
76      }
77  
78      if(key != null) { 
79        debugEnabled = OptionConverter.toBoolean(key, true);
80      }
81    }
82  
83    /**
84       Allows to enable/disable log4j internal logging.
85     */
86    static
87    public
88    void setInternalDebugging(boolean enabled) {
89      debugEnabled = enabled;
90    }
91  
92    /**
93       This method is used to output log4j internal debug
94       statements. Output goes to <code>System.out</code>.
95    */
96    public
97    static
98    void debug(String msg) {
99      if(debugEnabled && !quietMode) {
100       System.out.println(PREFIX+msg);
101     }
102   }
103 
104   /**
105      This method is used to output log4j internal debug
106      statements. Output goes to <code>System.out</code>.
107   */
108   public
109   static
110   void debug(String msg, Throwable t) {
111     if(debugEnabled && !quietMode) {
112       System.out.println(PREFIX+msg);
113       if(t != null)
114 	t.printStackTrace(System.out);
115     }
116   }
117   
118 
119   /**
120      This method is used to output log4j internal error
121      statements. There is no way to disable error statements.
122      Output goes to <code>System.err</code>.
123   */
124   public
125   static
126   void error(String msg) {
127     if(quietMode)
128       return;
129     System.err.println(ERR_PREFIX+msg);
130   }  
131 
132   /**
133      This method is used to output log4j internal error
134      statements. There is no way to disable error statements.
135      Output goes to <code>System.err</code>.  
136   */
137   public
138   static
139   void error(String msg, Throwable t) {
140     if(quietMode)
141       return;
142 
143     System.err.println(ERR_PREFIX+msg);
144     if(t != null) {
145       t.printStackTrace();
146     }
147   }  
148 
149   /**
150      In quite mode no LogLog generates strictly no output, not even
151      for errors. 
152 
153      @param quietMode A true for not
154   */
155   public
156   static
157   void setQuietMode(boolean quietMode) {
158     LogLog.quietMode = quietMode;
159   }
160 
161   /**
162      This method is used to output log4j internal warning
163      statements. There is no way to disable warning statements.
164      Output goes to <code>System.err</code>.  */
165   public
166   static
167   void warn(String msg) {
168     if(quietMode)
169       return;
170 
171     System.err.println(WARN_PREFIX+msg);
172   }  
173 
174   /**
175      This method is used to output log4j internal warnings. There is
176      no way to disable warning statements.  Output goes to
177      <code>System.err</code>.  */
178   public
179   static
180   void warn(String msg, Throwable t) {
181     if(quietMode)
182       return;
183 
184     System.err.println(WARN_PREFIX+msg);
185     if(t != null) {
186       t.printStackTrace();
187     }
188   }  
189 }