1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.pattern;
15
16 import java.util.List;
17 import java.util.regex.Pattern;
18
19 public class ReplacingCompositeConverter<E> extends CompositeConverter<E> {
20
21 Pattern pattern;
22 String regex;
23 String replacement;
24
25 public void start() {
26 final List<String> optionList = getOptionList();
27 if (optionList == null) {
28 addError("at least two options are expected whereas you have declared none");
29 return;
30 }
31
32 int numOpts = optionList.size();
33
34 if (numOpts < 2) {
35 addError("at least two options are expected whereas you have declared only " + numOpts + "as [" + optionList
36 + "]");
37 return;
38 }
39 regex = optionList.get(0);
40 pattern = Pattern.compile(regex);
41 replacement = optionList.get(1);
42 super.start();
43 }
44
45 @Override
46 protected String transform(E event, String in) {
47 if (!started)
48 return in;
49 return pattern.matcher(in).replaceAll(replacement);
50 }
51 }