# Cloud Logging API - Class Google::Cloud::Logging::Middleware (v2.7.0)

2.7.0 (latest) 2.6.1 2.5.0 2.4.0 2.3.3 2.2.2 Reference documentation and code samples for the Cloud Logging API class Google::Cloud::Logging::Middleware.

## Inherits

- Object

## Methods

### .build_monitored_resource

    def self.build_monitored_resource(type = nil, labels = nil) -> Google::Cloud::Logging::Resource

Construct a monitored resource based on the given type and label if
both are provided. Otherwise, construct a default monitored resource
based on the current environment.
**Parameters**

- **type** (String) --- Type of Google::Cloud::Logging::Resource
- **labels** (Hash\<String, String\>) --- Metadata lebels of Google::Cloud::Logging::Resource
**Returns**

- ([Google::Cloud::Logging::Resource](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-logging/latest/Google-Cloud-Logging-Resource)) --- An Resource object with type and labels
**Examples**

If both type and labels are provided, it returns resource:

```ruby
rc = Google::Cloud::Logging::Middleware.build_monitored_resource(
       "aws_ec2_instance",
       {
         instance_id: "ec2-id",
         aws_account: "aws-id"
       }
     )
rc.type   #=> "aws_ec2_instance"
rc.labels #=> { instance_id: "ec2-id", aws_account: "aws-id" }
```

If running from GAE, returns default resource:

```ruby
rc = Google::Cloud::Logging::Middleware.build_monitored_resource
rc.type   #=> "gae_app"
rc.labels # { module_id: [GAE module name],
          #   version_id: [GAE module version] }
```

If running from GKE, returns default resource:

```ruby
rc = Google::Cloud::Logging::Middleware.build_monitored_resource
rc.type   #=> "container"
rc.labels # { cluster_name: [GKE cluster name],
          #   namespace_id: [GKE namespace_id] }
```

If running from GCE, return default resource:

```ruby
rc = Google::Cloud::Logging::Middleware.build_monitored_resource
rc.type   #=> "gce_instance"
rc.labels # { instance_id: [GCE VM instance id],
          #   zone: [GCE vm group zone] }
```

Otherwise default to generic "global" type:

```ruby
rc = Google::Cloud::Logging::Middleware.build_monitored_resource
rc.type   #=> "global"
rc.labels #=> {}
```

### #call

    def call(env) -> Rack::Response

Rack middleware entry point. In most Rack based frameworks, a request
is served by one thread. So entry point, we associate the GCP request
trace_id with the current thread's object_id in logger. All the logs
written by logger beyond this point will carry this request's
trace_id. Untrack the trace_id with this thread upon exiting.
**Parameter**

- **env** (Hash) --- Rack environment hash
**Returns**

- (Rack::Response) --- The response from downstream Rack app

### #initialize

    def initialize(app, logger: nil, on_init: nil, **kwargs) -> Google::Cloud::Logging::Middleware

Create a new AppEngine logging Middleware.
**Parameters**

- **app** (Rack Application) --- Rack application
- **logger** ([Google::Cloud::Logging::Logger](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-logging/latest/Google-Cloud-Logging-Logger)) *(defaults to: nil)* --- A logger to be used by this middleware. The middleware will be interacting with the logger to track Stackdriver request trace ID. It also properly sets env\["rack.logger"\] to this assigned logger for accessing. If not specified, a default logger with be used.
- **on_init** (Proc) *(defaults to: nil)* --- A callback to be invoked when the middleware is initialized. The callback takes no arguments. Optional.
- **kwargs** (Hash) --- Hash of configuration settings. Used for backward API compatibility. See the [Configuration
  Guide](https://googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html) for the prefered way to set configuration parameters.
**Returns**

- ([Google::Cloud::Logging::Middleware](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-logging/latest/Google-Cloud-Logging-Middleware)) --- A new Google::Cloud::Logging::Middleware instance

### #logger

    def logger()

The Google::Cloud::Logging::Logger instance

## Constants

### DEFAULT_LOG_NAME

**value:** "ruby_app_log".freeze  
The default log name used to instantiate the default logger if one
isn't provided.

### DEFAULT_LOG_NAME_MAP

**value:** { "/_ah/health" =\> "ruby_health_check_log",
"/healthz" =\> "ruby_health_check_log" }.freeze  
A default value for the log_name_map argument. Directs health check
logs to a separate log name so they don't spam the main log.