logstash filter

In ELK, Logstash handles the resource-heavy task of aggregating and processing logs. The processing work performed by Logstash makes sure our log messages are parsed and structured correctly, and it is this structure that enables you to analyze and visualize the data more easily post indexing in Elasticsearch.

What exact processing is performed on the data is determined by you in the filter section of your Logstash configuration files. In this section you can choose from a large number of both officially supported and community filter plugins to determine how exactly to transform the logs. The most commonly used filter plugin is grok, but there are a number of other extremely useful plugins you can use.

Which plugin you use will of course depend on the logs themselves, but this article tries to list five of the plugins you will most likely find useful in any logging pipeline that involves Logstash.

# 1 grok

As mentioned above, grok is by far the most commonly used filter plugin in Logstash. Despite the fact that it is not easy to use, grok is popular because what it allows you to do is give structure to unstructured logs.

Take this random log message for example:

The grok pattern we will use looks like this:

After processing, the log message will be parsed as follows:

This is how Elasticsearch indexes the log message. Sorted out in this format, the log message has been broken up into logically-named fields which can be then queried, analyzed and visualized more easily.

More information about how grok works and how to use it can be found in this article.

# 2 mutate 

Another common Logstash filter plugin is mutate. As its name implies, this filter allows you to really massage your log messages by “mutating” the various fields. You can, for example, use the filter to change fields, join them together, rename them, and more. 

Using the log above as an example, using the lowercase configuration option for the mutate plugin, we can transform the ‘log-level’ field into lowercase: 

The mutate plugin is a great way to change the format of your logs. A full list of the different configuration options for the plugin is listed here.

# 3 date 

How can you analyze logs and events if they are not accurately sorted in chronological order?  

The Logstash date filter plugin can be used to pull a time and date from a log message and define it as the timestamp field (@timestamp) for the log. Once defined, this timestamp field will sort out the logs in the correct chronological order and help you analyze them more effectively. 

There are tens, if not hundreds, of different ways time and date can be formatted in logs.  

Here is an example of an Apache access log:  

Using the date filter as follows, we can extract the date and time pattern and define it as the @timestamp field according to which all are logs will be sorted by:

It’s important to note that if you do not use the date filter, Logstash will automatically set a timestamp based on the input time.

Read about additional configuration options here. 

# 4 json 

JSON is an extremely popular format for logs because it allows users to write structured and standardized messages that can be easily read and analyzed.  

To maintain the JSON structure of either an entire message or a specific field, the Logstash json filter plugin enables you to extract and maintain the JSON data structure within the log message. 

The example below is an Apache access log formatted as a JSON: 

Instead of having the log flattened into one line, we can use the json filter to retain the data structure:

The source configuration option defines which field in the log is the JSON you wish to parse. In this example, the entire message field is a JSON. I’m also using the target option to expand the JSON into a data structure within a field called log.

Read about additional configuration options here. 

# 5 kv 

Key value pairs, or KVPs, is another commonly used logging format. Like JSON, this format is popular mainly because it is readable, and the Logstash kv filter plugin allows you to automatically parse messages or specific fields formatted this way. 

Take this log as an example:

I can use the following kv filter to instruct Logstash how to process it:

Note the usage here of configuration options. I’m using source to define the field to perform the key=value searching on, trim to omit specific characters, include_keys to specify the parsed keys which should be added to the log, and target to define the container into which all the key-pair values are to be placed in.

Read about additional configuration options here 

Summary 

As I said at the beginning of the article, there is a huge amount of Logstash filter plugins at your disposal. Which one you use greatly depends of course on the specific log message you want to process.  

Other extremely useful filter plugins that are worth mentioning are the geoip (for adding geographical data for IP fields) and csv (for parsing CSV logs) plugins.

While each and every one of these plugins is useful in its own right, their full power is unleashed when used together to parse logs. Indeed, in most cases, you will most likely be using a combination of grok and at least one or two additional plugins. This combined usage will guarantee your logs come out on the other end of Logstash perfectly formatted!

Easily Configure and Ship Logs with Logz.io ELK as a Service.