Filebeat - Parse Fields From Message Line

Better Stack Team
Updated on November 18, 2024

To parse fields from a message line in Filebeat, you can use the grok processor. The grok processor allows you to extract structured data from log messages using regular expressions. Here's a step-by-step guide on how to set this up:

1. Define the Grok Pattern

First, you need to define the grok pattern that matches the format of your log messages and extracts the fields you need. For example, if your log message looks like this:

 
2024-09-16 14:25:30 INFO [app] User logged in: user_id=1234, username=johndoe

You might want to extract timestamp, level, app, user_id, and username fields.

2. Configure Filebeat to Use the Grok Processor

Edit your Filebeat configuration file (usually filebeat.yml) to include the grok processor. Here’s an example configuration:

 
filebeat.inputs:
  - type: log
    paths:
      - /var/log/myapp/*.log

processors:
  - grok:
      patterns:
        - '%{TIMESTAMP_ISO8601:timestamp} %{LOGLEVEL:level} \\[%{DATA:app}\\] User logged in: user_id=%{INT:user_id}, username=%{USERNAME:username}'
      on_failure:
        - drop_event

Explanation of the Configuration:

  • patterns: This specifies the grok pattern to match and parse fields from the log messages. The pattern used here is:
    • %{TIMESTAMP_ISO8601:timestamp}: Matches and extracts the timestamp.
    • %{LOGLEVEL:level}: Matches and extracts the log level (e.g., INFO, ERROR).
    • \\[%{DATA:app}\\]: Matches and extracts the application name enclosed in square brackets.
    • User logged in: user_id=%{INT:user_id}, username=%{USERNAME:username}: Matches and extracts the user_id and username fields from the message.
  • on_failure: This specifies actions to take if the grok pattern fails to parse a log line. In this case, the event will be dropped if parsing fails.

3. Test and Validate

Make sure to test your configuration to ensure that the grok pattern correctly parses the log lines and extracts the fields as expected. You can use tools like the Grok Debugger to test your patterns.

4. Start Filebeat

Once you’ve configured Filebeat, restart or start the Filebeat service to apply the new configuration:

 
sudo systemctl restart filebeat

Additional Notes:

  • Custom Patterns: If the default grok patterns don’t fit your log format, you can define custom patterns in your Filebeat configuration file.
  • Multiple Patterns: If you have different log formats, you can specify multiple patterns in the patterns list.
  • Field Overwriting: If you have fields with the same name in different parts of your configuration, make sure to handle potential field overwriting.

Using Filebeat’s grok processor effectively allows you to structure and enrich your log data before sending it to Elasticsearch or Logstash.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github