@@ -43,17 +43,24 @@ public final class DateProcessor extends AbstractProcessor {
4343
4444 public static final String TYPE = "date" ;
4545 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" ;
4747
48+ private final DateFormatter formatter ;
4849 private final TemplateScript .Factory timezone ;
4950 private final TemplateScript .Factory locale ;
5051 private final String field ;
5152 private final String targetField ;
5253 private final List <String > formats ;
5354 private final List <Function <Map <String , Object >, Function <String , ZonedDateTime >>> dateParsers ;
55+ private final String outputFormat ;
5456
5557 DateProcessor (String tag , String description , @ Nullable TemplateScript .Factory timezone , @ Nullable TemplateScript .Factory locale ,
5658 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 ) {
5764 super (tag , description );
5865 this .timezone = timezone ;
5966 this .locale = locale ;
@@ -65,6 +72,8 @@ public final class DateProcessor extends AbstractProcessor {
6572 DateFormat dateFormat = DateFormat .fromString (format );
6673 dateParsers .add ((params ) -> dateFormat .getFunction (format , newDateTimeZone (params ), newLocale (params )));
6774 }
75+ this .outputFormat = outputFormat ;
76+ formatter = DateFormatter .forPattern (this .outputFormat );
6877 }
6978
7079 private ZoneId newDateTimeZone (Map <String , Object > params ) {
@@ -99,7 +108,7 @@ public IngestDocument execute(IngestDocument ingestDocument) {
99108 throw new IllegalArgumentException ("unable to parse date [" + value + "]" , lastException );
100109 }
101110
102- ingestDocument .setFieldValue (targetField , FORMATTER .format (dateTime ));
111+ ingestDocument .setFieldValue (targetField , formatter .format (dateTime ));
103112 return ingestDocument ;
104113 }
105114
@@ -128,6 +137,10 @@ List<String> getFormats() {
128137 return formats ;
129138 }
130139
140+ String getOutputFormat () {
141+ return outputFormat ;
142+ }
143+
131144 public static final class Factory implements Processor .Factory {
132145
133146 private final ScriptService scriptService ;
@@ -153,8 +166,16 @@ public DateProcessor create(Map<String, Processor.Factory> registry, String proc
153166 "locale" , localeString , scriptService );
154167 }
155168 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+
156177 return new DateProcessor (processorTag , description , compiledTimezoneTemplate , compiledLocaleTemplate , field , formats ,
157- targetField );
178+ targetField , outputFormat );
158179 }
159180 }
160181}
0 commit comments