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ülcü
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 /**
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 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 PREFIX = "log4j: ";
67 private static final String ERR_PREFIX = "log4j:ERROR ";
68 private static final String WARN_PREFIX = "log4j:WARN ";
69
70 static {
71 }
72
73 /**
74 Allows to enable/disable log4j internal logging.
75 */
76 static
77 public
78 void setInternalDebugging(boolean enabled) {
79 debugEnabled = enabled;
80 }
81
82 /**
83 This method is used to output log4j internal debug
84 statements. Output goes to <code>System.out</code>.
85 */
86 public
87 static
88 void debug(String msg) {
89 if(debugEnabled && !quietMode) {
90 System.out.println(PREFIX+msg);
91 }
92 }
93
94 /**
95 This method is used to output log4j internal debug
96 statements. Output goes to <code>System.out</code>.
97 */
98 public
99 static
100 void debug(String msg, Throwable t) {
101 if(debugEnabled && !quietMode) {
102 System.out.println(PREFIX+msg);
103 if(t != null)
104 t.printStackTrace(System.out);
105 }
106 }
107
108
109 /**
110 This method is used to output log4j internal error
111 statements. There is no way to disable error statements.
112 Output goes to <code>System.err</code>.
113 */
114 public
115 static
116 void error(String msg) {
117 if(quietMode)
118 return;
119 System.err.println(ERR_PREFIX+msg);
120 }
121
122 /**
123 This method is used to output log4j internal error
124 statements. There is no way to disable error statements.
125 Output goes to <code>System.err</code>.
126 */
127 public
128 static
129 void error(String msg, Throwable t) {
130 if(quietMode)
131 return;
132
133 System.err.println(ERR_PREFIX+msg);
134 if(t != null) {
135 t.printStackTrace();
136 }
137 }
138
139 /**
140 In quite mode no LogLog generates strictly no output, not even
141 for errors.
142
143 @param quietMode A true for not
144 */
145 public
146 static
147 void setQuietMode(boolean quietMode) {
148 LogLog.quietMode = quietMode;
149 }
150
151 /**
152 This method is used to output log4j internal warning
153 statements. There is no way to disable warning statements.
154 Output goes to <code>System.err</code>. */
155 public
156 static
157 void warn(String msg) {
158 if(quietMode)
159 return;
160
161 System.err.println(WARN_PREFIX+msg);
162 }
163
164 /**
165 This method is used to output log4j internal warnings. There is
166 no way to disable warning statements. Output goes to
167 <code>System.err</code>. */
168 public
169 static
170 void warn(String msg, Throwable t) {
171 if(quietMode)
172 return;
173
174 System.err.println(WARN_PREFIX+msg);
175 if(t != null) {
176 t.printStackTrace();
177 }
178 }
179 }