View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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.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  /**
26   * ConsoleAppender appends log events to <code>System.out</code> or
27   * <code>System.err</code> using a layout specified by the user. The default
28   * target is <code>System.out</code>.
29   * <p/>
30   * For more information about this appender, please refer to the online manual
31   * at http://logback.qos.ch/manual/appenders.html#ConsoleAppender
32   *
33   * @author Ceki G&uuml;lc&uuml;
34   * @author Tom SH Liu
35   * @author Ruediger Dohna
36   */
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      /**
46       * Sets the value of the <b>Target</b> option. Recognized values are
47       * "System.out" and "System.err". Any other value will be ignored.
48       */
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      /**
59       * Returns the current value of the <b>target</b> property. The default value
60       * of the option is "System.out".
61       * <p/>
62       * See also {@link #setTarget}.
63       */
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          // enable jansi only on Windows and only if withJansi set to true
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      /**
98       * @return
99       */
100     public boolean isWithJansi() {
101         return withJansi;
102     }
103 
104     /**
105      * If true, this appender will output to a stream which
106      *
107      * @param withJansi
108      * @since 1.0.5
109      */
110     public void setWithJansi(boolean withJansi) {
111         this.withJansi = withJansi;
112     }
113 
114 }