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