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.pattern.color;
15  
16  import ch.qos.logback.core.pattern.CompositeConverter;
17  import static ch.qos.logback.core.pattern.color.ANSIConstants.*;
18  
19  /**
20   * Base class for all foreground color setting composite converters.
21   *
22   * @param <E>
23   * @since 1.0.5
24   */
25  abstract public class ForegroundCompositeConverterBase<E> extends CompositeConverter<E> {
26  
27      final private static String SET_DEFAULT_COLOR = ESC_START + "0;" + DEFAULT_FG + ESC_END;
28  
29      @Override
30      protected String transform(E event, String in) {
31          StringBuilder sb = new StringBuilder();
32          sb.append(ESC_START);
33          sb.append(getForegroundColorCode(event));
34          sb.append(ESC_END);
35          sb.append(in);
36          sb.append(SET_DEFAULT_COLOR);
37          return sb.toString();
38      }
39  
40      /**
41       * Derived classes return the foreground color specific to the derived class instance.
42       * @return  the foreground color for this instance
43       */
44      abstract protected String getForegroundColorCode(E event);
45  }