Google.
File_get_contents() returns a files data as a string. Then sort as needed.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
What would be the best way to parse out the date, environment, level and message from each log entry in one of Laravel's log files?
I know that there are packages that do this for you, but I'd rather not use them in this case.
@Ackirb if you really want to process log files the easiest way to do so would propably be using fgets in combination with a regular expression (preg_match). You have to keep in mind though that log messages may span over multiple lines.
The Monolog LineFormatter class contains the default format used for log lines, this should help you create your regex.
However, have you considered using a custom log handler / formatter / logger? Log files are not really meant to be parsed. Laravel uses Monlog itself so you could e.g. use a handler to write log entries to a Redis or an Elasticsearch server (or even a mySQL database).
I highly recommend using your own handler especially if you want to build a view for your log entries and make them searchable through some kind of interface.
Please or to participate in this conversation.