Logging Application Events with Java

This part of the Bookshelf tutorial for Java 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 Java Bookshelf App.

Running the app on your local machine

To run the app locally:

  1. In the getting-started-java/bookshelf/5-logging directory, enter this command to start a local web server:

    mvn -Plocal clean jetty:run-exploded -DprojectID=[YOUR-PROJECT-ID]
  2. In your web browser, navigate to http://localhost:8080

Deploying the app to the App Engine flexible environment

  1. Enter this command to deploy the app:

    mvn appengine:deploy -DprojectID=YOUR-PROJECT-ID
    
  2. In your web browser, enter this address. Replace [YOUR_PROJECT_ID] with your project ID:

    https://[YOUR_PROJECT_ID].appspot-preview.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 GCP 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 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 - App Engine flexible environment

The following diagram shows how the app handles logging when deployed to Google Compute Engine, which is covered in the last step of the tutorial.

Logging sample structure - Compute Engine

Understanding the code

There are many Java logging packages that are compatible with the App Engine flexible environment. This sample uses java.util.logging, as it's the most well understood. Each class needs a Logger instantiated as follows:

private final Logger logger = Logger.getLogger(ReadBookServlet.class.getName());

The log method, takes a Level and a string. Possible values for the level are of FINEST, FINER, FINE, CONFIG, INFO, WARNING, and SERVERE. It is also possible to log using one of the short-cut methods: config, info, warning, or severe.

logger.log(Level.INFO, "Read book with id {0}", id);

Logging levels, which are often set from properties files, can also be set and changed in app.yaml:

env_variables:    # Logging options
  JAVA_OPTS: >-
    -D.level=INFO

Understanding the logging configuration

When the sample app runs in the App Engine flexible environment, anything logged to stderr and stdout is automatically collected by Cloud Logging and available for viewing, searching, and exporting in the logs viewer in the GCP Console.

In this sample, all logs are written to stderr/stdout.

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Delete the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete non-default versions of your app

If you don't want to delete your project, 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.

Delete your Cloud Storage bucket

To delete a Cloud Storage bucket:

  1. In the Cloud Platform Console, go to the Cloud Storage browser.

    Go to the Cloud Storage browser

  2. Click the checkbox next to the bucket you want to delete.
  3. Click the Delete button at the top of the page to delete the bucket.

What's next

Learn how to run the Java Bookshelf sample on Compute Engine.

Learn about using Java in the App Engine standard environment.

Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Send feedback about...