# Extract File Name from source

**URL:** <https://discuss.elastic.co/t/extract-file-name-from-source/73288>\
**Category:** Logstash\
**Created:** [January 30, 2017, 9:35pm UTC](https://discuss.elastic.co/t/extract-file-name-from-source/73288 "2017-01-30T21:35:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sde/32/45707_2.png) [@sde](https://discuss.elastic.co/u/sde)\
**Post date:** [January 30, 2017, 9:35pm UTC](https://discuss.elastic.co/t/extract-file-name-from-source/73288/1 "2017-01-30T21:35:06Z")

</div>

Hello,

I am trying to read log file using Filebeat from an application server and write to a remote directory using Logstash.

I am not able to extract the filename from the source field. Need help to extract the file name so use the same file name as output.

stdout:

Sending logstash logs to /var/log/logstash/logstash.log.  
{  
"message" =\> "2017-01-30 16:21:32,276 INFO accessLogger - Tracking\_Id:1, User\_Name:apple, Service\_Name:WCustomerService, Method\_Name:wEGetCustomerSeasonalStatus, Authenticated:true",  
"@version" =\> "1",  
"@timestamp" =\> "2017-01-30T21:21:39.750Z",  
"source" =\> "/app/tomcat8080/logs/DATAWAR\_access.2017-01-30.log",  
"offset" =\> 5761,  
"type" =\> "log",  
"input\_type" =\> "log",  
"beat" =\> {  
"hostname" =\> "[daspoc-wc-a01d.sys.abc.net](http://daspoc-wc-a01d.sys.abc.net)",  
"version" =\> "5.1.2",  
"name" =\> "[daspoc-wc-a01d.sys.abc.net](http://daspoc-wc-a01d.sys.abc.net)"  
},  
"host" =\> "[daspoc-wc-a01d.sys.abc.net](http://daspoc-wc-a01d.sys.abc.net)",  
"tags" =\> [  
[0] "beats\_input\_codec\_plain\_applied",  
[1] "\_grokparsefailure"  
]  
}

logstash.conf  
input {  
beats {  
port =\> 5000  
}  
}

filter {  
grok {  
match =\> ["path","/app/tomcat8080/logs/%{DATA:filename}.log"]  
}  
}  
output {  
file {  
path =\>"/var/log/logstash/%{filename}"  
codec =\> line { format =\> "%{message}"}  
}  
stdout { codec =\> rubydebug }  
}

---

<div class="post-metadata">

**Author:** ![sde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sde/32/45707_2.png) [@sde](https://discuss.elastic.co/u/sde)\
**Post date:** [January 31, 2017, 3:53am UTC](https://discuss.elastic.co/t/extract-file-name-from-source/73288/2 "2017-01-31T03:53:11Z")

</div>

Please ignore...found the soln.

input {  
beats {  
port =\> 5000  
}  
}

filter {  
ruby {  
code =\> "event['filename'] = event['source'].split('/').last"  
}  
}

output {

```
  file {
           path =>"/var/log/logstash/%{filename}"
           codec => line { format => "%{message}"}
   }

    stdout { codec => rubydebug }

```

}

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [January 31, 2017, 7:27am UTC](https://discuss.elastic.co/t/extract-file-name-from-source/73288/3 "2017-01-31T07:27:33Z")

</div>

Your original grok filter looked fine except that it tried to match against the non-existent `path` field instead of the `source` field.

---

<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:** [February 28, 2017, 7:27am UTC](https://discuss.elastic.co/t/extract-file-name-from-source/73288/4 "2017-02-28T07:27:46Z")

</div>

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