Skip to content

Commit

Permalink
Fix Lorekeeper::BacktraceCleaner to support BacktraceLocation (#41)
Browse files Browse the repository at this point in the history
* Fix Lorekeeper::BacktraceCleaner to support BacktraceLocation

* make rubocop happy
  • Loading branch information
ykitamura-mdsol authored Jan 17, 2024
1 parent 15c6d0f commit fbd22ea
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
ruby: '2.7'

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
12 changes: 8 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ AllCops:
#
- 'vendor/bundle/**/*'

Lint/ConstantDefinitionInBlock:
Exclude:
- 'spec/**/*'

Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Lint/ConstantDefinitionInBlock:
Exclude:
- 'spec/**/*'

Lint/StructNewOverride:
Exclude:
- spec/lib/lorekeeper/backtrace_cleaner_spec.rb

Metrics/ParameterLists:
CountKeywordArgs: false

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.6.3
* Fix Lorekeeper::BacktraceCleaner to support BacktraceLocation

# 2.6.2
* Fix respond_to? method signature

Expand Down
1 change: 1 addition & 0 deletions lib/lorekeeper/backtrace_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def filter_rails_root_backtrace(backtrace)
last_index = nil
result = []
backtrace.each_with_index do |line, idx|
line = line.to_s
if line.start_with?(@rails_root) && @gem_path.none? { |path| line.start_with?(path) }
result << line[@rails_root_size..]
last_index = idx
Expand Down
6 changes: 3 additions & 3 deletions lib/lorekeeper/json_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def reset_state
# Delegates methods to the existing Logger instance
# We are extending the logger API with methods error_with_data, etc
LOGGING_METHODS.each do |method_name|
define_method "#{method_name}_with_data", ->(message_param = nil, data = {}, &block) {
define_method :"#{method_name}_with_data", ->(message_param = nil, data = {}, &block) {
return true if METHOD_SEVERITY_MAP[method_name] < @level

extra_fields = { DATA => (data || {}) }
extra_fields = { DATA => data || {} }
with_extra_fields(extra_fields) do
add(METHOD_SEVERITY_MAP[method_name], message_param, nil, &block)
end
Expand Down Expand Up @@ -87,7 +87,7 @@ def exception(exception, custom_message = nil, custom_data = nil, custom_level =
else
log_data(METHOD_SEVERITY_MAP[:warn], 'Logger exception called without exception class.')
message = "#{exception.class}: #{exception.inspect} #{param_message}"
with_extra_fields(DATA => (param_data || {})) { log_data(log_level, message) }
with_extra_fields(DATA => param_data || {}) { log_data(log_level, message) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lorekeeper/simple_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def inspect

# Extending the logger API with methods error_with_data, etc
LOGGING_METHODS.each do |method_name|
define_method "#{method_name}_with_data", ->(message_param = nil, data = {}) {
define_method :"#{method_name}_with_data", ->(message_param = nil, data = {}) {
return true if METHOD_SEVERITY_MAP[method_name] < @level

log_data(METHOD_SEVERITY_MAP[method_name], "#{message_param}, data: #{data}")
Expand Down
2 changes: 1 addition & 1 deletion lib/lorekeeper/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Lorekeeper
VERSION = '2.6.2'
VERSION = '2.6.3'
end
12 changes: 10 additions & 2 deletions spec/lib/lorekeeper/backtrace_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
"/home/app/web/vendor/bundle/ruby/2.7.0/bin/rake:25:in `load'"
]
end
let(:new_backtrace_location) { new_backtrace.map { |bt| BacktraceLocation.new('', '', bt) } }

before do
allow(Gem).to receive(:path).and_return(['/ruby/2.5.0', '/home/app/web/vendor/bundle/ruby/2.7.0'])
stub_const('RbConfig::CONFIG', { 'rubylibdir' => '/usr/local/rvm/rubies/ruby-2.7.6/lib/ruby/2.7.0' })
stub_const('Rails', double(root: '/home/app/web'))
stub_const('BacktraceLocation', Struct.new(:path, :lineno, :to_s)) # https://github.com/rails/rails/blob/v7.1.2/activesupport/lib/active_support/syntax_error_proxy.rb#L15
end

context 'Logging just an exception' do
Expand All @@ -75,11 +77,17 @@
ActiveSupport::VERSION::MAJOR < 6 ? active_support_exception_less_than_v6 : active_support_exception_v6
end

it 'Does not log the lines matched with the denylist' do
it 'does not log the lines matched with the denylist' do
expect(instance.clean(new_backtrace)).to eq(no_noise_backtrace)
end

it 'Logs all backtraces when ActiveSupport::BacktraceCleaner and Rails.root are not defined' do
context 'with backtrace location' do
it 'does not log the lines matched with the denylist' do
expect(instance.clean(new_backtrace_location)).to eq(no_noise_backtrace)
end
end

it 'logs all backtraces when ActiveSupport::BacktraceCleaner and Rails.root are not defined' do
hide_const('ActiveSupport::BacktraceCleaner')
hide_const('Rails')

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/lorekeeper/json_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
expect(io.received_message.keys[0..2]).to eq(%w[timestamp message level])
end
it "Outputs the correct format for #{method}_with_data" do
logger.send("#{method}_with_data", message, data)
logger.send(:"#{method}_with_data", message, data)
expect(io.received_message).to eq(expected_data.merge('level' => level_name.call(method)))
end
end
Expand Down Expand Up @@ -396,7 +396,7 @@
end
it "No data is written to the device for #{method}_with_data" do
expect(io).not_to receive(:write)
logger.send("#{method}_with_data", message, data)
logger.send(:"#{method}_with_data", message, data)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/lorekeeper/multi_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
logger.add_logger(json_logger)

Lorekeeper::FastLogger::LOGGING_METHODS.each do |log_level|
logger.send("#{log_level}_with_data", message, { sum: 123 })
logger.send(:"#{log_level}_with_data", message, { sum: 123 })

expect(io.received_message).to include("#{message}, data: {:sum=>123}")
expect(json_io.received_message).to include(
Expand Down

0 comments on commit fbd22ea

Please sign in to comment.