Class: RailsNexus::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_nexus/logger.rb

Constant Summary collapse

LEVELS =
%i[debug info warn error fatal].freeze
MAX_CAUSE_DEPTH =
10
MAX_VALUE_DEPTH =
5
MAX_STRING_LENGTH =
10_000
DEFAULT_CONTEXT =
{
  gem: "rails_nexus",
  version: RailsNexus::VERSION
}.freeze

Class Method Summary collapse

Class Method Details

.debug(message, controller: nil, extra: {}, **kwargs) ⇒ Object



34
35
36
# File 'lib/rails_nexus/logger.rb', line 34

def debug(message, controller: nil, extra: {}, **kwargs)
  log(:debug, message, controller: controller, extra: extra, **kwargs)
end

.error(exception, controller: nil, extra: {}, **kwargs) ⇒ Object



18
19
20
# File 'lib/rails_nexus/logger.rb', line 18

def error(exception, controller: nil, extra: {}, **kwargs)
  log(:error, exception, controller: controller, extra: extra, **kwargs)
end

.event(name, data = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_nexus/logger.rb', line 38

def event(name, data = {})
  return unless loggable?(:info)

  entry = build_entry(:info, name.to_s, extra: data)
  write(entry)
  entry
rescue StandardError => error
  report_internal_error(error)
  nil
end

.fatal(exception, controller: nil, extra: {}, **kwargs) ⇒ Object



26
27
28
# File 'lib/rails_nexus/logger.rb', line 26

def fatal(exception, controller: nil, extra: {}, **kwargs)
  log(:fatal, exception, controller: controller, extra: extra, **kwargs)
end

.info(message, controller: nil, extra: {}, **kwargs) ⇒ Object



30
31
32
# File 'lib/rails_nexus/logger.rb', line 30

def info(message, controller: nil, extra: {}, **kwargs)
  log(:info, message, controller: controller, extra: extra, **kwargs)
end

.query(level: nil, since: nil, controller_name: nil, limit: 100) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/rails_nexus/logger.rb', line 49

def query(level: nil, since: nil, controller_name: nil, limit: 100)
  scope = RailsNexus::LoggedException.order(created_at: :desc)
  scope = scope.where("created_at >= ?", since) if since.present?
  scope = scope.none if level.present? && level.to_sym != :error
  scope = scope.where(controller_name: controller_name) if controller_name.present?
  scope.limit(limit)
end

.stats(since: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_nexus/logger.rb', line 57

def stats(since: nil)
  scope = RailsNexus::LoggedException.all
  scope = scope.where("created_at >= ?", since) if since.present?

  {
    total: scope.count,
    occurrences: scope.sum(:occurrence_count),
    by_class: scope.group(:exception_class).count,
    by_controller: scope.group(:controller_name).count,
    by_hour: scope.group_by { |entry| entry.created_at.hour }.transform_values(&:count),
    top_exceptions: scope.group(:exception_class).count.sort_by { |_, count| -count }.first(10),
    occurrences_by_class: scope.group(:exception_class).sum(:occurrence_count),
    occurrences_by_controller: scope.group(:controller_name).sum(:occurrence_count)
  }
end

.warn(exception, controller: nil, extra: {}, **kwargs) ⇒ Object



22
23
24
# File 'lib/rails_nexus/logger.rb', line 22

def warn(exception, controller: nil, extra: {}, **kwargs)
  log(:warn, exception, controller: controller, extra: extra, **kwargs)
end