2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import org.apache.log4j.helpers.LogLog;
25 * ConsoleAppender appends log events to <code>System.out</code> or
26 * <code>System.err</code> using a layout specified by the user. The
27 * default target is <code>System.out</code>.
29 * @author Ceki Gülcü
32 public class ConsoleAppender extends WriterAppender {
34 public static final String SYSTEM_OUT = "System.out";
35 public static final String SYSTEM_ERR = "System.err";
37 protected String target = SYSTEM_OUT;
40 * Determines if the appender honors reassignments of System.out
41 * or System.err made after configuration.
43 private boolean follow = false;
46 * Constructs an unconfigured appender.
48 public ConsoleAppender() {
52 * Creates a configured appender.
54 * @param layout layout, may not be null.
56 public ConsoleAppender(Layout layout) {
57 this(layout, SYSTEM_OUT);
61 * Creates a configured appender.
62 * @param layout layout, may not be null.
63 * @param target target, either "System.err" or "System.out".
65 public ConsoleAppender(Layout layout, String target) {
72 * Sets the value of the <b>Target</b> option. Recognized values
73 * are "System.out" and "System.err". Any other value will be
77 void setTarget(String value) {
78 String v = value.trim();
80 if (SYSTEM_OUT.equalsIgnoreCase(v)) {
82 } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
90 * Returns the current value of the <b>Target</b> property. The
91 * default value of the option is "System.out".
93 * See also {@link #setTarget}.
101 * Sets whether the appender honors reassignments of System.out
102 * or System.err made after configuration.
103 * @param newValue if true, appender will use value of System.out or
104 * System.err in force at the time when logging events are appended.
107 public final void setFollow(final boolean newValue) {
112 * Gets whether the appender honors reassignments of System.out
113 * or System.err made after configuration.
114 * @return true if appender will use value of System.out or
115 * System.err in force at the time when logging events are appended.
118 public final boolean getFollow() {
122 void targetWarn(String val) {
123 LogLog.warn("["+val+"] should be System.out or System.err.");
124 LogLog.warn("Using previously set target, System.out by default.");
128 * Prepares the appender for use.
130 public void activateOptions() {
132 if (target.equals(SYSTEM_ERR)) {
133 setWriter(createWriter(new SystemErrStream()));
135 setWriter(createWriter(new SystemOutStream()));
138 if (target.equals(SYSTEM_ERR)) {
139 setWriter(createWriter(System.err));
141 setWriter(createWriter(System.out));
145 super.activateOptions();
161 * An implementation of OutputStream that redirects to the
162 * current System.err.
165 private static class SystemErrStream extends OutputStream {
166 public SystemErrStream() {
169 public void close() {
172 public void flush() {
176 public void write(final byte[] b) throws IOException {
180 public void write(final byte[] b, final int off, final int len)
182 System.err.write(b, off, len);
185 public void write(final int b) throws IOException {
191 * An implementation of OutputStream that redirects to the
192 * current System.out.
195 private static class SystemOutStream extends OutputStream {
196 public SystemOutStream() {
199 public void close() {
202 public void flush() {
206 public void write(final byte[] b) throws IOException {
210 public void write(final byte[] b, final int off, final int len)
212 System.out.write(b, off, len);
215 public void write(final int b) throws IOException {