Logging Application Events with PHP

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

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, 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 web browser, enter this address.

    http://localhost:8000

Deploying the app to the App Engine flexible environment

  1. Deploy the sample app:

    gcloud app deploy
    
  2. In your web browser, enter this address. Replace [YOUR_PROJECT_ID] with your project ID:

    https://[YOUR_PROJECT_ID].appspot.com
    

    If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated 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.

    To delete an app version:

    1. In the Cloud Platform Console, go to the App Engine Versions page.

      Go to the Versions page

    2. Click the checkbox next to the non-default app version you want to delete.
    3. Click the Delete button at the top of the page to delete the app version.

    For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.

    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 the log monitoring tools 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 Google Cloud Storage Bucket.

    Logging in the console

    Application structure

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

    Logging sample structure - flexible environment

    Understanding the code

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

    $app->register(new Silex\Provider\MonologServiceProvider());
    if (isset($_SERVER['GAE_VM']) && $_SERVER['GAE_VM'] === 'true') {
        $app['monolog.handler'] = new AppEngineFlexHandler();
    } else {
        $app['monolog.handler'] = new Monolog\Handler\ErrorLogHandler();
    }

    Now you can log events from anywhere using the monolog service and they will be visible in your project:

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

    Additional debugging

    There are times when the logs alone do not provide enough visibility. To accomplish this, you can SSH directly into your instance by navigating to App Engine > Instances and clicking SSH next to one of the running instances.

    Once you have shell access to your VM, you can view and debug your running application. Because the flexible environment uses containers, Docker commands must be used to inspect the application code.

    $ sudo docker ps
    

    This command will list 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       0.0.0.0:8080->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 application code. You can now execute commands and see your running application:

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

What's next

Send feedback about...