diff --git a/logback-core/src/main/java/ch/qos/logback/core/ConsoleAppender.java b/logback-core/src/main/java/ch/qos/logback/core/ConsoleAppender.java index 265d787..ec96e1d 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/ConsoleAppender.java +++ b/logback-core/src/main/java/ch/qos/logback/core/ConsoleAppender.java @@ -13,7 +13,6 @@ */ package ch.qos.logback.core; -import ch.qos.logback.core.helpers.ConsoleOutputStreamWrapper; import ch.qos.logback.core.joran.spi.ConsoleTarget; import ch.qos.logback.core.status.Status; import ch.qos.logback.core.status.WarnStatus; @@ -32,29 +31,18 @@ import ch.qos.logback.core.status.WarnStatus; public class ConsoleAppender extends OutputStreamAppender { - public static final String SYSTEM_OUT = "System.out"; - public static final String SYSTEM_ERR = "System.err"; protected ConsoleTarget target = ConsoleTarget.SystemOut; /** - * As in most logback components, the default constructor does nothing. - */ - public ConsoleAppender() { - } - - /** * Sets the value of the Target 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 = ConsoleTarget.SystemOut; - } else if (SYSTEM_ERR.equalsIgnoreCase(v)) { - target = ConsoleTarget.SystemErr; - } else { + ConsoleTarget t = ConsoleTarget.findByName(value.trim()); + if (t == null) { targetWarn(value); + } else { + target = t; } } @@ -65,26 +53,18 @@ public class ConsoleAppender extends OutputStreamAppender { * See also {@link #setTarget}. */ public String getTarget() { - switch (target) { - case SystemOut: - return SYSTEM_OUT; - case SystemErr: - return SYSTEM_ERR; - } - throw new IllegalStateException("Unexpected target value ["+target+"]"); + return target.getName(); } - void targetWarn(String val) { - Status status = new WarnStatus("[" + val - + " should be System.out or System.err.", this); - status.add(new WarnStatus( - "Using previously set target, System.out by default.", this)); + private void targetWarn(String val) { + Status status = new WarnStatus("[" + val + " should be in " + ConsoleTarget.values(), this); + status.add(new WarnStatus("Using previously set target, System.out by default.", this)); addStatus(status); } + @Override public void start() { - setOutputStream(new ConsoleOutputStreamWrapper(target)); + setOutputStream(target.getStream()); super.start(); } - } diff --git a/logback-core/src/main/java/ch/qos/logback/core/helpers/ConsoleOutputStreamWrapper.java b/logback-core/src/main/java/ch/qos/logback/core/helpers/ConsoleOutputStreamWrapper.java deleted file mode 100644 index ae53e03..0000000 --- a/logback-core/src/main/java/ch/qos/logback/core/helpers/ConsoleOutputStreamWrapper.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Logback: the reliable, generic, fast and flexible logging framework. - * Copyright (C) 1999-2010, QOS.ch. All rights reserved. - * - * This program and the accompanying materials are dual-licensed under either - * the terms of the Eclipse Public License v1.0 as published by the Eclipse - * Foundation - * - * or (per the licensee's choosing) - * - * under the terms of the GNU Lesser General Public License version 2.1 as - * published by the Free Software Foundation. - */ -package ch.qos.logback.core.helpers; - -import java.io.IOException; -import java.io.OutputStream; - -import ch.qos.logback.core.joran.spi.ConsoleTarget; - -/** - * An {@link OutputStream} which always outputs to the current value of - * System.out/System.err. - * - * @author Ceki Gülcü - * @author Tom SH Liu - */ -public class ConsoleOutputStreamWrapper extends OutputStream { - - ConsoleTarget consoleTarget; - - public ConsoleOutputStreamWrapper(ConsoleTarget consoleTarget) { - this.consoleTarget = consoleTarget; - } - - private OutputStream getOutputStream() { - switch (consoleTarget) { - case SystemOut: - return System.out; - case SystemErr: - return System.err; - } - throw new IllegalStateException("Unpexpected consoleTarget value [" - + consoleTarget + "]"); - } - - @Override - public void write(int b) throws IOException { - getOutputStream().write(b); - } - - @Override - public void write(byte b[]) throws IOException { - this.write(b, 0, b.length); - } - - @Override - public void write(byte b[], int off, int len) throws IOException { - getOutputStream().write(b, off, len); - } - - @Override - public void flush() throws IOException { - getOutputStream().flush(); - } - - @Override - public void close() throws IOException { - // the console is not ours to close - } -} diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/spi/ConsoleTarget.java b/logback-core/src/main/java/ch/qos/logback/core/joran/spi/ConsoleTarget.java index dfbcffb..8eea95b 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/joran/spi/ConsoleTarget.java +++ b/logback-core/src/main/java/ch/qos/logback/core/joran/spi/ConsoleTarget.java @@ -13,11 +13,52 @@ */ package ch.qos.logback.core.joran.spi; +import java.io.IOException; +import java.io.OutputStream; + /** * The set of console output targets. * * @author Ceki Gülcü */ public enum ConsoleTarget { - SystemOut, SystemErr; + + SystemOut("System.out", new OutputStream() { + @Override + public void write(int b) throws IOException { + System.out.write(b); + } + }), + + SystemErr("System.err", new OutputStream() { + @Override + public void write(int b) throws IOException { + System.err.write(b); + } + }); + + public static ConsoleTarget findByName(String name) { + for (ConsoleTarget target : ConsoleTarget.values()) { + if (target.name.equalsIgnoreCase(name)) { + return target; + } + } + return null; + } + + private final String name; + private final OutputStream stream; + + private ConsoleTarget(String name, OutputStream stream) { + this.name = name; + this.stream = stream; + } + + public String getName() { + return name; + } + + public OutputStream getStream() { + return stream; + } } \ No newline at end of file