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.
Most log formatters will render the message body as a string and ignore
the other attributes. The StructuredFormatter, however, will format the
full message data in the JSON format understood by the Google logging
agent.
Inherits
Object
Methods
.from
defself.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 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.
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, Hash, :caller, nil) (defaults to: nil) — The source
location for the log entry. Can be provided as a SourceLocation
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.
(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
defsource_location()->SourceLocation,nil
Returns
(SourceLocation, nil) — The source location for the log entry, or
nil if not present.
#span_id
defspan_id()->String,nil
Returns
(String, nil) — The trace span containing this entry, or nil if
not present.
#timestamp
deftimestamp()->Time,nil
Returns
(Time, nil) — The timestamp for the log entry, or nil if not
present.
#to_h
defto_h()->Hash
Returns
(Hash) — A hash of kwargs that can be passed to the constructor
to clone this message.
(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
deftrace()->String,nil
Returns
(String, nil) — The Google Cloud Trace resource name, or nil if
not present.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# google-logging-utils - Class Google::Logging::Message (v0.2.0)\n\nVersion latestkeyboard_arrow_down\n\n- [0.2.0 (latest)](/ruby/docs/reference/google-logging-utils/latest/Google-Logging-Message)\n- [0.1.0](/ruby/docs/reference/google-logging-utils/0.1.0/Google-Logging-Message) \nReference documentation and code samples for the google-logging-utils class Google::Logging::Message.\n\nA log message that can be formatted either as a normal text log entry or\nas a structured log entry suitable for Google Cloud Logging.\n\n\nA log message has a \"body\" which consists of either a string message, a\nJSON object (i.e. a Hash whose values are typically strings or numbers\nbut could be nested arrays and hashes), or both. It also includes several\nadditional optional fields used by the Google Cloud Logging backend.\n\n\u003cbr /\u003e\n\nMost log formatters will render the message body as a string and ignore\nthe other attributes. The [StructuredFormatter](/ruby/docs/reference/google-logging-utils/latest/Google-Logging-StructuredFormatter \"Google::Logging::StructuredFormatter (class)\"), however, will format the\nfull message data in the JSON format understood by the Google logging\nagent. \n\nInherits\n--------\n\n- Object\n\nMethods\n-------\n\n### .from\n\n def self.from(input) -\u003e Message\n\nCreate a log message from an input object, which can be any of:\n\n- An existing Message object.\n- A Hash. Symbol keys are used as keyword arguments to the [#initialize](/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.\n- Any other object is converted to a string with `to_s` and used as a simple text payload. \n**Parameter**\n\n- **input** (Object) --- A log message input object. \n**Returns**\n\n- ([Message](./Google-Logging-Message))\n\n### #fields\n\n def fields() -\u003e Hash{String=\u003eObject}, nil\n\n**Returns**\n\n- (Hash{String=\\\u003eObject}, nil) --- The log message as a set of key-value pairs, or nil if not present.\n\n### #full_message\n\n def full_message() -\u003e String\n\n**Aliases**\n\n- [#inspect](./Google-Logging-Message#Google__Logging__Message_inspect_instance_) \n**Returns**\n\n- (String) --- A full string representation of the message and fields as rendered in the standard logger formatter.\n\n### #initialize\n\n def initialize(message: nil, fields: nil, timestamp: nil, source_location: nil, insert_id: nil, trace: nil, span_id: nil, trace_sampled: nil, labels: nil) -\u003e Message\n\nLow-level constructor for a logging message.\nAll arguments are optional, with the exception that at least one of\n`:message` and `:fields` must be provided. \n**Parameters**\n\n- **message** (String) *(defaults to: nil)* --- The main log message as a string.\n- **fields** (Hash{String=\\\u003eObject}) *(defaults to: nil)* --- The log message as a set of key-value pairs representing a JSON object.\n- **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.\n- **source_location** ([SourceLocation](./Google-Logging-SourceLocation), Hash, :caller, nil) *(defaults to: nil)* --- The source location for the log entry. Can be provided as a [SourceLocation](/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.\n- **insert_id** (String) *(defaults to: nil)* --- A unique ID for this log entry that could be used on the backend to dedup messages. Optional.\n- **trace** (String) *(defaults to: nil)* --- A Google Cloud Trace resource name. Optional.\n- **span_id** (String) *(defaults to: nil)* --- The trace span containing this entry. Optional.\n- **trace_sampled** (boolean) *(defaults to: nil)* --- Whether this trace is sampled. Optional.\n- **labels** (Hash{String=\\\u003eString}) *(defaults to: nil)* --- Optional metadata. \n**Returns**\n\n- ([Message](./Google-Logging-Message)) --- a new instance of Message\n\n### #insert_id\n\n def insert_id() -\u003e String, nil\n\n**Returns**\n\n- (String, nil) --- The unique ID for this log entry that could be used on the backend to dedup messages, or nil if not present.\n\n### #inspect\n\n def inspect() -\u003e String\n\n**Alias Of** : [#full_message](./Google-Logging-Message#Google__Logging__Message_full_message_instance_) \n**Returns**\n\n- (String) --- A full string representation of the message and fields as rendered in the standard logger formatter.\n\n### #labels\n\n def labels() -\u003e Hash{String=\u003eString}, nil\n\n**Returns**\n\n- (Hash{String=\\\u003eString}, nil) --- Metadata, or nil if not present.\n\n### #message\n\n def message() -\u003e String\n\n**Aliases**\n\n- [#to_s](./Google-Logging-Message#Google__Logging__Message_to_s_instance_) \n**Returns**\n\n- (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.\n\n### #source_location\n\n def source_location() -\u003e SourceLocation, nil\n\n**Returns**\n\n- ([SourceLocation](./Google-Logging-SourceLocation), nil) --- The source location for the log entry, or nil if not present.\n\n### #span_id\n\n def span_id() -\u003e String, nil\n\n**Returns**\n\n- (String, nil) --- The trace span containing this entry, or nil if not present.\n\n### #timestamp\n\n def timestamp() -\u003e Time, nil\n\n**Returns**\n\n- (Time, nil) --- The timestamp for the log entry, or nil if not present.\n\n### #to_h\n\n def to_h() -\u003e Hash\n\n**Returns**\n\n- (Hash) --- A hash of kwargs that can be passed to the constructor to clone this message.\n\n### #to_s\n\n def to_s() -\u003e String\n\n**Alias Of** : [#message](./Google-Logging-Message#Google__Logging__Message_message_instance_) \n**Returns**\n\n- (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.\n\n### #trace\n\n def trace() -\u003e String, nil\n\n**Returns**\n\n- (String, nil) --- The Google Cloud Trace resource name, or nil if not present.\n\n### #trace_sampled\n\n def trace_sampled() -\u003e true, false, nil\n\n**Aliases**\n\n- [#trace_sampled?](./Google-Logging-Message#Google__Logging__Message_trace_sampled?_instance_) \n**Returns**\n\n- (true, false, nil) --- Whether this trace is sampled, or nil if not present or known.\n\n### #trace_sampled?\n\n def trace_sampled?() -\u003e true, false, nil\n\n**Alias Of** : [#trace_sampled](./Google-Logging-Message#Google__Logging__Message_trace_sampled_instance_) \n**Returns**\n\n- (true, false, nil) --- Whether this trace is sampled, or nil if not present or known."]]