1
14 package ch.qos.logback.core;
15
16 import java.io.OutputStream;
17 import java.util.Arrays;
18
19 import ch.qos.logback.core.joran.spi.ConsoleTarget;
20 import ch.qos.logback.core.status.Status;
21 import ch.qos.logback.core.status.WarnStatus;
22 import ch.qos.logback.core.util.EnvUtil;
23 import ch.qos.logback.core.util.OptionHelper;
24
25
37
38 public class ConsoleAppender<E> extends OutputStreamAppender<E> {
39
40 protected ConsoleTarget target = ConsoleTarget.SystemOut;
41 protected boolean withJansi = false;
42
43 private final static String WindowsAnsiOutputStream_CLASS_NAME = "org.fusesource.jansi.WindowsAnsiOutputStream";
44
45
49 public void setTarget(String value) {
50 ConsoleTarget t = ConsoleTarget.findByName(value.trim());
51 if (t == null) {
52 targetWarn(value);
53 } else {
54 target = t;
55 }
56 }
57
58
64 public String getTarget() {
65 return target.getName();
66 }
67
68 private void targetWarn(String val) {
69 Status status = new WarnStatus("[" + val + "] should be one of " + Arrays.toString(ConsoleTarget.values()), this);
70 status.add(new WarnStatus("Using previously set target, System.out by default.", this));
71 addStatus(status);
72 }
73
74 @Override
75 public void start() {
76 OutputStream targetStream = target.getStream();
77
78 if (EnvUtil.isWindows() && withJansi) {
79 targetStream = getTargetStreamForWindows(targetStream);
80 }
81 setOutputStream(targetStream);
82 super.start();
83 }
84
85 private OutputStream getTargetStreamForWindows(OutputStream targetStream) {
86 try {
87 addInfo("Enabling JANSI WindowsAnsiOutputStream for the console.");
88 Object windowsAnsiOutputStream = OptionHelper.instantiateByClassNameAndParameter(WindowsAnsiOutputStream_CLASS_NAME, Object.class, context,
89 OutputStream.class, targetStream);
90 return (OutputStream) windowsAnsiOutputStream;
91 } catch (Exception e) {
92 addWarn("Failed to create WindowsAnsiOutputStream. Falling back on the default stream.", e);
93 }
94 return targetStream;
95 }
96
97
100 public boolean isWithJansi() {
101 return withJansi;
102 }
103
104
110 public void setWithJansi(boolean withJansi) {
111 this.withJansi = withJansi;
112 }
113
114 }
115