View Javadoc
1   /*
2    * Copyright 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  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 DEBUG_KEY = "log4j.debug";
45  
46      /**
47         Defining this value makes log4j components print log4j-internal
48         debug statements to <code>System.out</code>.
49      
50        <p> The value of this string is <b>log4j.configDebug</b>.
51      
52        <p>Note that the search for all option names is case sensitive.
53      
54        @deprecated Use {@link #DEBUG_KEY} instead.
55      */
56      public static final String CONFIG_DEBUG_KEY = "log4j.configDebug";
57  
58      protected static boolean debugEnabled = false;
59  
60      /**
61         In quietMode not even errors generate any output.
62       */
63      private static boolean quietMode = false;
64  
65      private static final String PREFIX = "log4j: ";
66      private static final String ERR_PREFIX = "log4j:ERROR ";
67      private static final String WARN_PREFIX = "log4j:WARN ";
68  
69      static {
70      }
71  
72      /**
73         Allows to enable/disable log4j internal logging.
74       */
75      static public void setInternalDebugging(boolean enabled) {
76          debugEnabled = enabled;
77      }
78  
79      /**
80         This method is used to output log4j internal debug
81         statements. Output goes to <code>System.out</code>.
82      */
83      public static void debug(String msg) {
84          if (debugEnabled && !quietMode) {
85              System.out.println(PREFIX + msg);
86          }
87      }
88  
89      /**
90         This method is used to output log4j internal debug
91         statements. Output goes to <code>System.out</code>.
92      */
93      public static void debug(String msg, Throwable t) {
94          if (debugEnabled && !quietMode) {
95              System.out.println(PREFIX + msg);
96              if (t != null)
97                  t.printStackTrace(System.out);
98          }
99      }
100 
101     /**
102        This method is used to output log4j internal error
103        statements. There is no way to disable error statements.
104        Output goes to <code>System.err</code>.
105     */
106     public static void error(String msg) {
107         if (quietMode)
108             return;
109         System.err.println(ERR_PREFIX + msg);
110     }
111 
112     /**
113        This method is used to output log4j internal error
114        statements. There is no way to disable error statements.
115        Output goes to <code>System.err</code>.
116     */
117     public static void error(String msg, Throwable t) {
118         if (quietMode)
119             return;
120 
121         System.err.println(ERR_PREFIX + msg);
122         if (t != null) {
123             t.printStackTrace();
124         }
125     }
126 
127     /**
128        In quite mode no LogLog generates strictly no output, not even
129        for errors.
130     
131        @param quietMode A true for not
132     */
133     public static void setQuietMode(boolean quietMode) {
134         LogLog.quietMode = quietMode;
135     }
136 
137     /**
138        This method is used to output log4j internal warning
139        statements. There is no way to disable warning statements.
140        Output goes to <code>System.err</code>.  */
141     public static void warn(String msg) {
142         if (quietMode)
143             return;
144 
145         System.err.println(WARN_PREFIX + msg);
146     }
147 
148     /**
149        This method is used to output log4j internal warnings. There is
150        no way to disable warning statements.  Output goes to
151        <code>System.err</code>.  */
152     public static void warn(String msg, Throwable t) {
153         if (quietMode)
154             return;
155 
156         System.err.println(WARN_PREFIX + msg);
157         if (t != null) {
158             t.printStackTrace();
159         }
160     }
161 }