Skip to content

Commit

Permalink
Merge pull request #97 from FundingCircle/file-upload
Browse files Browse the repository at this point in the history
Fixe encoding error when converting uploaded file to JSON
  • Loading branch information
timstott committed Dec 5, 2016
2 parents ec44d28 + 2d77a5c commit 4cc0a7f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.1.1] - 2016-12-02
### Fixed
- Encoding error when converting uploaded file to JSON
[rails/rails#25250](https://github.com/rails/rails/issues/25250)

## [2.1.0] - 2016-11-17
## [2.1.0.pre.1]
### Changed
Expand Down Expand Up @@ -48,6 +53,7 @@ when using simple format. The formatter adds level, timestamp, pid and tags prep
### Changed
- Silence ActionDispatch::DebugExceptions' logger

[2.1.1]: https://github.com/FundingCircle/loga/compare/v2.1.0...v2.1.1
[2.1.0]: https://github.com/FundingCircle/loga/compare/v2.0.0...v2.1.0
[2.1.0.pre.1]: https://github.com/FundingCircle/loga/compare/v2.0.0...v2.1.0.pre.1
[2.0.0]: https://github.com/FundingCircle/loga/compare/v1.4.0...v2.0.0
Expand Down
7 changes: 7 additions & 0 deletions lib/loga/ext/core/tempfile.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 12 additions & 5 deletions lib/loga/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion lib/loga/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Loga
VERSION = '2.1.0'.freeze
VERSION = '2.1.1'.freeze
end
Binary file added spec/fixtures/random_bin
Binary file not shown.
7 changes: 7 additions & 0 deletions spec/integration/rails/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
19 changes: 19 additions & 0 deletions spec/support/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,23 @@
end
end
end

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('spec/fixtures/random_bin')

expect(last_log_entry).to include(
'short_message' => 'POST /users?username=yoshi 200 in 0ms',
'level' => 6,
'_request.params' => {
'username' => 'yoshi',
'bob_file' => include(
'filename' => 'random_bin',
'name' => 'bob_file',
),
},
)
end
end
end

0 comments on commit 4cc0a7f

Please sign in to comment.