From b120db5b6222d1b89577f9ca2ae8da8f5cbc3341 Mon Sep 17 00:00:00 2001 From: Timothy Stott Date: Fri, 2 Dec 2016 16:17:43 +0000 Subject: [PATCH] :guardsman: Fix encoding error when converting uploaded file to JSON --- lib/loga/ext/core/tempfile.rb | 7 +++++++ lib/loga/railtie.rb | 17 ++++++++++++----- spec/fixtures/random_bin | Bin 0 -> 933 bytes spec/integration/rails/railtie_spec.rb | 7 +++++++ spec/support/request_spec.rb | 10 ++++------ 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 lib/loga/ext/core/tempfile.rb create mode 100644 spec/fixtures/random_bin 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 0000000000000000000000000000000000000000..dc9a063a024666398fdfcaf37b729b807c3ee082 GIT binary patch literal 933 zcmV;W16uq%?4xKLDEs6}Vqye)R1iC>G**GNr=FIZkpfgWI=qbz11c-Z*>7lk zsS9w}Awvh%Bd+EfHm2+PrE6M+-vgpHUq9iqlK|nUtY=+|G9Jzk_B-&Jp`0W`GnDHN zk8Oyd&0p1_wBYS8 z2aw~GX0Yc$iY|u#-F_YAv}t#i$WENr#D$1;*NKM@^v;nS+1t+SIfl3bLFD2uxy+rz z=$z!Oz<-@hRTjWps~_s+Pe`89Ey4(twBMuDQ>O#3AQ#=@!BOVugJlksc}&oeKs*?+ zBWYGORvIZ{H0*0r&k*1*K0;npD~pyo8#fXSklTq%S|mDV9#&6>hVb$J&yI-}aHJGc zc2AlJnxV{O(VmD$56_!RhTu|k{S2DVkDiPRY4ivn0FNa_oIC53zkqwkmnu_ z^!RS{m^uVw8=uRwSK=;XH;4p0#NL!WQa$4-+{Bq@RA8_*evK;9F<;|wF01bsdy}FE zYunyzh@mAP`=DhMr0tBgdoow;fbTzAfFBefAa;aXOPK=f;?T%r+rMz)_XY?D2tK;A zpFbHlkRCb1MMAKk!pb@C(%ET(^K5-5iid$Aq<8HDpK~*848(3b#$)|vPQhTABJ1kF z!MmkW4k$C*M#Q4hFp#1huS;TiYTLge`@Z!Ce6$uVmoQugGZUP7e|Qe15ZQzTK zOZ|4gn5rZl|L '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', ), }, )