diff --git a/lib/loga/ext/core/tempfile.rb b/lib/loga/ext/core/tempfile.rb new file mode 100644 index 0000000..9b2d796 --- /dev/null +++ b/lib/loga/ext/core/tempfile.rb @@ -0,0 +1,7 @@ +# Fixes encoding error when converting uploaded file to JSON +# https://github.com/rails/rails/issues/25250 +class Tempfile + def as_json(_ = nil) + to_s + end +end diff --git a/lib/loga/railtie.rb b/lib/loga/railtie.rb index 83ef695..2660141 100644 --- a/lib/loga/railtie.rb +++ b/lib/loga/railtie.rb @@ -17,6 +17,7 @@ def initialize(app) def call validate_user_options + change_tempfile_as_json Loga.configure(user_options, rails_options) app.config.colorize_logging = false if Loga.configuration.structured? app.config.logger = Loga.logger @@ -51,17 +52,23 @@ def sync Rails::VERSION::MAJOR > 3 ? app.config.autoflush_log : true end - # rubocop:disable Metrics/LineLength def validate_user_options if user_options[:tags].present? - raise Loga::ConfigurationError, 'Configure tags with Rails config.log_tags' + raise Loga::ConfigurationError, + 'Configure tags with Rails config.log_tags' elsif user_options[:level].present? - raise Loga::ConfigurationError, 'Configure level with Rails config.log_level' + raise Loga::ConfigurationError, + 'Configure level with Rails config.log_level' elsif user_options[:filter_parameters].present? - raise Loga::ConfigurationError, 'Configure filter_parameters with Rails config.filter_parameters' + raise Loga::ConfigurationError, + 'Configure filter_parameters with Rails config.filter_parameters' end end - # rubocop:enable Metrics/LineLength + + # Fixes encoding error when converting uploaded file to JSON + def change_tempfile_as_json + require 'loga/ext/core/tempfile' + end end initializer :loga_initialize_logger, before: :initialize_logger do |app| diff --git a/spec/fixtures/random_bin b/spec/fixtures/random_bin new file mode 100644 index 0000000..dc9a063 Binary files /dev/null and b/spec/fixtures/random_bin differ diff --git a/spec/integration/rails/railtie_spec.rb b/spec/integration/rails/railtie_spec.rb index 2e99607..bff2adc 100644 --- a/spec/integration/rails/railtie_spec.rb +++ b/spec/integration/rails/railtie_spec.rb @@ -4,6 +4,13 @@ let(:app) { Rails.application } let(:middlewares) { app.middleware.middlewares } + describe 'Tempfile' do + let(:tempfile) { Tempfile.new('README.md') } + it 'monkey patches #as_json' do + expect(tempfile.as_json).to eql(tempfile.to_s) + end + end + context 'development', if: Rails.env.development? do describe 'loga_initialize_logger' do let(:formatter) { Loga::Formatters::SimpleFormatter } diff --git a/spec/support/request_spec.rb b/spec/support/request_spec.rb index 5a76c23..447f8fb 100644 --- a/spec/support/request_spec.rb +++ b/spec/support/request_spec.rb @@ -182,11 +182,10 @@ end end - describe 'when the request uploads a file' do - it 'works' do + describe 'when the request uploads a binary file', focus: true do + it 'logs the request' do post '/users?username=yoshi', - { bob_file: Rack::Test::UploadedFile.new('README.md', 'text/markdown') }, - 'HTTP_X_REQUEST_ID' => '471a34dc' + bob_file: Rack::Test::UploadedFile.new('spec/fixtures/random_bin') expect(last_log_entry).to include( 'short_message' => 'POST /users?username=yoshi 200 in 0ms', @@ -194,9 +193,8 @@ '_request.params' => { 'username' => 'yoshi', 'bob_file' => include( - 'filename' => 'README.md', + 'filename' => 'random_bin', 'name' => 'bob_file', - 'type' => 'text/markdown', ), }, )