View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import java.util.Arrays;
17  
18  import ch.qos.logback.core.joran.spi.ConsoleTarget;
19  import ch.qos.logback.core.status.Status;
20  import ch.qos.logback.core.status.WarnStatus;
21  
22  /**
23   * ConsoleAppender appends log events to <code>System.out</code> or
24   * <code>System.err</code> using a layout specified by the user. The default
25   * target is <code>System.out</code>.
26   * 
27   * For more information about this appender, please refer to the online manual
28   * at http://logback.qos.ch/manual/appenders.html#ConsoleAppender
29   * 
30   * @author Ceki G&uuml;lc&uuml;
31   * @author Tom SH Liu
32   * @author Ruediger Dohna
33   */
34  
35  public class ConsoleAppender<E> extends OutputStreamAppender<E> {
36  
37    protected ConsoleTarget target = ConsoleTarget.SystemOut;
38  
39    /**
40     * Sets the value of the <b>Target</b> option. Recognized values are
41     * "System.out" and "System.err". Any other value will be ignored.
42     */
43    public void setTarget(String value) {
44      ConsoleTarget t = ConsoleTarget.findByName(value.trim());
45      if (t == null) {
46        targetWarn(value);
47      } else {
48        target = t;
49      }
50    }
51  
52    /**
53     * Returns the current value of the <b>target</b> property. The default value
54     * of the option is "System.out".
55     * 
56     * See also {@link #setTarget}.
57     */
58    public String getTarget() {
59      return target.getName();
60    }
61  
62    private void targetWarn(String val) {
63      Status status = new WarnStatus("[" + val + "] should be one of "
64          + Arrays.toString(ConsoleTarget.values()), this);
65      status.add(new WarnStatus(
66          "Using previously set target, System.out by default.", this));
67      addStatus(status);
68    }
69  
70    @Override
71    public void start() {
72      setOutputStream(target.getStream());
73      super.start();
74    }
75  }