# How to pass date from messages to output filename in logstash

**URL:** <https://discuss.elastic.co/t/how-to-pass-date-from-messages-to-output-filename-in-logstash/108653>\
**Category:** Logstash\
**Created:** [November 22, 2017, 7:07am UTC](https://discuss.elastic.co/t/how-to-pass-date-from-messages-to-output-filename-in-logstash/108653 "2017-11-22T07:07:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adi\_info](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@adi\_info](https://discuss.elastic.co/u/adi_info)\
**Post date:** [November 22, 2017, 7:07am UTC](https://discuss.elastic.co/t/how-to-pass-date-from-messages-to-output-filename-in-logstash/108653/1 "2017-11-22T07:07:42Z")

</div>

I have a requirement to name the output file based on the date present in input messages.

I want to assign the incoming date to metadata and then use it in the output like

path =\> /path/location/filename\_%{[@metadata][log\_date]}.log

I am on logstast2.2 and giving %{+yyyy-MM-dd} directly in path is not working. Please help

---

<div class="post-metadata">

**Author:** ![ericohtake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericohtake/32/24539_2.png) [@ericohtake](https://discuss.elastic.co/u/ericohtake)\
**Post date:** [November 22, 2017, 7:57am UTC](https://discuss.elastic.co/t/how-to-pass-date-from-messages-to-output-filename-in-logstash/108653/2 "2017-11-22T07:57:11Z")

</div>

I've tried in the following 2 ways:

Where my events have the timestamp in the @timestamp field:

```
file {
      path => "/tmp/log-%{@timestamp}"
}

```

Result files (each event in its own file):

> log-2017-08-31T15:00:04.314Z  
> log-2017-08-31T16:00:04.314Z  
> log-2017-08-31T17:00:04.314Z  
> log-2017-09-01T08:00:04.314Z  
> log-2017-09-01T10:00:04.314Z

Or using the way you described:

```
file {
      path => "/tmp/log-%{+YYYY-MM-dd}"
}

```

Result files:

> log-2017-08-31 (all events of this day inside this file)  
> log-2017-09-01 (all events of this day inside this file)

I think the version 2.2 supports both methods.  
[https://www.elastic.co/guide/en/logstash/2.2/plugins-outputs-file.html#plugins-outputs-file-path](https://www.elastic.co/guide/en/logstash/2.2/plugins-outputs-file.html#plugins-outputs-file-path)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 20, 2017, 7:57am UTC](https://discuss.elastic.co/t/how-to-pass-date-from-messages-to-output-filename-in-logstash/108653/3 "2017-12-20T07:57:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
