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 multi-page tutorial. To start from the beginning and read the setup instructions, go to Ruby Bookshelf app.
Go to the
getting-started-ruby/5-logging directory, and enter the following
Copy the example settings file.
cp config/settings.example.yml config/settings.yml
settings.ymlfile. 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.ymlfile would look like this:
default: &default project_id: my-project gcs_bucket: my-bucket oauth2: client_id: XYZCLIENTID client_secret: XYZCLIENTSECRET
Copy the example
cp config/database.example.yml config/database.yml
Configure the sample app to use the same database that you set up during the Using structured data portion of this tutorial.
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]
[MYSQL_PASS]with your Cloud SQL instance username and password that you created previously.
[MYSQL_DATABASE]with the name of the database that you created previously.
Instance Connection Nameof your Cloud SQL instance.
bundle exec rake db:migrate
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
184.108.40.206, your username is
postgres, and your password is
pword123. Also suppose your database name of
bookshelf. Then the PostgreSQL portion of your
database.ymlfile would look like this:
# PostgreSQL Sample Database Configuration # ---------------------------------------- adapter: postgresql encoding: unicode pool: 5 username: postgres password: pword123 host: 220.127.116.11 database: bookshelf
Create the required database and tables.
bundle exec rake db:create bundle exec rake db:migrate
database.yml. Uncomment the one line in the Cloud Datastore portion of the file. Replace
your-project-idwith your Google Cloud Platform project ID. For example, suppose your project ID is
my-project: Then the Cloud Datastore portion of your
database.ymlfile 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 Cloud Datastore.
bundle exec rake backend:datastore
Running the app on your local machine
Start a local web server.
bundle exec rails server
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
RAILS_ENV=production bundle exec rake assets:precompile
Deploy the sample app.
gcloud app deploy
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:
- In the GCP Console, go to the Versions page for App Engine.
- Select the checkbox for the non-default app version you want to delete.
- Click Delete deleteto delete the app version.
For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.
The following diagram shows how the app handles logging when deployed to the App Engine flexible environment.
As the bookshelf app runs, it writes logging data that is collected and made available in the Google Cloud Platform 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.
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") end
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 "Infomational." logger.fatal "Fatal error."