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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* |-------------------------------------------------------------------------- | Logging Configuration |-------------------------------------------------------------------------- | | Here you may configure the log settings for your application. Out of | the box, Laravel uses the Monolog PHP logging library. This gives | you a variety of powerful log handlers / formatters to utilize. | | Available Settings: "single", "daily", "syslog", "errorlog" | */ 'log' => env('APP_LOG', 'single'), |
By default, it is set to a single file.
We have different levels of logging:
Log::info – Logs an informational message
Log:warning – Logs a warning message
Log::alert – Logs an alert message
Log::debug – Logs a debug message
Log::emergency – Logs an emergency message
Log::notice – Logs a notice
Log::error – Logs an error message
Log::critical – Logs 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { $this->validate($request, [ 'shoutout' => 'required' ]); $request->user()->shouts()->create([ 'text' => $request->shoutout, ]); Log::info('User created a shoutout successfully', ['email' => $request->user()->email]); return redirect('shouts'); } |
Now, remember to require the Log
Facade at the top of the file like so:
1 |
use Log; |
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:
1 |
'log' => env('APP_LOG', 'daily'), |
..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:
1 |
LOGENTRY_TOKEN=xxxxx-xxxxxx-xxxxxx-xxx |
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:
1 2 3 |
$app->configureMonologUsing( function($monolog) { $monolog->pushHandler(new MonologHandlerLogEntriesHandler(env('LOGENTRY_TOKEN'))); }); |
That’s the Monolog LogEntriesHandler and it takes in the token.
So, reload your application and make a shoutout.
Awesome!! . 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:
1 |
SLACK_API_TOKEN=xxx-xxx-xxxxxx-xxxx |
Now, go to bootstrap/app.php
and switch the handler there with Monolog Slack Handler like so:
1 2 3 4 5 |
$app->configureMonologUsing( function($monolog) { $slackHandler = new MonologHandlerSlackHandler(env('SLACK_API_TOKEN'), '#devplayground'); $slackHandler->setlevel(MonologLogger::INFO); $monolog->pushHandler( $slackHandler); }); |
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!!!. . 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

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016