Class: GoogleCloudRun::LogEntry

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/google_cloud_run/entry.rb

Constant Summary

Constants included from Logger::Severity

Logger::Severity::G_ALERT, Logger::Severity::G_CRITICAL, Logger::Severity::G_DEBUG, Logger::Severity::G_DEFAULT, Logger::Severity::G_EMERGENCY, Logger::Severity::G_ERROR, Logger::Severity::G_INFO, Logger::Severity::G_NOTICE, Logger::Severity::G_WARNING

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogEntry

Returns a new instance of LogEntry.



14
15
16
17
18
# File 'lib/google_cloud_run/entry.rb', line 14

def initialize
  @severity = G_DEFAULT
  @timestamp = Time.now.utc
  @insert_id = SecureRandom.uuid
end

Instance Attribute Details

#labelsObject

Returns the value of attribute labels.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def labels
  @labels
end

#location_lineObject

Returns the value of attribute location_line.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def location_line
  @location_line
end

#location_methodObject

Returns the value of attribute location_method.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def location_method
  @location_method
end

#location_pathObject

Returns the value of attribute location_path.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def location_path
  @location_path
end

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def message
  @message
end

#project_idObject

Returns the value of attribute project_id.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def project_id
  @project_id
end

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def request
  @request
end

#severityObject

Returns the value of attribute severity.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def severity
  @severity
end

#timestampObject

Returns the value of attribute timestamp.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def timestamp
  @timestamp
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/google_cloud_run/entry.rb', line 5

def user
  @user
end

Instance Method Details

#to_jsonObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google_cloud_run/entry.rb', line 20

def to_json
  raise "labels must be hash" if !@labels.blank? && !@labels.is_a?(Hash)

  labels["user"] = @user unless @user.blank?

  j = {}

  j["logging.googleapis.com/insertId"] = @insert_id
  j["severity"] = Severity.to_s(Severity.mapping(@severity))
  j["message"] = @message.is_a?(String) ? @message.strip : @message.inspect
  j["timestampSeconds"] = @timestamp.to_i
  j["timestampNanos"] = @timestamp.nsec
  j["logging.googleapis.com/labels"] = @labels unless @labels.blank?

  if @request
    # https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
    j["httpRequest"] = {}
    j["httpRequest"]["requestMethod"] = @request&.method.to_s
    j["httpRequest"]["requestUrl"] = @request&.url.to_s
    j["httpRequest"]["userAgent"] = @request&.headers["user-agent"].to_s unless @request&.headers["user-agent"].blank?
    j["httpRequest"]["remoteIp"] = @request&.remote_ip.to_s
    j["httpRequest"]["referer"] = @request&.headers["referer"].to_s unless @request&.headers["referer"].blank?

    trace, span, sample = GoogleCloudRun.parse_trace_context(@request&.headers["X-Cloud-Trace-Context"])
    j["logging.googleapis.com/trace"] = "projects/#{@project_id}/traces/#{trace}" unless trace.blank?
    j["logging.googleapis.com/spanId"] = span unless span.blank?
    j["logging.googleapis.com/trace_sampled"] = sample unless sample.nil?
  end

  if @location_path || @location_line || @location_method
    j["logging.googleapis.com/sourceLocation"] = {}
    j["logging.googleapis.com/sourceLocation"]["function"] = @location_method.to_s
    j["logging.googleapis.com/sourceLocation"]["file"] = @location_path.to_s
    j["logging.googleapis.com/sourceLocation"]["line"] = @location_line.to_i
  end

  j.to_json
end