Sending Laravel Logs to Loggly

Loggly is a cloud-based log management tool that allows real-time scanning of your logs, making it easier to search through data. This is very useful when it comes to get the best out of your application.

As stated in the Loggly page, the functionalities are endless:

  • Monitoring operations
  • Identifying root causes of operational issues
  • Tuning performance
  • Tracking down security issues
  • Correlating technology performance with business performance
  • And more

So, as you can already tell, this is a very powerful way to fix issues on your application. But the great thing about Loggly is the aggregation, indexation and parsing of data that makes it more human-readable.

You can configure Loggly to tons of log sources, but in this tutorial I will show how to integrate the PHP Monolog libraly used in Laravel.

I'm assuming you have your Laravel project up and running. Let's begin:

First, signup to Loggly - yes, it has a free trial with full features!
Login and go to Source Setup > Customer Tokens and add a new token, this is be needed later on, so store it somewhere safe!

Open your command line and cd to your Laravel project directory. Once you're there, install the latest version of Monolog, using composer:
composer require monolog/monolog

You'll need PHP Curl, so if you haven't, install it:
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Now, open config/services.php and add:
'loggly' => [ 'key' => env('LOGGLY_KEY'), 'tag' => 'ProjectName_' .strtolower(App::environment()), ],

As you can see, we're fetching the LOGGLY_KEY from the .env. The tag option is used by Loggly to make it easier for you to ruffle through logs. In this example I'm setting it to the Project Name and the current environment. Using this you can distinguish local/production logs, but you can set it to what suits you best.

Store the LOGGLY_KEY - the one that you create at the first step - in your .env file, like so:
LOGGLY_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

We need to let Laravel Monolog know that the logs should be sent to Loogly, to achieve this go to bootstrap/app.php and these lines before returning the $app variable:
$app->configureMonologUsing(function ($monolog) { $handler = new Monolog\Handler\LogglyHandler(config('services.loggly.key'), Monolog\Logger::DEBUG ); $handler->setTag(config('services.loggly.tag')); $monolog->pushHandler($handler); $monolog->addWarning('Testing logs to loggly'); });

The last line will trigger a warning, to test the setup. If everything went well, once you open your app root, the warning will show up in the Loggly Dashboard. Give it some minutes and then check it here.

Now, you can have some fun and log some random stuff, using one of the MonoLog logger levels. Also, all thrown exceptions are sent to Loggly by default.

As you look through the dashboard, you'll notice that the logs are tagged, parsed and really easy to read. And that's it, you're ready to go!

I hope you find all of this useful!

Many thanks to clearBucket Labs:
http://www.clearbucketlabs.com/2014/02/logging-with-loggly-in-laravel/

comments powered by Disqus