Logging Application Events with Go

This part of the Bookshelf tutorial shows how an app can incorporate detailed logging to help with detecting, debugging, and monitoring potential issues. Logging application events can help you identify issues and solve problems, both during development and after your app is in production.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Go Bookshelf App.

Enter this command to go to the directory that contains the sample code:

Linux/Mac OS X

cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/getting-started/bookshelf

Windows

cd %GOPATH%\src\github.com\GoogleCloudPlatform\golang-samples\getting-started\bookshelf

Running the app on your local machine

  1. Run the app to start a local web server:

    cd app
    go run app.go auth.go template.go
    
  2. In your web browser, enter this address:

    http://localhost:8080

Press Control+C to exit the local web server.

Deploying the app to the App Engine flexible environment

  1. In the app directory, enter this command to deploy the sample:

    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.

Understanding the code

After your app is deployed and running, it's important to understand how well the app is working. Cloud Platform provides logging and monitoring tools built right into the Cloud Platform Console, so you can quickly get a powerful view into the detailed activity within your app, which can help you identify critical issues or trends.

The Bookshelf sample app uses Go's standard log package for error logs, and the Gorilla web toolkit's CombinedLoggingHandler for request logging. The following code examples demonstrate logging of HTTP requests, HTTP request errors, and application events.

Logging errors when handling HTTP requests

This code sets up logging of important errors that occur within the app. Each handler in the app is an appHandler, which returns an appError. If the handler returns a non-nil appError, a message is shown to the user, and the error details and message are logged:

type appHandler func(http.ResponseWriter, *http.Request) *appError

type appError struct {
	Error   error
	Message string
	Code    int
}

func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if e := fn(w, r); e != nil { // e is *appError, not os.Error.
		log.Printf("Handler error: status code: %d, message: %s, underlying err: %#v",
			e.Code, e.Message, e.Error)

		http.Error(w, e.Message, e.Code)
	}
}

Logging HTTP requests

Each HTTP request is logged using the Gorilla web toolkit's CombinedLoggingHandler. In the following code, r is the Gorilla mux.Router used to route all of the application's request handlers. CombinedLoggingHandler wraps each request to log to os.Stderr before the request is served.

// Delegate all of the HTTP routing and serving to the gorilla/mux router.
// Log all requests using the standard Apache format.
http.Handle("/", handlers.CombinedLoggingHandler(os.Stderr, r))

Logging application events

The app does not have any general log statements or debug log statements, but you can use the standard log package to add them:

log.Printf("Something happened: %v", x)

Understanding the logging configuration

Running in the App Engine flexible environment

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

Viewing logs

After the sample app is deployed and running, logging data will begin to be collected. Analyzing this logging data can be done with the log monitoring tools built right into the Cloud Platform Console. The following image shows logged events in the Cloud Platform Console.

Logging in the console

If you want even- more-detailed analysis, you can stream or import the app's logs into BigQuery or export them to a Google Cloud Storage Bucket. You can do both right from within the Cloud Platform Console.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...