Class: RailsSemanticLogger::ActionView::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/rails_semantic_logger/action_view/log_subscriber.rb

Overview

Output Semantic logs from Action View.

Constant Summary collapse

VIEWS_PATTERN =
%r{^app/views/}.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogSubscriber

Returns a new instance of LogSubscriber.



14
15
16
17
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 14

def initialize
  @rails_root = nil
  super
end

Class Attribute Details

.loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 10

def logger
  @logger
end

.rendered_log_levelObject

Returns the value of attribute rendered_log_level.



11
12
13
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 11

def rendered_log_level
  @rendered_log_level
end

Instance Method Details

#render_collection(event) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 54

def render_collection(event)
  return unless should_log?

  identifier = event.payload[:identifier] || "templates"

  payload = {
    template: from_rails_root(identifier),
    count:    event.payload[:count]
  }
  payload[:cache_hits] = event.payload[:cache_hits] if event.payload[:cache_hits]
  payload[:allocations] = event.allocations if event.respond_to?(:allocations)

  logger.measure(
    self.class.rendered_log_level,
    "Rendered",
    payload:  payload,
    duration: event.duration
  )
end

#render_partial(event) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 36

def render_partial(event)
  return unless should_log?

  payload = {
    partial: from_rails_root(event.payload[:identifier])
  }
  payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout]
  payload[:cache]  = event.payload[:cache_hit] unless event.payload[:cache_hit].nil?
  payload[:allocations] = event.allocations if event.respond_to?(:allocations)

  logger.measure(
    self.class.rendered_log_level,
    "Rendered",
    payload:  payload,
    duration: event.duration
  )
end

#render_template(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 19

def render_template(event)
  return unless should_log?

  payload = {
    template: from_rails_root(event.payload[:identifier])
  }
  payload[:within] = from_rails_root(event.payload[:layout]) if event.payload[:layout]
  payload[:allocations] = event.allocations if event.respond_to?(:allocations)

  logger.measure(
    self.class.rendered_log_level,
    "Rendered",
    payload:  payload,
    duration: event.duration
  )
end

#start(name, id, payload) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/rails_semantic_logger/action_view/log_subscriber.rb', line 74

def start(name, id, payload)
  if (name == "render_template.action_view") && should_log?
    payload          = {template: from_rails_root(payload[:identifier])}
    payload[:within] = from_rails_root(payload[:layout]) if payload[:layout]

    logger.send(self.class.rendered_log_level, message: "Rendering", payload: payload)
  end

  super
end