Logging app events with Ruby

This part of the Bookshelf app tutorial shows how an app can incorporate detailed logging to help with detecting, debugging, and monitoring potential issues. Logging app 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 multipage tutorial. To start from the beginning and read the setup instructions, go to Ruby Bookshelf app.

Installing dependencies

Go to the getting-started-ruby/5-logging directory, and enter the following command:

bundle install

Configuring settings

  1. Copy the example settings file.

    cp config/settings.example.yml config/settings.yml
  2. Edit the settings.yml file. the same way you did in the Authenticating users section.

    For example, suppose your web app client ID is XYZCLIENTID, your client secret is XYZCLIENTSECRET, your project name is my-project, and your Cloud Storage bucket name is my-bucket. Then the default section of your settings.yml file would look like this:

    default: &default
      project_id: my-project
      gcs_bucket: my-bucket
        client_id: XYZCLIENTID
        client_secret: XYZCLIENTSECRET
  3. Copy the example database file.

    cp config/database.example.yml config/database.yml
  4. Configure the sample app to use the same database that you set up during the Using structured data portion of this tutorial.

    Cloud SQL

    • Edit database.yml. Uncomment the lines in the Cloud SQL portion of the file.

       mysql_settings: &mysql_settings
         adapter: mysql2
         encoding: utf8
         pool: 5
         timeout: 5000
         username: [MYSQL_USER]
         password: [MYSQL_PASS]
         database: [MYSQL_DATABASE]
         socket: /cloudsql/[YOUR_INSTANCE_CONNECTION_NAME]
      • Replace [MYSQL_USER] and [MYSQL_PASS] with your Cloud SQL instance username and password that you created previously.

      • Replace [MYSQL_DATABASE] with the name of the database that you created previously.

      • Replace [YOUR_INSTANCE_CONNECTION_NAME] with the Instance Connection Name of your Cloud SQL instance.

    • Run migrations.

      bundle exec rake db:migrate


    • Edit database.yml. Uncomment the lines in the PostgreSQL portion of the file. Replace the your-postgresql-* placeholders with the values for your PostgreSQL instance and database. For example, suppose your IPv4 address is, your username is postgres, and your password is pword123. Also suppose your database name of bookshelf. Then the PostgreSQL portion of your database.yml file would look like this:

      # PostgreSQL Sample Database Configuration
      # ----------------------------------------
        adapter: postgresql
        encoding: unicode
        pool: 5
        username: postgres
        password: pword123
        database: bookshelf
    • Create the required database and tables.

      bundle exec rake db:create
      bundle exec rake db:migrate


    • Edit database.yml. Uncomment the one line in the Datastore portion of the file. Replace your-project-id with your Google Cloud project ID. For example, suppose your project ID is my-project: Then the Datastore portion of your database.yml file would look like this:

      # Google Cloud Datastore Sample Database Configuration
      # ----------------------------------------------------
      dataset_id: my-project
    • Run a rake task to copy the sample project files for Datastore.

      bundle exec rake backend:datastore

Running the app on your local machine

  1. Start a local web server.

    bundle exec rails server
  2. In your web browser, enter the following address:


To exit the local web server, press Control+C .

Deploying the app to the App Engine flexible environment

  1. Compile the JavaScript assets for production.

    RAILS_ENV=production bundle exec rake assets:precompile
  2. Deploy the sample app.

    gcloud app deploy
  3. In your web browser, enter the following address.


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

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

Viewing logs

As the bookshelf app runs, it writes logging data that is collected and made available in the Google Cloud Console. You can use the log monitoring tools in the Cloud Console to analyze the logs directly. If you want more detailed analysis, you can use the Cloud Console to stream or import the app's logs into BigQuery or export them to a Cloud Storage bucket.

Logging in the Cloud Console

Understanding the code

Rails includes a powerful and configurable logging system. The Bookshelf app configures Rails to place logs in the directory where the App Engine flexible environment expects to find them.

if Dir.exist? "/var/log/app_engine/custom_logs"
  config.logger = ActiveSupport::TaggedLogging.new Logger.new("/var/log/app_engine/custom_logs/application.log")

In addition to the default messages provided by Rails, you can log anything you want by using the Rails logger object.

logger.debug "Debug message."
logger.info "Informational."
logger.fatal "Fatal error."
Var denne siden nyttig? Si fra hva du synes:

Send tilbakemelding om ...