Logging app 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 read the setup instructions, 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 the following command to start a local web server. Replace [YOUR_PROJECT_ID] with your GCP project ID:

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

Deploying the app to the App Engine flexible environment

  1. Deploy the app.

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

    https://[YOUR_PROJECT_ID].appspot-preview.com
    

When you update your app, you can deploy the updated version using the same command you used when you first deployed the app. 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. All of these app versions and VM instances are billable resources.

You can reduce costs by deleting the non-default versions of your app. 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 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 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 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: 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);

You can set and change logging levels in properties files and 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 Stackdriver Logging and available to view, search, and export 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 GCP 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.
  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 GCP 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 GCP 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

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

Send feedback about...