Curt,
I noticed that you removed the protected closeWriter method. This need to be included because these streams should not be closed.
I also wanted to point out that you only need a single reference of SystemErrStream and SystemOutStream. I would recommend that instead of creating new SystemErrStream and SystemOutStream instances make them final static attributes instead. For instance,
class ConsoleAppender ... {
private final static OutputStream SystemErrStream = new OutputStream() { ... };
}
This would remove the overhead of having to create these instances since all instances will be using the same standard error or standard out.
--Claudio
[EMAIL PROTECTED] wrote:
carnold 2005/05/07 09:56:41
Modified: src/java/org/apache/log4j ConsoleAppender.java
Log:
Bug 31056: Console appender doesn't notices changes to System.out
Revision Changes Path
1.24 +138 -72 logging-log4j/src/java/org/apache/log4j/ConsoleAppender.java
Index: ConsoleAppender.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/ConsoleAppender.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- ConsoleAppender.java 8 Mar 2005 22:32:56 -0000 1.23
+++ ConsoleAppender.java 7 May 2005 16:56:41 -0000 1.24
@@ -1,21 +1,23 @@
/*
* Copyright 1999,2005 The Apache Software Foundation.
- * + *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- * + *
* http://www.apache.org/licenses/LICENSE-2.0
- * + *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.log4j;
+import java.io.IOException;
+import java.io.OutputStream;
+
/**
* ConsoleAppender appends log events to <code>System.out</code> or
@@ -23,74 +25,138 @@
* default target is <code>System.out</code>.
*
* @author <a href="http://www.qos.ch/log4j/">Ceki Gülcü</a>
+ * @author Curt Arnold
* @since 1.1 */
public class ConsoleAppender extends WriterAppender {
- public static final String SYSTEM_OUT = "System.out";
- public static final String SYSTEM_ERR = "System.err";
- protected String target = SYSTEM_OUT;
-
- /**
- * As in most cases, the default constructor does nothing.
- */
- public ConsoleAppender() {
- }
-
- public ConsoleAppender(Layout layout) {
- this(layout, SYSTEM_OUT);
- }
-
- public ConsoleAppender(Layout layout, String targetStr) {
- this.layout = layout;
- setTarget(targetStr);
- activateOptions();
- }
-
- /**
- * Sets the value of the <b>Target</b> option. Recognized values
- * are "System.out" and "System.err". Any other value will be
- * ignored.
- * */
- public void setTarget(String value) {
- String v = value.trim();
-
- if (SYSTEM_OUT.equalsIgnoreCase(v)) {
- target = SYSTEM_OUT;
- } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
- target = SYSTEM_ERR;
- } else {
- targetWarn(value);
- }
- }
-
- /**
- * Returns the current value of the <b>Target</b> property. The
- * default value of the option is "System.out".
- *
- * See also [EMAIL PROTECTED] #setTarget}.
- * */
- public String getTarget() {
- return target;
- }
-
- void targetWarn(String val) {
- getLogger().warn("[{}] should be System.out or System.err.", val);
- getLogger().warn("Using previously set target, System.out by default.");
- }
-
- public void activateOptions() {
- if (target.equals(SYSTEM_OUT)) {
- setWriter(createWriter(System.out));
- } else {
- setWriter(createWriter(System.err));
- }
- super.activateOptions();
- }
-
- /**
- * This method overrides the parent [EMAIL PROTECTED]
- * WriterAppender#closeWriter} implementation to do nothing because
- * the console stream is not ours to close.
- * */
- protected final void closeWriter() {
- }
+ public static final String SYSTEM_OUT = "System.out";
+ public static final String SYSTEM_ERR = "System.err";
+ protected String target = SYSTEM_OUT;
+
+ /**
+ * Constructs an unconfigured appender.
+ */
+ public ConsoleAppender() {
+ }
+
+ /**
+ * Creates a configured appender.
+ *
+ * @param layout layout, may not be null.
+ */
+ public ConsoleAppender(final Layout layout) {
+ setLayout(layout);
+ activateOptions();
+ }
+
+ /**
+ * Creates a configured appender.
+ * @param layout layout, may not be null.
+ * @param targetStr target, either "System.err" or "System.out".
+ */
+ public ConsoleAppender(final Layout layout, final String targetStr) {
+ setLayout(layout);
+ setTarget(targetStr);
+ activateOptions();
+ }
+
+ /**
+ * Sets the value of the <b>Target</b> option. Recognized values
+ * are "System.out" and "System.err". Any other value will be
+ * ignored.
+ * */
+ public void setTarget(final String value) {
+ String v = value.trim();
+
+ if (SYSTEM_OUT.equalsIgnoreCase(v)) {
+ target = SYSTEM_OUT;
+ } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
+ target = SYSTEM_ERR;
+ } else {
+ getLogger().warn("[{}] should be System.out or System.err.", value);
+ getLogger().warn("Using previously set target, System.out by default.");
+ }
+ }
+
+ /**
+ * Returns the current value of the <b>Target</b> property. The
+ * default value of the option is "System.out".
+ *
+ * See also [EMAIL PROTECTED] #setTarget}.
+ * */
+ public String getTarget() {
+ return target;
+ }
+
+ /**
+ * Prepares the appender for use.
+ */
+ public void activateOptions() {
+ if (target.equals(SYSTEM_ERR)) {
+ setWriter(createWriter(new SystemErrStream()));
+ } else {
+ setWriter(createWriter(new SystemOutStream()));
+ }
+
+ super.activateOptions();
+ }
+
+ /**
+ * An implementation of OutputStream that redirects to the
+ * current System.err.
+ *
+ */
+ private static class SystemErrStream extends OutputStream {
+ public SystemErrStream() {
+ }
+
+ public void close() {
+ }
+
+ public void flush() {
+ System.err.flush();
+ }
+
+ public void write(final byte[] b) throws IOException {
+ System.err.write(b);
+ }
+
+ public void write(final byte[] b, final int off, final int len)
+ throws IOException {
+ System.err.write(b, off, len);
+ }
+
+ public void write(final int b) throws IOException {
+ System.err.write(b);
+ }
+ }
+
+ /**
+ * An implementation of OutputStream that redirects to the
+ * current System.out.
+ *
+ */
+ private static class SystemOutStream extends OutputStream {
+ public SystemOutStream() {
+ }
+
+ public void close() {
+ }
+
+ public void flush() {
+ System.out.flush();
+ }
+
+ public void write(final byte[] b) throws IOException {
+ System.out.write(b);
+ }
+
+ public void write(final byte[] b, final int off, final int len)
+ throws IOException {
+ System.out.write(b, off, len);
+ }
+
+ public void write(final int b) throws IOException {
+ System.out.write(b);
+ }
+ }
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]