PHP Application Logging

Table of Contents

Heroku treats logs as streams and thus will automatically aggregate all app, system and API log messages that are sent to stdout or stderr to provide a single channel of logging information for your application.

Most PHP frameworks can be configured to write appropriate log output, and this article provides examples for some common libraries and frameworks.

Logging using PHP or libraries

Logging from plain PHP

The normal PHP error log is automatically sent to stderr, and any message logged using the error_log function will thus be available in heroku logs:

error_log("hello, this is a test!");

The alternative approach, is writing to the php://stderr stream directly:

file_put_contents("php://stderr", "hello, this is a test!\n");

Make sure you always terminate a message with a trailing newline character like in the example above when manually writing to stderr!

Logging using Monolog

If you’re using the excellent Monolog library for PHP (and you should!), you can simply use a StreamHandler to write to php://stderr:

Symfony

Adding logging for legacy applications

Some applications cannot be easily modified to write logs to stderr or stdout. In these cases, if the application allows configuration of a static log file name, it’s possible to instruct Heroku’s PHP support to tail such a log file into the Logplex stream using the -l option of the boot scripts for PHP applications.

For example, if your legacy application writes to a file named log/error.log your Procfile entry could look like this:

web: vendor/bin/heroku-php-apache2 -l log/error.log

You can specify the -l option multiple times to tail multiple files, and of course combine it with other options and the document root argument:

For obvious reasons, if your legacy application creates dynamic log file names, e.g. including the current date, you will need to patch or configure it to use a fixed, static log file name instead for this feature to work.