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:
-
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]
- In your web browser, go to http://localhost:8080.
Deploying the app to the App Engine flexible environment
Deploy the app.
mvn appengine:deploy -DprojectID=YOUR-PROJECT-ID
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.
App structure
The following diagram shows how the app handles logging when deployed to the 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.
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:
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
.
You can set and change logging levels in properties files and in app.yaml
:
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:
- In the GCP Console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete.
- 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:
- In the GCP Console, go to the App Engine Versions page.
- Click the checkbox next to the non-default app version you want to delete.
- 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:
- In the GCP Console, go to the Cloud Storage browser.
- Click the checkbox next to the bucket you want to delete.
- 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.