View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 chapter5;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.pattern.ClassicConverter;
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  
20  public class MySampleConverter extends ClassicConverter {
21  
22    private static final String END_COLOR = "\u001b[m";
23  
24    private static final String ERROR_COLOR = "\u001b[0;31m";
25    private static final String WARN_COLOR = "\u001b[0;33m";
26  
27    @Override
28    public String convert(ILoggingEvent event) {
29      StringBuffer sbuf = new StringBuffer();
30      sbuf.append(getColor(event.getLevel()));
31      sbuf.append(event.getLevel());
32      sbuf.append(END_COLOR);
33      return sbuf.toString();
34    }
35  
36    /**
37     * Returns the appropriate characters to change the color for the specified
38     * logging level.
39     */
40    private String getColor(Level level) {
41      switch (level.toInt()) {
42      case Level.ERROR_INT:
43        return ERROR_COLOR;
44      case Level.WARN_INT:
45        return WARN_COLOR;
46      default:
47        return "";
48      }
49    }
50  }