@@ -43,17 +43,24 @@ public final class DateProcessor extends AbstractProcessor {
43
43
44
44
public static final String TYPE = "date" ;
45
45
static final String DEFAULT_TARGET_FIELD = "@timestamp" ;
46
- private static final DateFormatter FORMATTER = DateFormatter . forPattern ( "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) ;
46
+ static final String DEFAULT_OUTPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ;
47
47
48
+ private final DateFormatter formatter ;
48
49
private final TemplateScript .Factory timezone ;
49
50
private final TemplateScript .Factory locale ;
50
51
private final String field ;
51
52
private final String targetField ;
52
53
private final List <String > formats ;
53
54
private final List <Function <Map <String , Object >, Function <String , ZonedDateTime >>> dateParsers ;
55
+ private final String outputFormat ;
54
56
55
57
DateProcessor (String tag , String description , @ Nullable TemplateScript .Factory timezone , @ Nullable TemplateScript .Factory locale ,
56
58
String field , List <String > formats , String targetField ) {
59
+ this (tag , description , timezone , locale , field , formats , targetField , DEFAULT_OUTPUT_FORMAT );
60
+ }
61
+
62
+ DateProcessor (String tag , String description , @ Nullable TemplateScript .Factory timezone , @ Nullable TemplateScript .Factory locale ,
63
+ String field , List <String > formats , String targetField , String outputFormat ) {
57
64
super (tag , description );
58
65
this .timezone = timezone ;
59
66
this .locale = locale ;
@@ -65,6 +72,8 @@ public final class DateProcessor extends AbstractProcessor {
65
72
DateFormat dateFormat = DateFormat .fromString (format );
66
73
dateParsers .add ((params ) -> dateFormat .getFunction (format , newDateTimeZone (params ), newLocale (params )));
67
74
}
75
+ this .outputFormat = outputFormat ;
76
+ formatter = DateFormatter .forPattern (this .outputFormat );
68
77
}
69
78
70
79
private ZoneId newDateTimeZone (Map <String , Object > params ) {
@@ -99,7 +108,7 @@ public IngestDocument execute(IngestDocument ingestDocument) {
99
108
throw new IllegalArgumentException ("unable to parse date [" + value + "]" , lastException );
100
109
}
101
110
102
- ingestDocument .setFieldValue (targetField , FORMATTER .format (dateTime ));
111
+ ingestDocument .setFieldValue (targetField , formatter .format (dateTime ));
103
112
return ingestDocument ;
104
113
}
105
114
@@ -128,6 +137,10 @@ List<String> getFormats() {
128
137
return formats ;
129
138
}
130
139
140
+ String getOutputFormat () {
141
+ return outputFormat ;
142
+ }
143
+
131
144
public static final class Factory implements Processor .Factory {
132
145
133
146
private final ScriptService scriptService ;
@@ -153,8 +166,16 @@ public DateProcessor create(Map<String, Processor.Factory> registry, String proc
153
166
"locale" , localeString , scriptService );
154
167
}
155
168
List <String > formats = ConfigurationUtils .readList (TYPE , processorTag , config , "formats" );
169
+ String outputFormat =
170
+ ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "output_format" , DEFAULT_OUTPUT_FORMAT );
171
+ try {
172
+ DateFormatter .forPattern (outputFormat );
173
+ } catch (Exception e ) {
174
+ throw new IllegalArgumentException ("invalid output format [" + outputFormat + "]" , e );
175
+ }
176
+
156
177
return new DateProcessor (processorTag , description , compiledTimezoneTemplate , compiledLocaleTemplate , field , formats ,
157
- targetField );
178
+ targetField , outputFormat );
158
179
}
159
180
}
160
181
}
0 commit comments