In this tutorial, I will show you how to parse JSON logs in Logstash. There are two ways to parse JSON logs in Logstash:
- Parse JSON logs from a file
- Parse JSON logs from a string field in an event
Let's discover each way in detail.
Contents
Parse JSON from a File
To parse JSON logs from a file in Logstash config, you should use the multiline
codec to input the file and use the json
filter to parse the JSON logs.
Here is the explanation:
- I used the
multiline
codec to read the file line by line, each line will be an event and will be stored in themessage
field. - I used the
json
filter to parse themessage
field into a JSON object.
For example, I have a JSON log file like this:
For JSON data like below:
Please visit the Logstash Input from JSON File tutorial.
After running Logstash with the above config, I will get the output like this:
Parse JSON from a String Field
If you have an event or document that has a string field which contains a JSON object, you can use the json
filter to parse that field into a JSON object.
For example, I have a document like this:
To parse the message
field into a JSON object, I will use the json
filter with specfied source
field:
After running Logstash with the above config, I will get the output like this:
The json
filter also allows you to change the target field name by using the target
option:
After running Logstash with the above config, I will get the output like this:
Conclusion
I have shown you 2 ways to parse JSON logs in Logstash. Hope it helps you!
Comments
Be the first to comment!