LARAVEL

How to handle logs and reports in your app using Laravel 5 – Part 2


We Built a ShoutPad a.k.a ShoutBox in Part 1.

Now, you can log anything in your app.

From the creation of users, to users editing their profiles, to deletion of accounts to ever event happening in your app.

In this tutorial, we’ll log users giving shoutouts. I’ll show you how to log to a file and to external services using Laravel 5.

Let’s get Started

Laravel 5 comes shipped with the Log Facade. Out of the box, Laravel supports single, daily, syslog and errorlog logging modes.

Open your config/app.php, you will see something like this:

By default, it is set to a single file.

We have different levels of logging:

Log::infoLogs an informational message

Log:warning – Logs a warning message

Log::alertLogs an alert message

Log::debugLogs a debug message

Log::emergencyLogs an emergency message

Log::noticeLogs a notice 

Log::errorLogs an error message

Log::criticalLogs a very critical message

It’s left to you and team to figure out the methods for various situations and circumstances.

Logging to Files

Open ShoutoutController.php, in the store method, we’ll log users trying to give shoutouts like so:

Now, remember to require the Log Facade at the top of the file like so:

Run the app and create a shoutout. Then check the logs in storage/logs/laravel.log file

Aha!!!..Can you see the latest log?. It captured the information and also the timestamp. You might be wondering why all those other logs are there. It’s simple. Laravel by default captures all errors and its stack trace which is totally awesome, so you as a developer don’t really have to worry about that.

You might be wondering why all those other logs are there. It’s simple. Laravel by default captures all errors and its stack trace which is totally awesome, so you as a developer don’t really have to worry about that.

You can easily check the logs and notice why there is a weird behaviour on your site or maybe why your application crashed.

Now, we are logging to a single file. If I am using a file, I prefer the logs to be daily for organisation and reference purposes instead of one fat large file.

If you want to have daily log files, just change it in the config/app.php file like so:

..and you will have access to daily logs.

Logging to an external service

There are several logging cloud services that present your log reports in a better and more readable way. Examples are bugsnag, loggly, papertrail, logentries and a host of others.

I like logentries.com. So go head over there, create an account, create a new log set and get a token.

Go to your .env and put the token like so:

where xxxx-xxxx-xxx-xx represents your token.Now, go to bootstrap/app.php and in there you can config Laravel to use any type of Logging Service Handler. Now, Monolog has done a good job by providing several Handlers for several Logging services and luckily

Now, go to bootstrap/app.php and in there you can config Laravel to use any type of Logging Service Handler. Now, Monolog has done a good job by providing several Handlers for several Logging services and luckily Logentries is part of them.

So add this in the file like so:

That’s the Monolog LogEntriesHandler and it takes in the token.

So, reload your application and make a shoutout.

Awesome!! :smile: . I gave a shoutout 3 times and those are the results on logentries.com above.

It’s that simple.

Well, One more thing.

Let’s send our logs to Slack

Virtually every team uses Slack because it’s so good. So let’s integrate it into our application.

Go to https://api.slack.com/web and issue a token.

Then go to your .env file and add like so:

Now, go to bootstrap/app.php and switch the handler there with Monolog Slack Handler like so:

The #devplayground is the name of the channel where I want the logs to go to and I am reading the slack token from the env file.

The reason I had to setLevel is because the Monolog SlackHandler only treats Critical logs by default so if you don’t set a level you might not get other types of logs below Critical.

Now, run your application and create shoutouts!.

 

Yaaay!!!. :smile: . Those are the results on my slack channel.

Conclusion

Pat yourself in the back because you have learnt how easy it is to integrate Logging of any kind into your application.

Please, if you have any questions or observations, let me know in the comments section below :smile:

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.