|
11 | 11 | */
|
12 | 12 | public final class LayoutPatterns {
|
13 | 13 |
|
14 |
| - public static enum PATTERN_KEY { |
15 |
| - APPLICATION, EXCEPTION, REQUEST |
16 |
| - } |
| 14 | + public static enum PATTERN_KEY { |
| 15 | + APPLICATION, EXCEPTION, REQUEST |
| 16 | + } |
17 | 17 |
|
18 |
| - /* |
19 |
| - * -- this is the common prefix to all variants. -- the final line will add |
20 |
| - * non-predefined context parameters from the MDC -- as this list may be |
21 |
| - * empty, we use "replace" to at a colon if it's not |
22 |
| - */ |
23 |
| - private static final String COMMON_PREFIX_PATTERN = "{ " + JSON_FIELD(Fields.WRITTEN_AT, |
24 |
| - "%d{yyyy-MM-dd'T'HH:mm:ss.SSSX,UTC}", true, |
25 |
| - true) + JSON_FIELD(Fields.WRITTEN_TS, |
26 |
| - "%tstamp", false, false) + |
27 |
| - "%replace(%ctxp){'(.+)', ',$1'},"; |
| 18 | + /* |
| 19 | + * -- this is the common prefix to all variants. -- the final line will add |
| 20 | + * non-predefined context parameters from the MDC -- as this list may be empty, |
| 21 | + * we use "replace" to at a colon if it's not |
| 22 | + */ |
| 23 | + private static final String COMMON_PREFIX_PATTERN = "{ " |
| 24 | + + JSON_FIELD(Fields.WRITTEN_AT, "%d{yyyy-MM-dd'T'HH:mm:ss.SSSX,UTC}", true, true) |
| 25 | + + JSON_FIELD(Fields.WRITTEN_TS, "%tstamp", false, false) + "%replace(%ctxp){'(.+)', ',$1'},"; |
28 | 26 |
|
29 |
| - /* |
30 |
| - * -- all layout patterns end like this |
31 |
| - */ |
32 |
| - private static final String COMMON_POSTFIX_PATTERN = "}%n"; |
| 27 | + /* |
| 28 | + * -- all layout patterns end like this |
| 29 | + */ |
| 30 | + private static final String COMMON_POSTFIX_PATTERN = "}%n"; |
33 | 31 |
|
34 |
| - /* |
35 |
| - * -- for standard application log messages we always add these fields. -- |
36 |
| - * note the last line where we make sure that the original message string -- |
37 |
| - * is quoted and properly escaped and that "custom fields" are added |
38 |
| - */ |
39 |
| - private static final String APP_PREFIX_PATTERN = JSON_FIELD(Fields.TYPE, Defaults.TYPE_LOG, true, true) + |
40 |
| - JSON_FIELD(Fields.LOGGER, "%logger", true, true) + JSON_FIELD( |
41 |
| - Fields.THREAD, "%thread", true, true) + |
42 |
| - JSON_FIELD(Fields.LEVEL, "%p", true, true) + JSON_FIELD( |
43 |
| - Fields.CATEGORIES, |
44 |
| - "%categories", |
45 |
| - false, |
46 |
| - true) + |
47 |
| - JSON_FIELD(Fields.MSG, |
48 |
| - "%jsonmsg{escape}%replace(%args{custom_fields}){'(.+)', ',$1'}", |
49 |
| - false, false); |
| 32 | + /* |
| 33 | + * -- for standard application log messages we always add these fields. -- note |
| 34 | + * the last line where we make sure that the original message string -- is |
| 35 | + * quoted and properly escaped and that "custom fields" are added |
| 36 | + */ |
| 37 | + private static final String APP_PREFIX_PATTERN = JSON_FIELD(Fields.TYPE, Defaults.TYPE_LOG, true, true) |
| 38 | + + JSON_FIELD(Fields.LOGGER, "%replace(%logger){'\"','\\\\\"'}", true, true) |
| 39 | + + JSON_FIELD(Fields.THREAD, "%replace(%thread){'\"','\\\\\"'}", true, true) |
| 40 | + + JSON_FIELD(Fields.LEVEL, "%p", true, true) + JSON_FIELD(Fields.CATEGORIES, "%categories", false, true) |
| 41 | + + JSON_FIELD(Fields.MSG, "%jsonmsg{escape}%replace(%args{custom_fields}){'(.+)', ',$1'}", false, false); |
50 | 42 |
|
51 |
| - /* |
52 |
| - * -- a simple application log message does not include exception/stack |
53 |
| - * trace info |
54 |
| - */ |
55 |
| - private static final String APP_LOG_PATTERN = COMMON_PREFIX_PATTERN + APP_PREFIX_PATTERN + "%nopex " + |
56 |
| - COMMON_POSTFIX_PATTERN; |
| 43 | + /* |
| 44 | + * -- a simple application log message does not include exception/stack trace |
| 45 | + * info |
| 46 | + */ |
| 47 | + private static final String APP_LOG_PATTERN = COMMON_PREFIX_PATTERN + APP_PREFIX_PATTERN + "%nopex " |
| 48 | + + COMMON_POSTFIX_PATTERN; |
57 | 49 |
|
58 |
| - /* |
59 |
| - * -- if the application log was written including stack trace info, write |
60 |
| - * it out! -- to avoid any trouble with our event processing pipeline that |
61 |
| - * expect a single -- line message, make sure newlines and tabs are escaped. |
62 |
| - * -- newlines are actually escaped with additional surrounding spaces to |
63 |
| - * make the -- standard ES tokenizer work |
64 |
| - */ |
65 |
| - private static final String APP_EX_LOG_PATTERN = COMMON_PREFIX_PATTERN + APP_PREFIX_PATTERN + "," + JSON_FIELD( |
66 |
| - Fields.STACKTRACE, |
67 |
| - "%stacktrace", |
68 |
| - false, |
69 |
| - false) + |
70 |
| - "%nopex " + COMMON_POSTFIX_PATTERN; |
| 50 | + /* |
| 51 | + * -- if the application log was written including stack trace info, write it |
| 52 | + * out! -- to avoid any trouble with our event processing pipeline that expect a |
| 53 | + * single -- line message, make sure newlines and tabs are escaped. -- newlines |
| 54 | + * are actually escaped with additional surrounding spaces to make the -- |
| 55 | + * standard ES tokenizer work |
| 56 | + */ |
| 57 | + private static final String APP_EX_LOG_PATTERN = COMMON_PREFIX_PATTERN + APP_PREFIX_PATTERN + "," |
| 58 | + + JSON_FIELD(Fields.STACKTRACE, "%stacktrace", false, false) + "%nopex " + COMMON_POSTFIX_PATTERN; |
71 | 59 |
|
72 |
| - /* |
73 |
| - * -- if we write a request log/"beat" all we want/need is already in the |
74 |
| - * message (object), but -- we need to flatten it as we don't want nested |
75 |
| - * JSON objects. |
76 |
| - */ |
77 |
| - private static final String REQUEST_METRICS_PATTERN = COMMON_PREFIX_PATTERN + JSON_FIELD(Fields.TYPE, |
78 |
| - Defaults.TYPE_REQUEST, |
79 |
| - true, true) + |
80 |
| - "%jsonmsg{flatten}%nopex" + COMMON_POSTFIX_PATTERN; |
| 60 | + /* |
| 61 | + * -- if we write a request log/"beat" all we want/need is already in the |
| 62 | + * message (object), but -- we need to flatten it as we don't want nested JSON |
| 63 | + * objects. |
| 64 | + */ |
| 65 | + private static final String REQUEST_METRICS_PATTERN = COMMON_PREFIX_PATTERN |
| 66 | + + JSON_FIELD(Fields.TYPE, Defaults.TYPE_REQUEST, true, true) + "%jsonmsg{flatten}%nopex" |
| 67 | + + COMMON_POSTFIX_PATTERN; |
81 | 68 |
|
82 |
| - public static String getPattern(PATTERN_KEY key) { |
83 |
| - switch (key) { |
84 |
| - case REQUEST: |
85 |
| - return REQUEST_METRICS_PATTERN; |
| 69 | + public static String getPattern(PATTERN_KEY key) { |
| 70 | + switch (key) { |
| 71 | + case REQUEST: |
| 72 | + return REQUEST_METRICS_PATTERN; |
86 | 73 |
|
87 |
| - case EXCEPTION: |
88 |
| - return APP_EX_LOG_PATTERN; |
| 74 | + case EXCEPTION: |
| 75 | + return APP_EX_LOG_PATTERN; |
89 | 76 |
|
90 |
| - case APPLICATION: |
91 |
| - default: |
92 |
| - return APP_LOG_PATTERN; |
93 |
| - } |
94 |
| - } |
| 77 | + case APPLICATION: |
| 78 | + default: |
| 79 | + return APP_LOG_PATTERN; |
| 80 | + } |
| 81 | + } |
95 | 82 | }
|
0 commit comments