1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.pattern;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.List;
22
23 import org.apache.logging.log4j.core.LogEvent;
24 import org.apache.logging.log4j.core.config.Configuration;
25 import org.apache.logging.log4j.core.config.plugins.Plugin;
26 import org.apache.logging.log4j.core.layout.PatternLayout;
27
28
29
30
31 public abstract class AbstractStyleNameConverter extends LogEventPatternConverter {
32
33 private final List<PatternFormatter> formatters;
34
35 private final String style;
36
37
38
39
40
41
42
43 protected AbstractStyleNameConverter(final String name, final List<PatternFormatter> formatters,
44 final String styling) {
45 super(name, "style");
46 this.formatters = formatters;
47 this.style = styling;
48 }
49
50
51
52
53 @Plugin(name = Black.NAME, category = "Converter")
54 @ConverterKeys(Black.NAME)
55 public static final class Black extends AbstractStyleNameConverter {
56
57
58 protected static final String NAME = "black";
59
60
61
62
63
64
65
66 public Black(final List<PatternFormatter> formatters, final String styling) {
67 super(NAME, formatters, styling);
68 }
69
70
71
72
73
74
75
76
77
78 public static Black newInstance(final Configuration config, final String[] options) {
79 return newInstance(Black.class, NAME, config, options);
80 }
81 }
82
83
84
85
86 @Plugin(name = Blue.NAME, category = "Converter")
87 @ConverterKeys(Blue.NAME)
88 public static final class Blue extends AbstractStyleNameConverter {
89
90
91 protected static final String NAME = "blue";
92
93
94
95
96
97
98
99 public Blue(final List<PatternFormatter> formatters, final String styling) {
100 super(NAME, formatters, styling);
101 }
102
103
104
105
106
107
108
109
110
111 public static Blue newInstance(final Configuration config, final String[] options) {
112 return newInstance(Blue.class, NAME, config, options);
113 }
114 }
115
116
117
118
119 @Plugin(name = Cyan.NAME, category = "Converter")
120 @ConverterKeys(Cyan.NAME)
121 public static final class Cyan extends AbstractStyleNameConverter {
122
123
124 protected static final String NAME = "cyan";
125
126
127
128
129
130
131
132 public Cyan(final List<PatternFormatter> formatters, final String styling) {
133 super(NAME, formatters, styling);
134 }
135
136
137
138
139
140
141
142
143
144 public static Cyan newInstance(final Configuration config, final String[] options) {
145 return newInstance(Cyan.class, NAME, config, options);
146 }
147 }
148
149
150
151
152 @Plugin(name = Green.NAME, category = "Converter")
153 @ConverterKeys(Green.NAME)
154 public static final class Green extends AbstractStyleNameConverter {
155
156
157 protected static final String NAME = "green";
158
159
160
161
162
163
164
165 public Green(final List<PatternFormatter> formatters, final String styling) {
166 super(NAME, formatters, styling);
167 }
168
169
170
171
172
173
174
175
176
177 public static Green newInstance(final Configuration config, final String[] options) {
178 return newInstance(Green.class, NAME, config, options);
179 }
180 }
181
182
183
184
185 @Plugin(name = Magenta.NAME, category = "Converter")
186 @ConverterKeys(Magenta.NAME)
187 public static final class Magenta extends AbstractStyleNameConverter {
188
189
190 protected static final String NAME = "magenta";
191
192
193
194
195
196
197
198 public Magenta(final List<PatternFormatter> formatters, final String styling) {
199 super(NAME, formatters, styling);
200 }
201
202
203
204
205
206
207
208
209
210 public static Magenta newInstance(final Configuration config, final String[] options) {
211 return newInstance(Magenta.class, NAME, config, options);
212 }
213 }
214
215
216
217
218 @Plugin(name = Red.NAME, category = "Converter")
219 @ConverterKeys(Red.NAME)
220 public static final class Red extends AbstractStyleNameConverter {
221
222
223 protected static final String NAME = "red";
224
225
226
227
228
229
230
231 public Red(final List<PatternFormatter> formatters, final String styling) {
232 super(NAME, formatters, styling);
233 }
234
235
236
237
238
239
240
241
242
243 public static Red newInstance(final Configuration config, final String[] options) {
244 return newInstance(Red.class, NAME, config, options);
245 }
246 }
247
248
249
250
251 @Plugin(name = White.NAME, category = "Converter")
252 @ConverterKeys(White.NAME)
253 public static final class White extends AbstractStyleNameConverter {
254
255
256 protected static final String NAME = "white";
257
258
259
260
261
262
263
264 public White(final List<PatternFormatter> formatters, final String styling) {
265 super(NAME, formatters, styling);
266 }
267
268
269
270
271
272
273
274
275
276 public static White newInstance(final Configuration config, final String[] options) {
277 return newInstance(White.class, NAME, config, options);
278 }
279 }
280
281
282
283
284 @Plugin(name = Yellow.NAME, category = "Converter")
285 @ConverterKeys(Yellow.NAME)
286 public static final class Yellow extends AbstractStyleNameConverter {
287
288
289 protected static final String NAME = "yellow";
290
291
292
293
294
295
296
297 public Yellow(final List<PatternFormatter> formatters, final String styling) {
298 super(NAME, formatters, styling);
299 }
300
301
302
303
304
305
306
307
308
309 public static Yellow newInstance(final Configuration config, final String[] options) {
310 return newInstance(Yellow.class, NAME, config, options);
311 }
312 }
313
314
315
316
317
318
319
320
321
322 protected static <T extends AbstractStyleNameConverter> T newInstance(final Class<T> asnConverterClass,
323 final String name, final Configuration config,
324 final String[] options) {
325 final List<PatternFormatter> formatters = toPatternFormatterList(config, options);
326 if (formatters == null) {
327 return null;
328 }
329 try {
330 final Constructor<T> constructor = asnConverterClass.getConstructor(List.class, String.class);
331 return constructor.newInstance(formatters, AnsiEscape.createSequence(name));
332 } catch (final SecurityException e) {
333 LOGGER.error(e.toString(), e);
334 } catch (final NoSuchMethodException e) {
335 LOGGER.error(e.toString(), e);
336 } catch (final IllegalArgumentException e) {
337 LOGGER.error(e.toString(), e);
338 } catch (final InstantiationException e) {
339 LOGGER.error(e.toString(), e);
340 } catch (final IllegalAccessException e) {
341 LOGGER.error(e.toString(), e);
342 } catch (final InvocationTargetException e) {
343 LOGGER.error(e.toString(), e);
344 }
345 return null;
346 }
347
348
349
350
351
352
353
354
355 private static List<PatternFormatter> toPatternFormatterList(final Configuration config, final String[] options) {
356 if (options.length == 0 || options[0] == null) {
357 LOGGER.error("No pattern supplied on style for config=" + config);
358 return null;
359 }
360 final PatternParser parser = PatternLayout.createPatternParser(config);
361 if (parser == null) {
362 LOGGER.error("No PatternParser created for config=" + config + ", options=" + options);
363 return null;
364 }
365 return parser.parse(options[0]);
366 }
367
368
369
370
371 @Override
372 public void format(final LogEvent event, final StringBuilder toAppendTo) {
373 final StringBuilder buf = new StringBuilder();
374 for (final PatternFormatter formatter : formatters) {
375 formatter.format(event, buf);
376 }
377 if (buf.length() > 0) {
378 toAppendTo.append(style).append(buf.toString()).append(AnsiEscape.getDefaultStyle());
379 }
380 }
381 }