Class: Bullematic::BullematicLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/bullematic/logger.rb,
sig/generated/bullematic/logger.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ BullematicLogger

Returns a new instance of BullematicLogger.

RBS:

  • logger: Logger?

  • return: void

Parameters:

  • logger (Logger, nil) (defaults to: nil)


15
16
17
18
# File 'lib/bullematic/logger.rb', line 15

def initialize(logger = nil)
  @logger = logger || Bullematic.configuration&.logger || ::Logger.new($stdout)
  @stats = { fixed: 0, planned: 0, skipped: 0, errors: 0 }
end

Instance Attribute Details

#statsObject (readonly)

RBS:

  • attr_reader stats: Hash[Symbol, Integer]

Returns:

  • (Object)


11
12
13
# File 'lib/bullematic/logger.rb', line 11

def stats
  @stats
end

Instance Method Details

#debug(message) ⇒ void

This method returns an undefined value.

RBS:

  • message: String

  • return: void

Parameters:

  • message (String)


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

def debug(message)
  return unless Bullematic.configuration&.debug

  @logger.debug("[Bullematic] #{message}")
end

#error(message) ⇒ void

This method returns an undefined value.

RBS:

  • message: String

  • return: void

Parameters:

  • message (String)


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

def error(message)
  @logger.error("[Bullematic] #{message}")
end

#info(message) ⇒ void

This method returns an undefined value.

RBS:

  • message: String

  • return: void

Parameters:

  • message (String)


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

def info(message)
  @logger.info("[Bullematic] #{message}")
end

#log_dry_run(filepath, original, modified) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • original: String

  • modified: String

  • return: void

Parameters:

  • filepath (String)
  • original (String)
  • modified (String)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bullematic/logger.rb', line 87

def log_dry_run(filepath, original, modified)
  original_lines = original.lines
  modified_lines = modified.lines
  prefix = 0
  prefix += 1 while original_lines[prefix] == modified_lines[prefix] && prefix < original_lines.size
  suffix = 0
  suffix += 1 while suffix < original_lines.size - prefix && suffix < modified_lines.size - prefix &&
                    original_lines[-suffix - 1] == modified_lines[-suffix - 1]
  removed = original_lines.slice(prefix, original_lines.size - prefix - suffix) || []
  added = modified_lines.slice(prefix, modified_lines.size - prefix - suffix) || []
  diff = ["[DRY RUN] Would modify #{filepath}:\n", "--- #{filepath}\n", "+++ #{filepath}\n",
          "@@ -#{prefix + 1},#{removed.size} +#{prefix + 1},#{added.size} @@\n"]
  removed.each { |line| diff << "-#{line.encode('UTF-8', invalid: :replace, undef: :replace)}" }
  added.each { |line| diff << "+#{line.encode('UTF-8', invalid: :replace, undef: :replace)}" }
  info(diff.join)
end

#log_error(filepath, err) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • err: Exception

  • return: void

Parameters:

  • filepath (String)
  • err (Exception)


78
79
80
81
# File 'lib/bullematic/logger.rb', line 78

def log_error(filepath, err)
  @stats[:errors] += 1
  error("Error processing #{filepath}: #{err.message}")
end

#log_fix(filepath, detection) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • detection: Detection

  • return: void

Parameters:



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

def log_fix(filepath, detection)
  @stats[:fixed] += 1
  info("Fixed N+1 query in #{filepath}:#{detection.line_number}")
  info("  Model: #{detection.model_class_name}")
  info("  Association: #{detection.associations.inspect}")
end

#log_plan(filepath, detection) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • detection: Detection

  • return: void

Parameters:



59
60
61
62
# File 'lib/bullematic/logger.rb', line 59

def log_plan(filepath, detection)
  @stats[:planned] += 1
  info("Planned N+1 fix in #{filepath}:#{detection.line_number}")
end

#log_skip(filepath, detection, reason) ⇒ void

This method returns an undefined value.

RBS:

  • filepath: String

  • detection: Detection

  • reason: String

  • return: void

Parameters:

  • filepath (String)
  • detection (Detection)
  • reason (String)


68
69
70
71
72
73
# File 'lib/bullematic/logger.rb', line 68

def log_skip(filepath, detection, reason)
  @stats[:skipped] += 1
  warn("Skipped #{filepath}:#{detection.line_number} (#{reason})")
  warn("  Model: #{detection.model_class_name}")
  warn("  Association: #{detection.associations.inspect}")
end

#log_summaryvoid

This method returns an undefined value.

: () -> void



105
106
107
108
# File 'lib/bullematic/logger.rb', line 105

def log_summary
  info("Summary: #{@stats[:fixed]} fixed, #{@stats[:planned]} planned, " \
       "#{@stats[:skipped]} skipped, #{@stats[:errors]} errors")
end

#reset_statsvoid

This method returns an undefined value.

: () -> void



111
112
113
# File 'lib/bullematic/logger.rb', line 111

def reset_stats
  @stats = { fixed: 0, planned: 0, skipped: 0, errors: 0 }
end

#warn(message) ⇒ void

This method returns an undefined value.

RBS:

  • message: String

  • return: void

Parameters:

  • message (String)


28
29
30
# File 'lib/bullematic/logger.rb', line 28

def warn(message)
  @logger.warn("[Bullematic] #{message}")
end