class GoogleCloudRun::Logger

Public Class Methods

new() click to toggle source
# File lib/google_cloud_run/logger.rb, line 5
def initialize
  @level = G_INFO
  @formatter = DummyFormatter.new
  @out = Rails.application.config.google_cloudrun.out
  @project_id = GoogleCloudRun.project_id
end

Public Instance Methods

<<(msg) click to toggle source
# File lib/google_cloud_run/logger.rb, line 133
def <<(msg)
  log(G_DEBUG, msg)
end
add(severity, msg = nil, progname = nil, **labels, &block)
Alias for: log
alert(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 109
def alert(msg = nil, **labels, &block)
  write(G_ALERT, msg, labels, &block)
end
alert!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 117
def alert!
  self.level = G_ALERT
end
alert?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 113
def alert?
  self.level?(G_ALERT)
end
close() click to toggle source

implement ::Logger interface, but do nothing

# File lib/google_cloud_run/logger.rb, line 157
def close; end
critical(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 97
def critical(msg = nil, **labels, &block)
  write(G_CRITICAL, msg, labels, &block)
end
Also aliased as: fatal
critical!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 105
def critical!
  self.level = G_CRITICAL
end
Also aliased as: fatal!
critical?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 101
def critical?
  self.level?(G_CRITICAL)
end
Also aliased as: fatal?
datetime_format() click to toggle source
# File lib/google_cloud_run/logger.rb, line 148
def datetime_format
  "%FT%T.%9NZ" # RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits
end
datetime_format=(format) click to toggle source
# File lib/google_cloud_run/logger.rb, line 161
def datetime_format=(format); end
debug(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 37
def debug(msg = nil, **labels, &block)
  write(G_DEBUG, msg, labels, &block)
end
debug!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 45
def debug!
  self.level = G_DEBUG
end
debug?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 41
def debug?
  self.level?(G_DEBUG)
end
default(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 25
def default(msg = nil, **labels, &block)
  write(G_DEFAULT, msg, labels, &block)
end
Also aliased as: unknown
default!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 33
def default!
  self.level = G_DEFAULT
end
default?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 29
def default?
  self.level?(G_DEFAULT)
end
emergency(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 121
def emergency(msg = nil, **labels, &block)
  write(G_EMERGENCY, msg, labels, &block)
end
emergency!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 129
def emergency!
  self.level = G_EMERGENCY
end
emergency?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 125
def emergency?
  self.level?(G_EMERGENCY)
end
error(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 85
def error(msg = nil, **labels, &block)
  write(G_ERROR, msg, labels, &block)
end
error!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 93
def error!
  self.level = G_ERROR
end
error?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 89
def error?
  self.level?(G_ERROR)
end
fatal(msg = nil, **labels, &block)
Alias for: critical
fatal!()
Alias for: critical!
fatal?()
Alias for: critical?
flush() click to toggle source

called by ActiveSupport::LogSubscriber.flush_all!

# File lib/google_cloud_run/logger.rb, line 143
def flush
  Thread.current[thread_key] = nil
  @out.flush
end
formatter() click to toggle source
# File lib/google_cloud_run/logger.rb, line 152
def formatter
  @formatter
end
formatter=(formatter) click to toggle source
# File lib/google_cloud_run/logger.rb, line 163
def formatter=(formatter); end
info(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 49
def info(msg = nil, **labels, &block)
  write(G_INFO, msg, labels, &block)
end
info!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 57
def info!
  self.level = G_INFO
end
info?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 53
def info?
  self.level?(G_INFO)
end
inject_request(request) click to toggle source

called by LoggerMiddleware

# File lib/google_cloud_run/logger.rb, line 138
def inject_request(request)
  Thread.current[thread_key] = request
end
level=(level) click to toggle source
# File lib/google_cloud_run/logger.rb, line 12
def level=(level)
  @level = Severity.mapping(level)
end
Also aliased as: sev_threshold
level?(level) click to toggle source
# File lib/google_cloud_run/logger.rb, line 16
def level?(level)
  Severity.mapping(level) >= @level
end
log(severity, msg = nil, progname = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 20
def log(severity, msg = nil, progname = nil, **labels, &block)
  labels["progname"] = progname unless progname.blank?
  write(severity, msg, labels, &block)
end
Also aliased as: add
notice(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 61
def notice(msg = nil, **labels, &block)
  write(G_NOTICE, msg, labels, &block)
end
notice!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 69
def notice!
  self.level = G_NOTICE
end
notice?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 65
def notice?
  self.level?(G_NOTICE)
end
reopen(logdev = nil) click to toggle source
# File lib/google_cloud_run/logger.rb, line 159
def reopen(logdev = nil); end
sev_threshold(level)
Alias for: level=
unknown(msg = nil, **labels, &block)
Alias for: default
warn(msg = nil, **labels, &block)
Alias for: warning
warn!()
Alias for: warning!
warn?()
Alias for: warning?
warning(msg = nil, **labels, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 73
def warning(msg = nil, **labels, &block)
  write(G_WARNING, msg, labels, &block)
end
Also aliased as: warn
warning!() click to toggle source
# File lib/google_cloud_run/logger.rb, line 81
def warning!
  self.level = G_WARNING
end
Also aliased as: warn!
warning?() click to toggle source
# File lib/google_cloud_run/logger.rb, line 77
def warning?
  self.level?(G_WARNING)
end
Also aliased as: warn?

Private Instance Methods

current_request() click to toggle source
# File lib/google_cloud_run/logger.rb, line 276
def current_request
  Thread.current[thread_key]
end
should_error_report?(severity) click to toggle source
# File lib/google_cloud_run/logger.rb, line 181
def should_error_report?(severity)
  Rails.application.config.google_cloudrun.error_reporting &&
    !Rails.application.config.google_cloudrun.error_reporting_level.nil? &&
    Severity.mapping(severity) >= Severity.mapping(Rails.application.config.google_cloudrun.error_reporting_level)
end
should_log?(severity) click to toggle source
# File lib/google_cloud_run/logger.rb, line 177
def should_log?(severity)
  Rails.application.config.google_cloudrun.logger && self.level?(severity)
end
thread_key() click to toggle source
# File lib/google_cloud_run/logger.rb, line 280
def thread_key
  # We use our object ID here to avoid conflicting with other instances
  thread_key = @thread_key ||= "google_cloudrun_logging_request:#{object_id}"
end
write(severity, msg, labels = {}, &block) click to toggle source
# File lib/google_cloud_run/logger.rb, line 187
def write(severity, msg, labels = {}, &block)
  should_log = should_log?(severity)
  should_error_report = should_error_report?(severity)
  return false if !should_log && !should_error_report

  # execute given block
  msg = block.call if block

  # write error report
  if should_error_report
    write_error_report(severity, msg, labels)

    # return early if we don't want to log as well
    return true if Rails.application.config.google_cloudrun.error_reporting_discard_log
  end

  # write log
  if should_log
    write_log(severity, msg, labels)
  end

  return true
end
write_error_report(severity, msg, labels) click to toggle source
# File lib/google_cloud_run/logger.rb, line 243
def write_error_report(severity, msg, labels)
  l = ErrorReportingEntry.new
  l.severity = severity
  l.request = current_request
  l.labels = labels
  l.message = msg
  l.project_id = @project_id

  # set caller location
  loc = caller_locations(3, 1)&.first
  if loc
    l.location_path = loc.path
    l.location_line = loc.lineno
    l.location_method = loc.label
  end

  # set context
  l.context_service = GoogleCloudRun.k_service
  l.context_version = GoogleCloudRun.k_revision

  # attach user to entry
  p = Rails.application.config.google_cloudrun.error_reporting_user
  if p && p.is_a?(Proc)
    begin
      l.user = p.call(current_request)
    rescue
      # TODO ignore or log?
    end
  end

  @out.puts l.to_json
end
write_log(severity, msg, labels) click to toggle source
# File lib/google_cloud_run/logger.rb, line 211
def write_log(severity, msg, labels)
  l = GoogleCloudRun::LogEntry.new
  l.severity = severity
  l.message = msg
  l.labels = labels
  l.request = current_request
  l.project_id = @project_id

  # set caller location
  if Rails.application.config.google_cloudrun.logger_source_location
    loc = caller_locations(3, 1)&.first
    if loc
      l.location_path = loc.path
      l.location_line = loc.lineno
      l.location_method = loc.label
    end
  end

  # attach user to entry
  p = Rails.application.config.google_cloudrun.logger_user
  if p && p.is_a?(Proc)
    begin
      l.user = p.call(current_request)
    rescue
      raise
      # TODO ignore or log?
    end
  end

  @out.puts l.to_json
end