Cloud Logging API - Class Google::Cloud::Logging::Logger (v2.4.0)

Reference documentation and code samples for the Cloud Logging API class Google::Cloud::Logging::Logger.

Logger

An API-compatible replacement for ruby's Logger that logs to the Stackdriver Logging Service.

Inherits

  • Object

Examples

require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production
logger.info "Job started."

Provide a hash to write a JSON payload to the log:

require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

payload = { "stats" => { "a" => 8, "b" => 12.5} }
logger.info payload

Methods

#<<

def <<(msg)

Logs the given message at UNKNOWN severity.

Parameter
  • msg (String) — The log entry payload as a string.

#add

def add(severity, message = nil, progname = nil)
Aliases

Log a message if the given severity is high enough. This is the generic logging method. Users will be more inclined to use #debug, #info, #warn, #error, and #fatal.

Parameters
  • severity (Integer, String, Symbol) — the integer code for or the name of the severity level
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#add_request_info

def add_request_info(info: nil, env: nil, trace_id: nil, log_name: nil, trace_sampled: nil)

Associate request data with the current Thread. You may provide either the individual pieces of data (trace ID, log name) or a populated RequestInfo object.

Parameters
  • info (RequestInfo) (defaults to: nil) — Info about the current request. Optional. If not present, a new RequestInfo is created using the remaining parameters.
  • trace_id (String, nil) (defaults to: nil) — The trace ID, or nil if no trace ID should be logged.
  • log_name (String, nil) (defaults to: nil) — The log name to use, or nil to use this logger's default.
  • env (Hash, nil) (defaults to: nil) — The request's Rack environment or nil if not available.

#add_trace_id

def add_trace_id(trace_id)

Track a given trace_id by associating it with the current Thread

#close

def close()

Close the logging "device". This effectively disables logging from this logger; any further log messages will be silently ignored. The logger may be re-enabled by calling #reopen.

#datetime_format

def datetime_format()

This logger does not use a formatter, but it implements this attribute for API compatibility with the standard Logger.

#datetime_format=

def datetime_format=(value)

This logger does not use a formatter, but it implements this attribute for API compatibility with the standard Logger.

#debug

def debug(message = nil, &block)

Log a DEBUG entry.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#debug?

def debug?() -> Boolean

Returns true if the current severity level allows for sending DEBUG messages.

Returns
  • (Boolean)

#delete_request_info

def delete_request_info() -> RequestInfo

Untrack the RequestInfo that's associated with current Thread

Returns

#delete_trace_id

def delete_trace_id() -> RequestInfo

Untrack the RequestInfo that's associated with current Thread

Returns

#error

def error(message = nil, &block)

Log an ERROR entry.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#error?

def error?() -> Boolean

Returns true if the current severity level allows for sending ERROR messages.

Returns
  • (Boolean)

#fatal

def fatal(message = nil, &block)

Log a FATAL entry.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#fatal?

def fatal?() -> Boolean

Returns true if the current severity level allows for sending FATAL messages.

Returns
  • (Boolean)

#flush

def flush()

No-op method. Created to match the spec of ActiveSupport::Logger#flush method when used in Rails application.

#formatter

def formatter()

This logger does not use a formatter, but it provides a default Logger::Formatter for API compatibility with the standard Logger.

#formatter=

def formatter=(value)

This logger does not use a formatter, but it provides a default Logger::Formatter for API compatibility with the standard Logger.

#info

def info(message = nil, &block)

Log an INFO entry.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#info?

def info?() -> Boolean

Returns true if the current severity level allows for sending INFO messages.

Returns
  • (Boolean)

#initialize

def initialize(writer, log_name, resource, labels = nil) -> Google::Cloud::Logging::Logger

Create a new Logger instance.

