Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logstash: how to add file name as a field?

Tags:

logstash

I'm using Logstash + Elasticsearch + Kibana to have an overview of my Tomcat log files.

For each log entry I need to know the name of the file from which it came. I'd like to add it as a field. Is there a way to do it? I've googled a little and I've only found this SO question, but the answer is no longer up-to-date.

So far the only solution I see is to specify separate configuration for each possible file name with different "add_field" like so:

input {   file {      type => "catalinalog"      path => [ "/path/to/my/files/catalina**" ]      add_field => { "server" => "prod1" }   } } 

But then I need to reconfigure logstash each time there is a new possible file name. Any better ideas?

like image 252
machinery Avatar asked Apr 07 '14 15:04

machinery


People also ask

What is Sincedb in Logstash?

sincedb_path just needs to be a directory where logstash has write permission for the registry. sincedb_write_interval defines how often logstash should write the sincedb registry. A larger value puts you at risk in logstash were to crash.

What is Logstash file?

Logstash is the “L” in the ELK Stack — the world's most popular log analysis platform and is responsible for aggregating data from different sources, processing it, and sending it down the pipeline, usually to be directly indexed in Elasticsearch.


2 Answers

Hi I added a grok filter to do just this. I only wanted to have the filename not the path, but you can change this to your needs.

filter {   grok {     match => ["path","%{GREEDYDATA}/%{GREEDYDATA:filename}\.log"]   } } 
like image 104
Jettro Coenradie Avatar answered Oct 06 '22 13:10

Jettro Coenradie


In case you would like to combine the message and file name in one event:

filter { grok {     match => {          message => "ERROR (?<function>[\S]*)"         } } grok {     match => {          path => "%{GREEDYDATA}/%{GREEDYDATA:filename}\.log"         } }}   

The result in ElasticSearch (focus on 'filename' and 'function' fields):

"_index": "logstash-2016.08.03",     "_type": "logs",     "_id": "AVZRyEI49-A6kyBCq6Yt",     "_score": 1,     "_source": {       "message": "27/07/16 12:16:18,321 ERROR blaaaaaaaaa.internal.com",       "@version": "1",       "@timestamp": "2016-08-03T19:01:33.083Z",       "path": "/home/admin/mylog.log",       "host": "my-virtual-machine",       "function": "blaaaaaaaaa.internal.com",       "filename": "mylog"     } 
like image 33
Vladik Y Avatar answered Oct 06 '22 12:10

Vladik Y