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-standard/5-logging directory, enter the following command to start a local web server:

    mvn -Plocal clean appengine:devserver -Dbookshelf.bucket=[YOUR-BUCKET]
    Replace [YOUR-BUCKET] with your bucket name.
  2. In your web browser, go to http://localhost:8080.

Deploying the app to the App Engine standard environment

To deploy to the App Engine standard environment:

  1. To create a Cloud Datastore index that is uploaded to the Bookshelf app, create at least one book, and then click My Books. Note, building the app using the clean command deletes that local index so do this on the build of the app that you are going to deploy.
  2. In the getting-started-java/bookshelf-standard/5-logging directory, enter this command to deploy the app:
    mvn appengine:update -Dappengine.appId=[YOUR-PROJECT-ID] -Dappengine.version=[YOUR-VERSION] -Dbookshelf.bucket=[YOUR-BUCKET].appspot.com
    Replace [YOUR-PROJECT-ID] with your project ID and [YOUR-VERSION] with your version, for example, 1, or 2, or some other string value you want to use.
  3. In your web browser, enter the following address:
    https://[YOUR-PROJECT-ID].appspot.com
    Replace [YOUR-PROJECT-ID] with your project ID.

After you update your app, you can redeploy the updated version by entering the same command as when you first deployed the app as well as specifying the same project ID and version. This overwrites the currently deployed app. If you specify a different version string in the updated command line, the new deployment creates a new version of your app and promotes it to be the currently serving version.

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

To delete an app version:

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

    Go to the Versions page

  2. Select the checkbox for the non-default app version you want to delete.
  3. Click Delete 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 GCP Console

App structure

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

Logging sample structure - App Engine standard environment

Understanding the code

There are many Java logging packages that are compatible with the App Engine standard 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 value and a string. (See the Java Level for more information.) Possible values for the level are:

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 logging.properties.

# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).

# Set the default logging level for all loggers to INFO
.level = INFO
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

Understanding the logging configuration

When the sample app runs in the App Engine standard environment, app logs are 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 Versions page for App Engine.

    Go to the Versions page

  2. Select the checkbox for the non-default app version you want to delete.
  3. Click Delete 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 page.

    Go to the Cloud Storage Browser page

  2. Click the checkbox for the bucket you want to delete.
  3. Click Delete to delete the bucket.

What's next

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

Send feedback about...