Logging app events with PHP

This part of the PHP Bookshelf app tutorial shows how an app can incorporate detailed logging to help with detecting, debugging, and monitoring potential issues.

This page is part of a multipage tutorial. To start from the beginning and read the setup instructions, go to PHP Bookshelf app.

Configuring settings

Copy your settings.yml file from the Authenticating users part of this tutorial to the getting-started-php/5-logging directory.

Installing dependencies

In the 5-logging directory, enter this command:

composer install

Running the app on your local machine

  1. Start a local web server:

    php -S localhost:8000 -t web
  2. In your browser, enter this address:


Deploying the app to the App Engine flexible environment

  1. In your terminal window, deploy the sample app:

    gcloud app deploy
  2. In your browser, enter this address. Replace [YOUR_PROJECT_ID] with your Google Cloud Platform (GCP) project ID:


If you update your app, you can deploy the updated version by entering the same command you used when you first deployed the app. The new deployment creates a version of your app and promotes it to the default version.

The older versions of your app remain, as do their associated virtual machine (VM) instances. Be aware that all of these app versions and VM instances are billable resources.

You can reduce costs by deleting the non-default versions of your app.

When you finish this tutorial, you can avoid continued billing by deleting the resources you created. See Cleaning up for more detail.

Viewing logs

As the Bookshelf app runs, it writes logging data that is collected and made available in the Google Cloud Platform Console. You can use Stackdriver Logging in the GCP Console to analyze the logs directly. If you want more detailed analysis, you can use the GCP Console to stream or import the app's logs into BigQuery or export them to a Cloud Storage bucket.

Logging in the GCP Console

App structure

The following diagram shows how the Bookshelf app handles logging when deployed to the App Engine flexible environment.

Logging sample structure - flexible environment

Understanding the code

The Bookshelf sample app uses the Monolog library for logging app events. If the app is running on App Engine, the app's initialization code configures Monolog to use AppEngineFlexHandler. If the app is running locally, it configures Monolog to use ErrorLogHandler:

$app->register(new Silex\Provider\MonologServiceProvider());
if (GCECredentials::onAppEngineFlexible()) {
    $app['monolog.handler'] = new AppEngineFlexHandler();
} else {
    $app['monolog.handler'] = new Monolog\Handler\ErrorLogHandler();

You can log events from anywhere in your app by using the monolog service, and those events are visible in your project:

$app['monolog']->notice('Deleted Book: ' . $book['id']);

Additional debugging

If the logs don't provide enough visibility into your project, you can use SSH to connect directly to your instances.

  1. In the GCP Console, go to the App Engine Instances page.

    Go to the Instances page

  2. Click SSH next to one of the running instances.

  3. After you have shell access to your VM, view and debug your running app by using the following command. Because the flexible environment uses containers, Docker commands are required to inspect your app's code.

    $ sudo docker ps

    This command lists all the running Docker images on your instance:

    CONTAINER ID        IMAGE                                                                           COMMAND                  CREATED             STATUS              PORTS                                  NAMES
    7f995fe47ec9        gcr.io/google-appengine/mvm-agent                                               "/usr/local/bin/gunic"   14 minutes ago      Up 14 minutes                                              reverent_carson
    3624910a55b4        gcr.io/google-appengine/nginx-proxy                                             "/var/lib/nginx/bin/s"   14 minutes ago      Up 14 minutes>8080/tcp, 8090/tcp       happy_liskov
    5c262bf5a7dd        appengine.gcr.io/234505678652123512/<your-project-id>.default.20160208t132609   "/entrypoint.sh /usr/"   14 minutes ago      Up 14 minutes       8080/tcp                               gaeapp
    e40568bcbf82        gcr.io/google-appengine/memcache-proxy                                          "/home/memcache/memca"   14 minutes ago      Up 14 minutes       11211/tcp                              memcache
    60527ecb8aa3        gcr.io/google-appengine/fluentd-logger                                          "/opt/google-fluentd/"   15 minutes ago      Up 15 minutes

    The container with the name gaeapp represents your app's code.

  4. To see your running app, run the following:

    $ sudo docker exec -t -i gaeapp /bin/bash

What's next

Was this page helpful? Let us know how we did:

Send feedback about...