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