Parameters
  • writer (#write_entries) — The object that will transmit log entries. Generally, to create a logger that blocks on transmitting log entries, pass the Project; otherwise, to create a logger that transmits log entries in the background, pass an AsyncWriter. You may also pass any other object that responds to #write_entries.
  • log_name (String) — A log resource name to be associated with the written log entries.
  • resource (Google::Cloud::Logging::Resource) — The monitored resource to be associated with written log entries.
  • labels (Hash) — A set of user-defined data to be associated with written log entries.
Returns
Example
require "google/cloud/logging"

logging = Google::Cloud::Logging.new

writer = logging.async_writer max_queue_size: 1000

resource = logging.resource "gae_app", labels: {
                              "module_id" => "1",
                              "version_id" => "20150925t173233"
                            }

logger = Google::Cloud::Logging::Logger.new writer,
                                            "my_app_log",
                                            resource,
                                            env: :production
logger.info "Job started."

#labels

def labels()

The Google Cloud labels to write the log entry with.

#level

def level()

The logging severity threshold (e.g. Logger::INFO)

#level=

def level=(severity)

Sets the logging severity level.

Example
require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

logger.level = "INFO"
logger.debug "Job started." # No log entry written

#local_level

def local_level()
Alias Of: #level

The logging severity threshold (e.g. Logger::INFO)

#local_level=

def local_level=(severity)
Alias Of: #level=

Sets the logging severity level.

Parameter
  • severity (Integer, String, Symbol) — the integer code for or the name of the severity level
Example
require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

logger.level = "INFO"
logger.debug "Job started." # No log entry written

#log

def log(severity, message = nil, progname = nil)
Alias Of: #add

Log a message if the given severity is high enough. This is the generic logging method. Users will be more inclined to use #debug, #info, #warn, #error, and #fatal.

Parameters
  • severity (Integer, String, Symbol) — the integer code for or the name of the severity level
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#log_name

def log_name()
Aliases

The Google Cloud log_name to write the log entry with.

#progname

def progname()
Alias Of: #log_name

The Google Cloud log_name to write the log entry with.

#progname=

def progname=(name)

This logger treats progname as an alias for log_name.

#project

def project()

The project ID this logger is sending data to. If set, this value is used to set the trace field of log entries.

#project=

def project=(value)

The project ID this logger is sending data to. If set, this value is used to set the trace field of log entries.

#reopen

def reopen(_logdev = nil)

Re-enable logging if the logger has been closed.

Note that this method accepts a "logdev" argument for compatibility with the standard Ruby Logger class; however, this argument is ignored because this logger does not use a log device.

#request_info

def request_info() -> RequestInfo, nil

Get the request data for the current Thread

Returns
  • (RequestInfo, nil) — The request data for the current thread, or nil if there is no data set.

#resource

def resource()

The Google Cloud resource to write the log entry with.

#sev_threshold

def sev_threshold()
Alias Of: #level

The logging severity threshold (e.g. Logger::INFO)

#sev_threshold=

def sev_threshold=(severity)
Alias Of: #level=

Sets the logging severity level.

Parameter
  • severity (Integer, String, Symbol) — the integer code for or the name of the severity level
Example
require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

logger.level = "INFO"
logger.debug "Job started." # No log entry written

#silence

def silence(temp_level = ::Logger::ERROR)

Filter out low severity messages within block.

Parameter
  • temp_level (Integer) — Severity threshold to filter within the block. Messages with lower severity will be blocked. Default ::Logger::ERROR
Example
require "google/cloud/logging"

logging = Google::Cloud::Logging.new

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "my_app_log", resource, env: :production

logger.silence do
  logger.info "Info message"   # No log entry written
  logger.error "Error message" # Log entry written
end

#silencer

def silencer()

Boolean flag that indicates whether this logger can be silenced or not.

#silencer=

def silencer=(value)

Boolean flag that indicates whether this logger can be silenced or not.

#trace_ids

def trace_ids()

A Hash of Thread IDs to Stackdriver request trace ID. The Stackdriver trace ID is a shared request identifier across all Stackdriver services.

This method is deprecated and returns a Hash containing only the current Thread ID/trace_id now.

#unknown

def unknown(message = nil, &block)

Log an UNKNOWN entry. This will be printed no matter what the logger's current severity level is.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#unknown?

def unknown?() -> Boolean

Returns true if the current severity level allows for sending UNKNOWN messages.

Returns
  • (Boolean)

#warn

def warn(message = nil, &block)

Log a WARN entry.

Parameter
  • message (String, Hash) — The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).
Yields
  • — Evaluates to the message to log. This is not evaluated unless the logger's level is sufficient to log the message. This allows you to create potentially expensive logging messages that are only called when the logger is configured to show them.

#warn?

def warn?() -> Boolean

Returns true if the current severity level allows for sending WARN messages.

Returns
  • (Boolean)

#writer

def writer()

The Google Cloud writer object that calls to #write_entries are made on. Either an AsyncWriter or Project object.