Today i’d like to show you how to make Logstash Docker container output its operation to a log file inside the container. I’m writing specifically about this, because the official Logstash documentation is a bit vague and unless you know how Java (the language ELK stack is written in) logging with the third party library log4j2 works, you might struggle with this issue like me.
Why would you need to log the operation of Logstash?
Some might ask, “why would you need logging for Logstash” - it’s a tool that i use to save my other logs into Elastic Search, right? Logging is useful for many reasons. In my case, i wanted to be able to easily debug possible problems and logs would let me reproduce the issues with low effort, using the input data from the pipeline. Since i am using a custom Logstash plugin that i wrote in Ruby, one other reason for logging was to check if i don’t lose any data being processed by Logstash, by writing the output to the log. In other cases, you may lose this to monitor your Logstash availability and liveness, to detect and prevent security issues, to analyze data flow and many more.
Okay, okay, give us the solution…
No more small talk - to begin with, let’s look at the official documentation at this page. It mentions two ways of enabling logging - through the Logstash API and through the Log4j2 configuration. We are interested in the latter, as any configuration through the API is not persistent and must be done every time Logstash is started, restarted (including crashes and outages) or updated, since this will require restart too.
To make this configuration, we need to change the log4j2.properties file, found under /usr/share/logstash/config. Bellow i will show the original contents of this file and the modifications that i made for my logging purposes. You may change these configurations based on what you want to log, using the official log4j2 documentation at this page.
# Original configuration
# Modified configuration
After making these changes, you can select log format and log paths in your logstash.yml file, located in the same folder as the properties file.
I’ve set the log folder to be under the Logstash home folder, to avoid any write permissions problems.
Wait, so what did we just do?
By default, the Logstash Docker container is configured to output logs only to the Console (Terminal). In other words, even if you configure a logs folder path and log format, you will not get any files. This may be obvious for some people, but i struggled for a long time, until i found that i also had to define a file appender in log4j2.properties and register it to the rootLogger object, to get output in my log files folder. For those of you that were just like me, i hope this article helped you save some time. Cheers and good luck!