laravel log system with logstash
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Centralizing Laravel Logs: Shipping Data from Application to Logstash and Kibana
As developers, the ability to aggregate logs from disparate services into a single, searchable platform is crucial for monitoring performance, debugging issues, and understanding application behavior. You have successfully set up your Elasticsearch and Kibana stack, which is the perfect foundation for centralized logging. The next challenge is bridging the gap: getting those logs generated by your Laravel application into Logstash so they can be indexed and visualized in Kibana.
Many developers search for a direct "Laravel to Logstash package," but often the most robust solutions involve leveraging existing log shippers rather than writing complex, custom drivers directly into the Monolog configuration. Let's explore the most effective, production-ready methods for achieving this integration.
Understanding the Logging Pipeline
The journey of a Laravel log message involves several stages:
- Generation: Laravel uses Monolog to write messages (INFO, ERROR, DEBUG) to a configured handler (file, database, Syslog).
- Collection: The log file is generated on the application server.
- Shipping: A mechanism must read this file and forward the data over the network to Logstash.
- Processing & Indexing: Logstash ingests the data, parses it (using Grok or JSON filters), and indexes it in Elasticsearch.
The complexity often lies in Step 3—the shipping mechanism. While you attempted a custom Monolog driver for Logstash, a more standard approach is to let an external agent handle the heavy lifting of file monitoring, which offers better reliability and separation of concerns.
Method 1: The Robust Approach – Filebeat/Fluentd Integration
For most modern deployments, the recommended path is to treat your application logs as raw files and use a dedicated log shipper like Filebeat (from Elastic) or Fluentd to monitor those files and send them to Logstash. This avoids tightly coupling the application layer with external logging infrastructure.
Step 1: Ensure Logs are Written in a Parseable Format
Ensure your Laravel logs are written in a format that is easy for Logstash to parse, ideally JSON or a simple structured text format. While Monolog outputs plain text, we can instruct it to output JSON if necessary, or rely on the shipper to do the parsing.
Step 2: Configure the Shipper (Filebeat Example)
If you are running this setup on a Linux server hosting your Laravel application, Filebeat is an excellent choice. You configure Filebeat to watch the directory where Laravel stores its logs (storage/logs/laravel.log).
In your Filebeat configuration file (filebeat.yml), you would define a file input:
filebeat.inputs:
- type: log
id: laravel-app-logs
paths:
- /var/www/html/your_laravel_app/storage/logs/*.log
fields:
log_type: "laravel"
Filebeat will continuously tail these files, read new lines, and forward them as streams to your Logstash input. This method is highly resilient because it operates independently of the PHP application process itself.
Method 2: Custom Monolog Handler (For Specific Needs)
Your attempt to use a custom driver ('logstash' => ['driver' => 'custom', 'via' => \App\LogstashLogger::class, ...]) is an advanced approach. This method allows you to intercept the log event before it hits the file and format it specifically for Logstash.
If you choose this route, your custom logger must format the message payload into a JSON object before sending it via an HTTP request to your Logstash endpoint (port 9200). This requires careful implementation within your App\LogstashLogger class to ensure correct field mapping and error handling. For deep dives into customizing Laravel components, checking the official documentation on laravelcompany.com regarding dependency injection and service providers is always a good first step before writing custom drivers.
Conclusion
While creating a direct Monolog driver is an option, leveraging established log shippers like Filebeat or Fluentd provides a more scalable, decoupled, and maintainable solution for shipping Laravel logs to Logstash. This separation ensures that if your logging destination changes, you only need to update the shipper configuration, not the core application code. By adopting this pipeline, you establish a robust system where your Laravel application focuses on business logic, and the monitoring stack handles the data flow efficiently.