1
2
3
4
5
6
7
8
9
10
11
12
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
38
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 }