Class: Bidi2pdf::Bidi::LoggerEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/bidi2pdf/bidi/logger_events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_id) ⇒ LoggerEvents

Returns a new instance of LoggerEvents.



11
12
13
14
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 11

def initialize(context_id)
  @context_id = context_id
  @browser_console_logger = BrowserConsoleLogger.new(Bidi2pdf.browser_console_logger)
end

Instance Attribute Details

#browser_console_loggerObject (readonly)

Returns the value of attribute browser_console_logger.



9
10
11
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 9

def browser_console_logger
  @browser_console_logger
end

#context_idObject (readonly)

Returns the value of attribute context_id.



9
10
11
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 9

def context_id
  @context_id
end

Instance Method Details

#handle_event(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 16

def handle_event(data)
  event = data["params"]
  method = data["method"]

  if event.dig("source", "context") == context_id
    handle_response(method, event)
  else
    # this should be Bidi2pdf.logger and not Bidi2pdf.browser_console_logger
    Bidi2pdf.logger.debug2 "Ignoring Log event: #{method}, context_id: #{context_id}, params: #{event}"
  end
rescue StandardError => e
  # this should be Bidi2pdf.logger and not Bidi2pdf.browser_console_logger
  Bidi2pdf.logger.error "Error handling Log event: #{e.message}\n#{e.backtrace&.join("\n")}"
end

#handle_response(_method, event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 31

def handle_response(_method, event)
  level = resolve_log_level(event["level"])
  text = event["text"]
  args = event["args"] || []
  stack_trace = event["stackTrace"]
  timestamp = event["timestamp"]

  Bidi2pdf.notification_service.instrument("browser_console_log_received.bidi2pdf",
                                           {
                                             level: level,
                                             text: text,
                                             args: args,
                                             stack_trace: stack_trace,
                                             timestamp: timestamp
                                           })

  browser_console_logger.builder
                        .with_level(level)
                        .with_timestamp(timestamp)
                        .with_text(text)
                        .with_args(args)
                        .with_stack_trace(stack_trace)
                        .log_event
end

#resolve_log_level(js_level) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/bidi2pdf/bidi/logger_events.rb', line 56

def resolve_log_level(js_level)
  case js_level
  when "info", "warn", "error", "trace"
    js_level.to_sym
  else
    :debug
  end
end