Class: GoogleLogger::Loggers::CloudLogger

Inherits:
Base
  • Object
show all
Defined in:
lib/google_logger/loggers/cloud_logger.rb

Instance Method Summary collapse

Constructor Details

#initializeCloudLogger

Creates a new logger with project_id and credentials specified in configuration



9
10
11
12
13
14
# File 'lib/google_logger/loggers/cloud_logger.rb', line 9

def initialize
  @project = Google::Cloud::Logging.new(
    project_id: configuration.project_id,
    credentials: configuration.credentials
  )
end

Instance Method Details

#build_entry(payload, log_name: 'default_log', severity: :DEFAULT) ⇒ Google::Cloud::Logging::Entry

Builds a new entry

Parameters:

  • payload (String, Hash)

    content of the log

  • log_name (String) (defaults to: 'default_log')

    log_name which can be used to filter logs

  • severity (Symbol) (defaults to: :DEFAULT)

    severity of the log

Returns:

  • (Google::Cloud::Logging::Entry)

    entry with payload and default resource configuration



23
24
25
26
27
28
# File 'lib/google_logger/loggers/cloud_logger.rb', line 23

def build_entry(payload, log_name: 'default_log', severity: :DEFAULT)
  entry = @project.entry(payload: payload, log_name: log_name, severity: severity, timestamp: Time.now)
  entry.resource.type = configuration.resource_type
  entry.resource.labels = configuration.resource_labels
  entry
end

#write_entry(entry) ⇒ Object

Writes an entry to google cloud

defaults to configuration value

return [Boolean] ‘true` if the entry was successfully written

Parameters:

  • entry (Google::Cloud::Logging::Entry)

    entry to be written to google cloud



36
37
38
39
# File 'lib/google_logger/loggers/cloud_logger.rb', line 36

def write_entry(entry)
  log_writer = configuration.async ? @project.async_writer : @project
  log_writer.write_entries(entry)
end