Class: Profiler::Collectors::LogCollector

Inherits:
BaseCollector show all
Defined in:
lib/profiler/collectors/log_collector.rb

Defined Under Namespace

Classes: CaptureLogger

Constant Summary collapse

SEVERITY_LABELS =
%w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, #has_data?, inherited, #initialize, #panel_content, #render_html, #render_mode

Constructor Details

This class inherits a constructor from Profiler::Collectors::BaseCollector

Instance Method Details

#collectObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/profiler/collectors/log_collector.rb', line 73

def collect
  logs = Thread.current[:profiler_logs] || []

  if defined?(Rails) && Rails.logger && @capture_logger
    if Rails.logger.respond_to?(:stop_broadcasting_to)
      Rails.logger.stop_broadcasting_to(@capture_logger)
    end
  end

  Thread.current[:profiler_logs] = []

  errors   = logs.count { |l| %w[ERROR FATAL].include?(l[:level]) }
  warnings = logs.count { |l| l[:level] == "WARN" }

  store_data({
    count: logs.size,
    errors: errors,
    warnings: warnings,
    logs: logs
  })
end

#iconObject



35
36
37
# File 'lib/profiler/collectors/log_collector.rb', line 35

def icon
  "📋"
end

#nameObject



43
44
45
# File 'lib/profiler/collectors/log_collector.rb', line 43

def name
  "logs"
end

#priorityObject



39
40
41
# File 'lib/profiler/collectors/log_collector.rb', line 39

def priority
  18
end

#subscribeObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/profiler/collectors/log_collector.rb', line 58

def subscribe
  Thread.current[:profiler_logs] = []
  @capture_logger = CaptureLogger.new

  if defined?(Rails) && Rails.logger
    if Rails.logger.respond_to?(:broadcast_to)
      Rails.logger.broadcast_to(@capture_logger)
    elsif defined?(ActiveSupport::Logger)
      Rails.logger.extend(ActiveSupport::Logger.broadcast(@capture_logger))
    end
  end
rescue => e
  warn "LogCollector#subscribe failed: #{e.message}"
end

#tab_configObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/profiler/collectors/log_collector.rb', line 47

def tab_config
  {
    key: "logs",
    label: "Logs",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/profiler/collectors/log_collector.rb', line 95

def toolbar_summary
  return { text: "0 logs", color: "gray" } if @data.empty?

  count    = @data[:count] || 0
  errors   = @data[:errors] || 0
  warnings = @data[:warnings] || 0

  color = if errors > 0
    "red"
  elsif warnings > 0
    "orange"
  else
    "gray"
  end

  label = if errors > 0
    "#{errors} error#{errors != 1 ? "s" : ""}"
  elsif warnings > 0
    "#{warnings} warning#{warnings != 1 ? "s" : ""}"
  else
    "#{count} log#{count != 1 ? "s" : ""}"
  end

  { text: label, color: color }
end