001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020/**
021   This class used to output log statements from within the log4j package.
022
023   <p>Log4j components cannot make log4j logging calls. However, it is
024   sometimes useful for the user to learn about what log4j is
025   doing. You can enable log4j internal logging by defining the
026   <b>log4j.configDebug</b> variable.
027
028   <p>All log4j internal debug calls go to <code>System.out</code>
029   where as internal error messages are sent to
030   <code>System.err</code>. All internal messages are prepended with
031   the string "log4j: ".
032   
033   @since 0.8.2
034   @author Ceki G&uuml;lc&uuml;
035*/
036public class LogLog {
037
038  /**
039     Defining this value makes log4j print log4j-internal debug
040     statements to <code>System.out</code>.
041     
042    <p> The value of this string is <b>log4j.debug</b>.
043    
044    <p>Note that the search for all option names is case sensitive.  */
045  public static final String DEBUG_KEY="log4j.debug";
046
047 
048  /**
049     Defining this value makes log4j components print log4j-internal
050     debug statements to <code>System.out</code>.
051     
052    <p> The value of this string is <b>log4j.configDebug</b>.
053    
054    <p>Note that the search for all option names is case sensitive.  
055
056    @deprecated Use {@link #DEBUG_KEY} instead.
057  */
058  public static final String CONFIG_DEBUG_KEY="log4j.configDebug";
059
060  protected static boolean debugEnabled = false;  
061
062  /**
063     In quietMode not even errors generate any output.
064   */
065  private static boolean quietMode = false;
066
067  private static final String PREFIX = "log4j: ";
068  private static final String ERR_PREFIX = "log4j:ERROR ";
069  private static final String WARN_PREFIX = "log4j:WARN ";
070
071  static {
072    String key = OptionConverter.getSystemProperty(DEBUG_KEY, null);
073
074    if(key == null) {
075      key = OptionConverter.getSystemProperty(CONFIG_DEBUG_KEY, null);
076    }
077
078    if(key != null) { 
079      debugEnabled = OptionConverter.toBoolean(key, true);
080    }
081  }
082
083  /**
084     Allows to enable/disable log4j internal logging.
085   */
086  static
087  public
088  void setInternalDebugging(boolean enabled) {
089    debugEnabled = enabled;
090  }
091
092  /**
093     This method is used to output log4j internal debug
094     statements. Output goes to <code>System.out</code>.
095  */
096  public
097  static
098  void debug(String msg) {
099    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}