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

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

  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 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

#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
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 , 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.
Returns
  • ( 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
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
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 , 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
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
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
Returns
  • (true, false, nil) — Whether this trace is sampled, or nil if not present or known.
Design a Mobile Site
View Site in Mobile | Classic
Share by: