Class: Faultline::Logger

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

Constant Summary collapse

LEVELS =

Log levels

%i[debug info warn error fatal].freeze
DEFAULT_CONTEXT =

Default context that's attached to every log entry

{
  gem: "faultline",
  version: Faultline::VERSION
}.freeze

Class Method Summary collapse

Class Method Details

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



31
32
33
# File 'lib/faultline/logger.rb', line 31

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

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

Log an exception with full context

Faultline::Logger.error(exception, controller: self, extra: { key: "value" })


19
20
21
# File 'lib/faultline/logger.rb', line 19

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

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

Log a custom event (not an exception)

Faultline::Logger.event("user.login", user_id: 123, method: "POST")


39
40
41
42
43
44
# File 'lib/faultline/logger.rb', line 39

def event(name, data = {})
  entry = build_entry(:info, name, nil, extra: data)
  write_to_output(entry)
  write_to_file(entry) if Faultline.configuration.log_file.present?
  entry
end

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



27
28
29
# File 'lib/faultline/logger.rb', line 27

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

Query logs with filters

Faultline::Logger.query(level: :error, since: 1.hour.ago, limit: 50)


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

def query(level: nil, since: nil, controller_name: nil, limit: 100)
  scope = Faultline::LoggedException.order(created_at: :desc)
  scope = scope.where("created_at >= ?", since) if since.present?
  scope = scope.where(exception_class: level.to_s.camelize) if level.present?
  scope = scope.where(controller_name: controller_name) if controller_name.present?
  scope.limit(limit)
end

.stats(since: nil) ⇒ Object

Get aggregated stats

Faultline::Logger.stats(since: 24.hours.ago)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/faultline/logger.rb', line 62

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

  {
    total: scope.count,
    by_class: scope.group(:exception_class).count,
    by_controller: scope.group(:controller_name).count,
    by_hour: scope.group_by { |e| e.created_at.hour }.transform_values(&:count),
    top_exceptions: scope.group(:exception_class).count.sort_by { |_, v| -v }.first(10)
  }
end

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



23
24
25
# File 'lib/faultline/logger.rb', line 23

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