PHP Logging Best Practices

Craig Ferril Insights for Dev Managers Leave a Comment


With our most recent release, we’re excited to announce we have added enhanced the support in our Stackify Errors & Logs product for apps written in PHP. Our recent release includes a direct logging API for PHP, enhanced support for the Monolog and log4php logging frameworks via Stackify-specific handlers/appenders, and an agent collector model that delivers fast performance for low-overhead logging that won’t bog down your PHP page load times.

 I’ll cover these recent PHP-specific log management enhancements here, and once you’re finished, I would highly recommend reading Smarter Errors & Logs: Putting the Data to Work, which gives a great bit of in-depth guidance on how to get the absolute most out of your logging strategy. It’s definitely a must-read if you’re trying to take your logging strategy to the next level for rapid problem resolution and more proactive monitoring of your application from the inside-out.

PHP Retrace

 Why do you need a PHP log management and error monitoring solution?

Before I dive into the deep- end and demonstrate some of the PHP log management enhancements, let me first offer a primer for those who might just be looking into our Errors & Logs product for the first time.

 Advantages of using Stackify Errors & Logs:

  • Next-Level Insights – What you’re really looking for with error monitoring and log management is faster results- you want to know as soon as your app or your users are having problems, and track down root cause more efficiently. Stackify’s Errors & Logs solution product offers capabilities you won’t find anywhere else, for more quickly tracking down root cause: de-duplication of errors for reduced noise, easy debug data logging for app-state insight, automatic inclusion of web request information, and most importantly, automated connection of errors to the relevant log messages for direct correlation. It’s never been easier to connect problems to their root causes!
  • Scale – If you are logging a lot of errors, you’re going to need a product that can handle not only large volumes of throughput and data storage for your required period of time, but also one that can search that data effectively and efficiently and make it useful. Stackify gives you a solution that can scale with you, with no management on your end.
  • Performance – Whatever logging approach you use, it needs to be something that is non-blocking in multi-threaded languages, or minimally blocking in languages that don’t offer multi-threading capabilities. The team at Stackify has made sure to deliver high-performance, low-impact libraries that will enable you to log as much as you want with minimal code performance impact.

 PHP Log Management in Action

Here’s a quick overview of what you will need to do to start logging to Stackify from your PHP app, along with the results you should see shortly thereafter.

Stackify Loves Developers

 Configuring Your App to Use Stackify Logging

For our sample, we will configure our test PHP app to log via Monolog, but you could just as easily use log4php or even our direct Stackify API for PHP. The approach is similar for each, as are the results, so even if you aren’t using Monolog this should still give you a good starting point. (You can see full configuration details on our Github pages: Stackify PHP API, Stackify Monolog Handler, and Stackify log4php Appender.)

use MonologLogger;

use StackifyLogMonologHandler as StackifyHandler;

$handler = new StackifyHandler('SampleMonologApp');

$log = new Logger('stackify_channel');


 This is all that is required to configure the Stackify handler to send logs and errors to the Stackify service, in this case using the Agent Transport method which utilizes the Stackify agent to collect, batch, and then send logged messages and errors to the cloud-based Stackify servers. (There are other, agent-less options available as well, so be sure to check out the documentation when you’re ready to get started.)

 Logging Debug Data

One of the things you will want to do, if you haven’t already, is to ensure that you are logging key event data within your code. Things happen within your application all the time that can be clues about how you ended up with an error in the first place. Traditional guidance was that we only “turn up logging” after there’s a problem, but if you think about it, that’s kind of silly. Following that guidance, you have to wait for your mystery event to happen a second time before you can detect and fix it, assuming you do detect it, and by then you have (at least) two impacted users instead of one.

 So, let’s assume we’re using the best practices from the “Smarter Errors & Logs” article mentioned above, and therefore we will be logging debug objects along the way in addition to catching and logging errors. Below is an example of some code that constructs a Foo object, sets some properties, and then tries to use the object with some bad data inside of it.

$foo = new Foo();

$foo->name = “Jane Doe”;

$foo->birthday = “11/22/1980”;

$foo->gender = “Female”;

$foo->badAttribute = NULL;

$log->addDebug('Created a Foo object', ['foo' => $foo]); // log the object, not just a message

As you can see above, even though it’s a simple example, not only can you log messages as you would traditionally do with a typical logging approach, but you can also easily log debug data for later reference and Stackify will put it to good use for you. You never know when that little bit of logged data will be the critical puzzle piece to help you quickly solve a mysterious issue.

PHP log file

Here you can see in the Stackify Logging dashboard that this logged debug object is available for investigation later, including giving you the ability to search by object contents. There’s also another powerful way that Stackify can put this logged information to work for you: within the context of an error.

 Logging an Error

If we expand our sample from above to include some try/catch blocks, and add a method call that throws an exception, we get something like this:


$foo = new Foo();

$foo->name = “Jane Doe”;

$foo->birthday = “11/22/1980”;

$foo->gender = “Female”;

$foo->badAttribute = NULL;

$log->addDebug('Created a Foo object', ['foo' => $foo]); // log the object, not just a message


use_foo($foo); // tries to access $foo->badAttribute, which then throws an exception


catch(Exception $ex) {

$log->addError('Foo failure occurred', ['ex' => $ex]);


Now that we have combined debug object logging with error logging, it’s extremely easy to quickly zero- in on root cause of our exception. Here’s what it looks like in Stackify:

PHP error dashboard

From the Errors dashboard, we can quickly see that an error has occurred. From here we can jump into the details of the error.

PHP error details

The Error Detail dialog gives us a great deal more information about the error itself, including request details and a bunch of info that doesn’t fit into this screenshot (header values, post data, server variables, and so on). In this screenshot, the thing that jumps out is that we had a NULL value where there shouldn’t have been. If we click on the Logs tab we can dive into the debug data and see what led up to the error.

Log data

What Stackify does next is very powerful – instead of having to wade through log files to find the events leading up to the error, you are simply presented with the relevant log statements that occurred just before the error! Stackify connects you instantly to just those log statements that will help you solve the issue – no need to sift through thousands of log statements scattered across multiple log files and servers.

 In this way, Stackify’s solution goes beyond just error monitoring, or just log aggregation, and into new territory that combines the two in a powerful solution. Here you can see the data that we logged earlier in our debug logging message payload, and as expected, it tells us precisely what we needed to see – there is one particular property that was NULL, which led to our error in a subsequent line of code.

 See For Yourself

With Stackify’s PHP  Error and Log management enhancements, you can quickly and easily take your app monitoring and troubleshooting to the next level. With Stackify, you will go from wading through raw data to seeing instant insights into your application health, and you can do it with very little work. We make it easy to log debug data, correlate events, peer into user behavior and application context, and see many more details that will prove invaluable when trying to find root cause of application issues and become more proactive about monitoring your application’s health. So why wait, start your free trial today.


Photo credit: Wayne Wilkinson

Leave a Reply

Your email address will not be published.