# google-logging-utils - Class Google::Logging::Message (v0.2.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [0.2.0 (latest)](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message)
- [0.1.0](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/0.1.0/Google-Logging-Message)
Reference documentation and code samples for the google-logging-utils class Google::Logging::Message.

A log message that can be formatted either as a normal text log entry or
as a structured log entry suitable for Google Cloud Logging.


A log message has a "body" which consists of either a string message, a
JSON object (i.e. a Hash whose values are typically strings or numbers
but could be nested arrays and hashes), or both. It also includes several
additional optional fields used by the Google Cloud Logging backend.

<br />

Most log formatters will render the message body as a string and ignore
the other attributes. The [StructuredFormatter](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-StructuredFormatter "Google::Logging::StructuredFormatter (class)"), however, will format the
full message data in the JSON format understood by the Google logging
agent.

## Inherits

- Object

## Methods

### .from

    def self.from(input) -> Message

Create a log message from an input object, which can be any of:

- An existing Message object.
- A Hash. Symbol keys are used as keyword arguments to the [#initialize](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_initialize_instance_ "Google::Logging::Message#initialize (method)") constructor. String keys are treated as fields in the JSON payload.
- Any other object is converted to a string with `to_s` and used as a simple text payload.
**Parameter**

- **input** (Object) --- A log message input object.
**Returns**

- ([Message](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message))

### #fields

    def fields() -> Hash{String=>Object}, nil

**Returns**

- (Hash{String=\>Object}, nil) --- The log message as a set of key-value pairs, or nil if not present.

### #full_message

    def full_message() -> String

**Aliases**

- [#inspect](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_inspect_instance_)
**Returns**

- (String) --- A full string representation of the message and fields as rendered in the standard logger formatter.

### #initialize

    def initialize(message: nil, fields: nil, timestamp: nil, source_location: nil, insert_id: nil, trace: nil, span_id: nil, trace_sampled: nil, labels: nil) -> Message

Low-level constructor for a logging message.
All arguments are optional, with the exception that at least one of
`:message` and `:fields` must be provided.
**Parameters**

- **message** (String) *(defaults to: nil)* --- The main log message as a string.
- **fields** (Hash{String=\>Object}) *(defaults to: nil)* --- The log message as a set of key-value pairs representing a JSON object.
- **timestamp** (Time, Numeric, :now) *(defaults to: nil)* --- The timestamp for the log entry. Can be provided as a Time object, a Numeric indicating the seconds since epoch, or `:now` to use the current time. Optional.
- **source_location** ([SourceLocation](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-SourceLocation), Hash, :caller, nil) *(defaults to: nil)* --- The source location for the log entry. Can be provided as a [SourceLocation](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-SourceLocation "Google::Logging::SourceLocation (class)") object, a Hash containing exactly the keys `:file`, `:line`, and `:function`, or `:caller` to use the location of the caller. Optional.
- **insert_id** (String) *(defaults to: nil)* --- A unique ID for this log entry that could be used on the backend to dedup messages. Optional.
- **trace** (String) *(defaults to: nil)* --- A Google Cloud Trace resource name. Optional.
- **span_id** (String) *(defaults to: nil)* --- The trace span containing this entry. Optional.
- **trace_sampled** (boolean) *(defaults to: nil)* --- Whether this trace is sampled. Optional.
- **labels** (Hash{String=\>String}) *(defaults to: nil)* --- Optional metadata.
**Returns**

- ([Message](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message)) --- a new instance of Message

### #insert_id

    def insert_id() -> String, nil

**Returns**

- (String, nil) --- The unique ID for this log entry that could be used on the backend to dedup messages, or nil if not present.

### #inspect

    def inspect() -> String

**Alias Of** : [#full_message](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_full_message_instance_) **Returns**

- (String) --- A full string representation of the message and fields as rendered in the standard logger formatter.

### #labels

    def labels() -> Hash{String=>String}, nil

**Returns**

- (Hash{String=\>String}, nil) --- Metadata, or nil if not present.

### #message

    def message() -> String

**Aliases**

- [#to_s](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_to_s_instance_)
**Returns**

- (String) --- The message as a string. This is always present as a nonempty string, and can be reliably used as the "display" of this log entry in a list of entries.

### #source_location

    def source_location() -> SourceLocation, nil

**Returns**

- ([SourceLocation](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-SourceLocation), nil) --- The source location for the log entry, or nil if not present.

### #span_id

    def span_id() -> String, nil

**Returns**

- (String, nil) --- The trace span containing this entry, or nil if not present.

### #timestamp

    def timestamp() -> Time, nil

**Returns**

- (Time, nil) --- The timestamp for the log entry, or nil if not present.

### #to_h

    def to_h() -> Hash

**Returns**

- (Hash) --- A hash of kwargs that can be passed to the constructor to clone this message.

### #to_s

    def to_s() -> String

**Alias Of** : [#message](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_message_instance_) **Returns**

- (String) --- The message as a string. This is always present as a nonempty string, and can be reliably used as the "display" of this log entry in a list of entries.

### #trace

    def trace() -> String, nil

**Returns**

- (String, nil) --- The Google Cloud Trace resource name, or nil if not present.

### #trace_sampled

    def trace_sampled() -> true, false, nil

**Aliases**

- [#trace_sampled?](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_trace_sampled?_instance_)
**Returns**

- (true, false, nil) --- Whether this trace is sampled, or nil if not present or known.

### #trace_sampled?

    def trace_sampled?() -> true, false, nil

**Alias Of** : [#trace_sampled](https://docs.cloud.google.com/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message#Google__Logging__Message_trace_sampled_instance_) **Returns**

- (true, false, nil) --- Whether this trace is sampled, or nil if not present or known.