So what is Laravel?
Laravel is a relatively new MVC web framework for PHP that was released in 2012. It’s also the most popular PHP project on Github at the time of this post. Like many web frameworks, Laravel boasts an easy learning-curve to get an app up and running. This was certainly the case when I used it.
While this post is focusing on logging in Laravel, its applicable to other php web frameworks, like Symfony2 and Aura to name a few. It’s worth noting that Laravel actually has several Symfony dependencies but the two can be used independently to build a web app.
When it comes to logging, each of these web frameworks have followed the PSR-3 logging standard interface.
Thanks to this, the frameworks actually come with Monolog out of the box, making it very easy to switch between them with minimal effect to your logging configurations, should you need to migrate your app.
Setting up with Monolog is just as simple.
In the case of Laravel, we can find the default logging configuration at this location in our projects root directory:
The default config writes to a file in the projects log folder located below in the project directory:
Thanks to one of our users and contributors, Rob Kaufmann, Monolog ships with built-in Logentries support, so we need only add 2 lines to
to start sending logs to Logentries.
This snippet below shows what we need to add to configure our Laravel config to point Monolog towards Logentries:
$logentriesHandler = new Monolog\Handler\LogEntriesHandler('LOG_TOKEN'); Log::getMonolog()->pushHandler($logentriesHandler);
The LogEntriesHandler has a single required parameter which is the UUID token for a log on Logentries, this allows the library to send logs to a specified log in a Logentries account.
To obtain a token, simply follow these steps:
1. Create A Log Destination
First, we need a Logentries Account, which you can get for free. Once we have a Logentries account, we’ll need a destination log in order to store the entries sent from within our Laravel app. In the Logentries UI, create a new log by clicking the Create Log button. If you already have a Host set up, you can add the log to the existing Host by click on the Host in the Web UI and then “Add new log” as shown below:
2. Click Manual Configuration button from the list of options.
3. Give the new log a name of your choice, and then select “Token TCP”:
4. Click the Register New Log button.
A token will be displayed in green. Copy this token and insert it into your global.php resulting in a config snippet similar to this:
$logentriesHandler = new Monolog\Handler\LogEntriesHandler('COPIED_TOKEN'); Log::getMonolog()->pushHandler($logentriesHandler);
Now, any log statements written by your Laravel app will now be forwarded to Logentries, it doesn’t get any easier than that.
Ready to start getting insights from your applications? Sign up for a Logentries free trial today.