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 ch.qos.logback.core.status.Status;
17 import ch.qos.logback.core.status.WarnStatus;
18
19
20 /**
21 * ConsoleAppender appends log events to <code>System.out</code> or
22 * <code>System.err</code> using a layout specified by the user. The default
23 * target is <code>System.out</code>.
24 *
25 * For more information about this appender, please refer to the online manual at
26 * http://logback.qos.ch/manual/appenders.html#ConsoleAppender
27 *
28 * @author Ceki Gülcü
29 */
30
31 public class ConsoleAppender<E> extends WriterAppender<E> {
32
33 public static final String SYSTEM_OUT = "System.out";
34 public static final String SYSTEM_ERR = "System.err";
35 protected String target = SYSTEM_OUT;
36
37 /**
38 * As in most logback components, the default constructor does nothing.
39 */
40 public ConsoleAppender() {
41 }
42
43 /**
44 * Sets the value of the <b>Target</b> option. Recognized values are
45 * "System.out" and "System.err". Any other value will be ignored.
46 */
47 public void setTarget(String value) {
48 String v = value.trim();
49
50 if (SYSTEM_OUT.equalsIgnoreCase(v)) {
51 target = SYSTEM_OUT;
52 } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
53 target = SYSTEM_ERR;
54 } else {
55 targetWarn(value);
56 }
57 }
58
59 /**
60 * Returns the current value of the <b>Target</b> property. The default
61 * value of the option is "System.out".
62 *
63 * See also {@link #setTarget}.
64 */
65 public String getTarget() {
66 return target;
67 }
68
69 void targetWarn(String val) {
70 Status status = new WarnStatus("["+val+" should be System.out or System.err.", this);
71 status.add(new WarnStatus("Using previously set target, System.out by default.", this));
72 addStatus(status);
73 }
74
75 public void start() {
76 if (target.equals(SYSTEM_OUT)) {
77 setWriter(createWriter(System.out));
78 } else {
79 setWriter(createWriter(System.err));
80 }
81 super.start();
82 }
83
84 /**
85 * This method overrides the parent {@link WriterAppender#closeWriter}
86 * implementation because the console stream is not ours to close.
87 */
88 protected final void closeWriter() {
89 writeFooter();
90 }
91 }
92
93