KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > ConsoleAppender


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j;
18
19 import java.io.OutputStreamWriter JavaDoc;
20 import org.apache.log4j.helpers.LogLog;
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
25   * default target is <code>System.out</code>.
26   *
27   * @author Ceki G&uuml;lc&uuml;
28   * @since 1.1 */

29 public class ConsoleAppender extends WriterAppender {
30
31   public static final String JavaDoc SYSTEM_OUT = "System.out";
32   public static final String JavaDoc SYSTEM_ERR = "System.err";
33
34   protected String JavaDoc target = SYSTEM_OUT;
35
36   /**
37      The default constructor does nothing.
38    */

39   public ConsoleAppender() {
40   }
41
42   public ConsoleAppender(Layout layout) {
43     this(layout, SYSTEM_OUT);
44   }
45
46   public ConsoleAppender(Layout layout, String JavaDoc target) {
47     this.layout = layout;
48
49     if (SYSTEM_OUT.equals(target)) {
50       setWriter(new OutputStreamWriter JavaDoc(System.out));
51     } else if (SYSTEM_ERR.equalsIgnoreCase(target)) {
52       setWriter(new OutputStreamWriter JavaDoc(System.err));
53     } else {
54       targetWarn(target);
55     }
56   }
57
58   /**
59    * Sets the value of the <b>Target</b> option. Recognized values
60    * are "System.out" and "System.err". Any other value will be
61    * ignored.
62    * */

63   public
64   void setTarget(String JavaDoc value) {
65     String JavaDoc v = value.trim();
66
67     if (SYSTEM_OUT.equalsIgnoreCase(v)) {
68       target = SYSTEM_OUT;
69     } else if (SYSTEM_ERR.equalsIgnoreCase(v)) {
70       target = SYSTEM_ERR;
71     } else {
72       targetWarn(value);
73     }
74   }
75
76   /**
77    * Returns the current value of the <b>Target</b> property. The
78    * default value of the option is "System.out".
79    *
80    * See also {@link #setTarget}.
81    * */

82   public
83   String JavaDoc getTarget() {
84     return target;
85   }
86
87   void targetWarn(String JavaDoc val) {
88     LogLog.warn("["+val+"] should be System.out or System.err.");
89     LogLog.warn("Using previously set target, System.out by default.");
90   }
91
92   public
93   void activateOptions() {
94     if(target.equals(SYSTEM_OUT)) {
95       setWriter(new OutputStreamWriter JavaDoc(System.out));
96     } else {
97       setWriter(new OutputStreamWriter JavaDoc(System.err));
98     }
99   }
100
101   /**
102    * This method overrides the parent {@link
103    * WriterAppender#closeWriter} implementation to do nothing because
104    * the console stream is not ours to close.
105    * */

106   protected
107   final
108   void closeWriter() {
109   }
110 }
111
Popular Tags