You can write logs to Cloud Logging from Ruby applications by
using the Cloud Logging library for Ruby, or by using the
Cloud Logging Cloud client library for Ruby directly.

The Cloud Logging agent does not have to be installed to use the
Cloud Logging library for Ruby on a Compute Engine VM instance.

## Before you begin

1. Install [Ruby 2.2+](https://www.ruby-lang.org/en/downloads/) or greater.


## Installing the library

We provide a Cloud Logging library for Ruby, which provides an easy-to-use logger
implementation for Rack-based Ruby web frameworks:

1. Add the Google Cloud Observability gem to your Gemfile:

       gem "stackdriver"

2. Use [Bundler](http://bundler.io/) to install the gem:

       bundle install

### Enabling the library

If you're using Ruby on Rails, Bundler automatically loads the library into
your application when it starts, which sets the default `Rails.logger` to an
instance of Cloud Logging logger.

Other Rack-based applications can use the Rack Middleware to replace the Rack
logger with the Cloud Logging logger if your web framework supports Rack
logger:

    require "google/cloud/logging"

    use Google::Cloud::Logging::Middleware

### Configuring the library

You can customize the behavior of the Cloud Logging library for Ruby. See [the library's configuration](https://github.com/googleapis/google-cloud-ruby/blob/master/stackdriver/INSTRUMENTATION_CONFIGURATION.md)
for a list of possible configuration options.

## Using the Cloud Logging Logger

Once the Cloud Logging library for Ruby is enabled, you can use the logger in
your application:

    logger.info "Hello World!"
    logger.error "Oh No!"

<br />

For more information on installation, see the
[documentation](https://github.com/googleapis/google-cloud-ruby/tree/master/google-cloud-logging) for the
Cloud Logging library for Ruby. You can also report issues using the
[issue tracker](https://github.com/googleapis/google-cloud-ruby/issues).

## Write logs with the Cloud Logging client library

For information on using the Cloud Logging client library for
Ruby directly, see [Cloud Logging Client Libraries](https://docs.cloud.google.com/logging/docs/reference/libraries).

## Run on Google Cloud

For an application to write logs by using the Cloud Logging library for Ruby, the
service account for the underlying resource must have the
[Logs Writer (`roles/logging.logWriter`)](https://docs.cloud.google.com/logging/docs/access-control#permissions_and_roles) [IAM](https://docs.cloud.google.com/iam/docs/understanding-roles) role.
Most Google Cloud environments automatically configure the default
service account to have this role.

### App Engine

Cloud Logging is automatically enabled for App Engine, and your app's
[default service account](https://docs.cloud.google.com/appengine/docs/standard/ruby/service-account) has
the IAM permissions by default to write log entries.

For more information, see [Writing and viewing
logs](https://docs.cloud.google.com/appengine/docs/standard/ruby/writing-application-logs).

### Google Kubernetes Engine (GKE)

[GKE](https://docs.cloud.google.com/kubernetes-engine/docs) automatically grants the default service account
the [Logs Writer (`roles/logging.logWriter`)](https://docs.cloud.google.com/logging/docs/access-control#permissions_and_roles) IAM role.
If you use
[Workload Identity Federation for GKE](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/workload-identity)
with this default service account to let workloads access specific
Google Cloud APIs, then no additional configuration is required.
However, if you use Workload Identity Federation for GKE with a
custom IAM service account, then ensure that the custom service
account has the role of Logs Writer (`roles/logging.logWriter`).

If needed, you can also use the following command to add the `logging.write`
access scope when creating the cluster:

    gcloud container clusters create example-cluster-name \
        --scopes https://www.googleapis.com/auth/logging.write

### Compute Engine

> [!NOTE]
> **Note:** To use the Cloud Logging library for Ruby on a **Compute Engine** VM instance, you do not need to install the Cloud Logging agent.

When using [Compute Engine](https://docs.cloud.google.com/compute/docs) VM instances, add the `cloud-platform`
access scope to each instance. When creating a new instance through the
Google Cloud console, you can do this in the **Identity and API access** section
of the **Create Instance** panel. Use the Compute Engine default service
account or another service account of your choice, and select
**Allow full access to all Cloud APIs** in the **Identity and API access**
section. Whichever service
account you select, ensure that it has been granted the
[Logs Writer role](https://docs.cloud.google.com/logging/docs/access-control#permissions_and_roles) in the **IAM \& Admin** section of the
Google Cloud console.

## Run locally and elsewhere

To use the Cloud Logging library for Ruby outside of Google Cloud,
including running the library on your own workstation, on your data center's
computers, or on the VM instances of another cloud provider, you must
set up Application Default Credentials (ADC) in your local environment to
authenticate to the Cloud Logging library for Ruby.

For more information, see
[Set up ADC for on-premises or another cloud provider](https://docs.cloud.google.com/docs/authentication/set-up-adc-on-premises).
**Using Ruby on Rails configuration interface:**

<br />

Provide the parameters through the Ruby on Rails configuration interface if
you are using Ruby on Rails framework:

    # Add this to config/environments/*.rb
    Rails.application.configure do |config|
      # Stackdriver Logging specific parameters
      config.google_cloud.logging.project_id = "YOUR-PROJECT-ID"
      config.google_cloud.logging.keyfile    = "/path/to/service-account.json"
    end

You can also set shared configuration for all Google Cloud Observability gems using:

    # Add this to config/environments/*.rb
    Rails.application.configure do |config|
      # Stackdriver Shared parameters
      config.google_cloud.project_id = "YOUR-PROJECT-ID"
      config.google_cloud.keyfile    = "/path/to/service-account.json"
    end

Logging is enabled by default when Rails is running in production mode. To
enable Logging in development mode, add the following add:

    # Add this to config/environments/development.rb
    Rails.application.configure do |config|
      config.google_cloud.use_logging = true
    end

**Using instrumentation configuration interface:**

Provide the parameters through the configuration interface when used in
other Rack-based applications:

    require "google/cloud/logging"

    Google::Cloud.configure do |config|
      # Stackdriver Logging specific parameters
      config.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-build-v1/latest/Google-Cloud-Build-V1-BuildOptions.html.project_id = "YOUR-PROJECT-ID"
      config.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-build-v1/latest/Google-Cloud-Build-V1-BuildOptions.html.keyfile    = "/path/to/service-account.json"
    end

You can also set shared configuration for all Google Cloud Observability gems using:

    require "stackdriver"

    Google::Cloud.configure do |config|
      # Stackdriver Shared parameters
      config.project_id = "YOUR-PROJECT-ID"
      config.keyfile    = "/path/to/service-account.json"
    end

## View the logs

In the Google Cloud console, go to the

**Logs Explorer** page:

[Go to **Logs Explorer**](https://console.cloud.google.com/logs/query)

<br />

If you use the search bar to find this page, then select the result whose subheading is
**Logging**.

In the Logs Explorer, you must specify one or more resources, but the resource
selection might not be obvious. Here are some tips to help you get
started:

- If you are deploying your application to App Engine or using the
  App Engine-specific libraries, set your resource to
  **GAE Application**.

- If you are deploying your application on Compute Engine, set the resource
  to **GCE VM Instance**.

- If you are deploying your application on Google Kubernetes Engine, your
  cluster's logging configuration determines the resource type of the log
  entries. For a detailed
  discussion on the Legacy Google Cloud Observability and the Google Cloud Observability
  Kubernetes Monitoring solutions, and how those options affect the
  resource type, see
  [Migrating to Google Cloud Observability Kubernetes Monitoring](https://docs.cloud.google.com/monitoring/kubernetes-engine/migration#stackdriver-options).

- If your application is using the Cloud Logging API directly,
  the resource is dependent on the API and your configuration. For example,
  in your application, you can specify a resource or use a default resource.

- If you don't see any logs in the Logs Explorer, to see all log entries,
  switch to the advanced query mode and use an empty query.

  1. To switch to the advanced query mode, click **menu** (▾) at the top of the Logs Explorer and then select **Convert to advanced filter**.
  2. Clear the content that appears in the filter box.
  3. Click **Submit Filter**.

  You can examine the individual entries to identify your resources.

For additional information, see [Using the Logs Explorer](https://docs.cloud.google.com/logging/docs/view/logs-explorer-interface).