Pushing Laravel logs to Loggly


Laravel uses the Monolog logging library for logging, however, it’s saving all logs to a local directory by default. Not a very useful thing in a production environment.

That’s where Loggly comes into play. Loggly acts as a central (cloud)platform that can receive logs from a multitude of platforms such as php frameworks (Laravel being the one), operating systems, their own Loggly API, and a lot of other frameworks and services. In this short tutorial I’ll show you how to implement Loggly into your multi-environment Laravel project.

1) Update composer

First we need to update composer so we’re 100% that we’re using the latest Monolog version, because older versions don’t support Loggly.

composer self-update
composer update

2) Setup Loggly account

Go to https://www.loggly.com and create a (free) account.

3) Add Loggly credentials to your Laravel environment

Now the nice thing about Loggly is that it’s pretty easy to configure. Go to https://www.loggly.com/tokens and setup a token for your project. Do this to keep things seperated. Got a big project? Then you might want to use source groups as well.
Now create a new file in your Laravel folder: /app/config/services.php and add this:

return array(
	// credentials for loggly.com
	'loggly' => array(
		'key'	=> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx',
		'tag'	=> 'ProjectName_' .strtolower(App::environment()),

Now if you’re using Laravel’s environment configuration you should do this:

return array(
	// credentials for loggly.com
	'loggly' => array(
		'key'	=> getenv('services.loggly.key'),
		'tag'	=> 'ProjectName_' .strtolower(App::environment()),

And then store it in a similar way in your .env.local.php file, or on Laravel Forge.

4) Install the logging code

Now this is where the magic happens. Open up /app/start/global.php and find the “Application Error Logger” section. Replace it with this code:

| Application Error Logger
| Here we will configure the error logger setup for the application which
| is built on top of the wonderful Monolog library. By default we will
| build a rotating log file setup which creates a new file each day.
$logFile = 'log-'.php_sapi_name().'.txt';
 * Setup Loggly Handler
$handler = new \Monolog\Handler\LogglyHandler(Config::get('services.loggly.key'),\Monolog\Logger::DEBUG);
$logger = Log::getMonolog();

This way, every environment will have it’s own tag and you can easily see in the Loggly dashboard when something happens on your dev/staging environment, or if it’s on live. I also strongly advise to setup alerts within Loggly so you get notified once you got a couple of 500 errors coming around.
Now the nice thing about Loggly is that you can go back in time and easily see in the stats how many errors occured in the past 24 hours, good luck doing that with log files! This is especially useful for keeping a close eye on cronjobs and incoming API calls.

5) Testing

Throw some 404 errors by opening up a page in your project and appending some random characters. Wait a couple of minutes and there we go. You should see something like this:
Screen Shot 2014-10-02 at 11.05.36

Also worth noting is that all other Laravel logging functions will also push to Loggly.

More info about Laravel’s logging features: http://laravel.com/docs/4.2/errors#logging
More info about Loggly: https://www.loggly.com

Laravel 4 released

Great news! My favorite PHP framework Laravel finally released version 4 after 5 public beta’s and months of waiting. Why am I so happy? Well.. it makes my job as a developer a lot simpler compared to my Zend Framework days.

Here’s why you must try Laravel

  • – Smart framework: write less code (faster + easier)
  • – Works with composer, the PHP plugin tool
  • – Filters, events, rest, built-in auth, … it has it all out of the box
  • – Elequent ORM: the smartest and easiest PHP ORM today!
  • – Unit testing for all components

So what’s new in Laravel 4?

  • – L4 now runs on PHP >= 5.3.7
  • – PSR-0 code standards are used
  • – Composer (packagist.org) instead of Laravel bundles
  • – Lot’s of small but convenient little tweaks and options
  • – Mail is now built in
  • – L4 is built with Unit Testing in mind
  • – Great community!
  • – The command line tool (artisan) is now more powerful


Here’s what changed in L4:

Laravel 4 introduction

Download Laravel @ http://laravel.com/, and make sure to follow them on Twitter: https://twitter.com/laravelphp