Class: Bullematic::BullematicLogger
- Inherits:
-
Object
- Object
- Bullematic::BullematicLogger
- Defined in:
- lib/bullematic/logger.rb,
sig/generated/bullematic/logger.rbs
Instance Attribute Summary collapse
- #stats ⇒ Object readonly
Instance Method Summary collapse
- #debug(message) ⇒ void
- #error(message) ⇒ void
- #info(message) ⇒ void
-
#initialize(logger = nil) ⇒ BullematicLogger
constructor
A new instance of BullematicLogger.
- #log_dry_run(filepath, original, modified) ⇒ void
- #log_error(filepath, err) ⇒ void
- #log_fix(filepath, detection) ⇒ void
- #log_plan(filepath, detection) ⇒ void
- #log_skip(filepath, detection, reason) ⇒ void
-
#log_summary ⇒ void
: () -> void.
-
#reset_stats ⇒ void
: () -> void.
- #warn(message) ⇒ void
Constructor Details
#initialize(logger = nil) ⇒ BullematicLogger
Returns a new instance of BullematicLogger.
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
#stats ⇒ Object (readonly)
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.
40 41 42 43 44 |
# File 'lib/bullematic/logger.rb', line 40 def debug() return unless Bullematic.configuration&.debug @logger.debug("[Bullematic] #{}") end |
#error(message) ⇒ void
This method returns an undefined value.
34 35 36 |
# File 'lib/bullematic/logger.rb', line 34 def error() @logger.error("[Bullematic] #{}") end |
#info(message) ⇒ void
This method returns an undefined value.
22 23 24 |
# File 'lib/bullematic/logger.rb', line 22 def info() @logger.info("[Bullematic] #{}") end |
#log_dry_run(filepath, original, modified) ⇒ void
This method returns an undefined value.
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.
78 79 80 81 |
# File 'lib/bullematic/logger.rb', line 78 def log_error(filepath, err) @stats[:errors] += 1 error("Error processing #{filepath}: #{err.}") end |
#log_fix(filepath, detection) ⇒ void
This method returns an undefined value.
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.
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.
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_summary ⇒ void
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_stats ⇒ void
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.
28 29 30 |
# File 'lib/bullematic/logger.rb', line 28 def warn() @logger.warn("[Bullematic] #{}") end |