DEV Community

Cover image for 5 Minute Solution for Viewing Logs in a Laravel App
Adi Sk
Adi Sk

Posted on • Originally published at simplestweb.in

5 Minute Solution for Viewing Logs in a Laravel App

Introduction

Hi All, it’s been a few months since my last Laravel related post. So I decided to write something small to get back into the schedule. I plan to publish something every month or even more frequently.

We have all been there, your Laravel app isn't working as expected and you want to get to logs as fast as possible. In most situations we may not have easy access to the log files because we need an ssh client to connect to the server, then navigate to the logs and finally view the last entry in there. It's always a pain in the neck to do this. In this post, I want to show you my simple solution to viewing logs from within the app. I implemented this on a recent client project and it works quite well.

As always, if you have a better implementation, please share, I would love to know about it.

TLDR;

Here’s the overview of my solution. I have a route to a page, who’s controller reads that day’s log file and sends to the view. The view has 2 parts to it, first, it has a form to change the date of the currently viewed file and secondly the part where the logs are shown.

Pretty simple aye. If it was not clear enough, check the code from below.

Code

First off, add a route where you want to display the log file entries
Route file content

Then comes the controller, below is the logic for the controller. Here we get the date for when we need the logs for. Then we extract information about the file and it's contents and send it to the view.

Controller file contents

Here in the view, we have 2 parts, one to change the date of the Log file and the other is to view the actual Log file's contents.
View file contents

Once you put everything together, you will have your own Log Viewer. The image below is what I have after adding some CSS. Not bad for like 5 minutes of work, ah.
Final result

Conclusion

I hope this solution is of some use to you. If you have a better solution to do the same, do let them know. I am more than glad to try it.

Related resources

That's all for now. Talk to you in the next one.
More about me at SimplestWeb.in

Top comments (0)