1 16 17 package org.apache.log4j; 18 19 import java.io.OutputStreamWriter ; 20 import org.apache.log4j.helpers.LogLog; 21 22 29 public class ConsoleAppender extends WriterAppender { 30 31 public static final String SYSTEM_OUT = "System.out"; 32 public static final String SYSTEM_ERR = "System.err"; 33 34 protected String target = SYSTEM_OUT; 35 36 39 public ConsoleAppender() { 40 } 41 42 public ConsoleAppender(Layout layout) { 43 this(layout, SYSTEM_OUT); 44 } 45 46 public ConsoleAppender(Layout layout, String target) { 47 this.layout = layout; 48 49 if (SYSTEM_OUT.equals(target)) { 50 setWriter(new OutputStreamWriter (System.out)); 51 } else if (SYSTEM_ERR.equalsIgnoreCase(target)) { 52 setWriter(new OutputStreamWriter (System.err)); 53 } else { 54 targetWarn(target); 55 } 56 } 57 58 63 public 64 void setTarget(String value) { 65 String 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 82 public 83 String getTarget() { 84 return target; 85 } 86 87 void targetWarn(String 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 (System.out)); 96 } else { 97 setWriter(new OutputStreamWriter (System.err)); 98 } 99 } 100 101 106 protected 107 final 108 void closeWriter() { 109 } 110 } 111 | Popular Tags |