Class: OpenC3::CosmosRailsFormatter

Inherits:
Object
  • Object
show all
Includes:
Authorization
Defined in:
lib/openc3/utilities/cosmos_rails_formatter.rb

Instance Method Summary collapse

Instance Method Details

#call(log, _appender = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
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
58
# File 'lib/openc3/utilities/cosmos_rails_formatter.rb', line 10

def call(log, _appender = nil)
  #<SemanticLogger::Log:0x0000ffffa5002b20 @level=:info, @thread_name="puma srv tp 001", @name="MicroservicesController",
  # @time=2024-09-22 18:04:27.955490052 +0000, @tags=[], @named_tags={}, @level_index=2, @message="Completed #traefik",
  # @payload={:controller=>"MicroservicesController", :action=>"traefik", :format=>"HTML", :method=>"GET",
  # :path=>"/openc3-api/traefik", :status=>200, :view_runtime=>0.41, :allocations=>1438, :status_message=>"OK",
  # :exception_object=>#<RuntimeError: death>}, @named_tags={:request_id=>"65446ef0-734e-4488-aa5f-e85b7b573fd8"},
  # @duration=3.2528750002384186, @metric=nil, @metric_amount=nil, @dimensions=nil, @exception=nil, @backtrace=nil>
  message = log.message
  other = {}
  other[:thread_name] = log.thread_name
  other[:duration] = log.duration if log.duration
  named_tags = log.named_tags
  username = nil
  if named_tags
    other[:request_id] = named_tags[:request_id]
    if named_tags[:token]
      begin
        user = (named_tags[:token])
      rescue
        user = {}
      end
      username = user['username']
      # Open Source username (EE has the actual username)
      username ||= 'anonymous'
    end
  end
  username ||= 'anonymous'
  payload = log.payload
  if payload
    other[:path] = payload[:path]
    other[:status] = payload[:status]
    other[:controller] = payload[:controller]
    other[:action] = payload[:action]
    other[:format] = payload[:format]
    other[:method] = payload[:method]
    other[:allocations] = payload[:allocations]
    other[:view_runtime] = payload[:view_runtime]
    if payload[:exception_object]
      other[:exception_message] = payload[:exception_object].message
      other[:exception_class] = payload[:exception_object].class.to_s
      other[:exception_backtrace] = payload[:exception_object].backtrace.as_json
    end
  end
  # This happens for a separate exception log entry which we want to not include the backtrace a second time
  if log.exception
    message = "Exception was raised - #{log.exception.class}:#{log.exception.message}" unless message
  end
  return OpenC3::Logger.build_log_data(log.level.to_s.upcase, message, user: username, type: OpenC3::Logger::LOG, url: nil, other: other).as_json(:allow_nan => true).to_json(:allow_nan => true)
end