renaming to :condensed

This commit is contained in:
Mason McLead 2017-06-02 09:50:46 -07:00
parent 4276cd642a
commit 6546c93512
5 changed files with 41 additions and 25 deletions

5
.byebug_history Normal file
View File

@ -0,0 +1,5 @@
exit
c
log_statements.compact.delete_if(&:empty?).each(&:strip!)
log_statements.compact.delete_if(&:empty?).each(&:lstrip)
log_statements.compact.delete_if(&:empty?).each(&:strip)

View File

@ -52,7 +52,7 @@ Completed 422 in 6.29ms
The middleware logger can be customized with the following options: The middleware logger can be customized with the following options:
* The `:logger` option can be any object that responds to `.info(String)` * The `:logger` option can be any object that responds to `.info(String)`
* The `:one_line` option configures the log output to be on one line instead of multiple. It accepts `true` or `false`. The default configuration is `false` * The `:condensed` option configures the log output to be on one line instead of multiple. It accepts `true` or `false`. The default configuration is `false`
* The `:filter` option can be any object that responds to `.filter(Hash)` and returns a hash. * The `:filter` option can be any object that responds to `.filter(Hash)` and returns a hash.
* The `:headers` option can be either `:all` or array of strings. * The `:headers` option can be either `:all` or array of strings.
+ If `:all`, all request headers will be output. + If `:all`, all request headers will be output.
@ -63,10 +63,9 @@ For example:
```ruby ```ruby
insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger, { insert_after Grape::Middleware::Formatter, Grape::Middleware::Logger, {
logger: Logger.new(STDERR), logger: Logger.new(STDERR),
one_line: true, condensed: true,
filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new, filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new,
headers: %w(version cache-control) headers: %w(version cache-control)
filter: Class.new { def filter(opts) opts.reject { |k, _| k.to_s == 'password' } end }.new
} }
``` ```

View File

@ -7,7 +7,7 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
attr_reader :logger attr_reader :logger
class << self class << self
attr_accessor :logger, :filter, :one_line, :headers attr_accessor :logger, :filter, :headers, :condensed
def default_logger def default_logger
default = Logger.new(STDOUT) default = Logger.new(STDOUT)
@ -20,7 +20,7 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
super super
@options[:filter] ||= self.class.filter @options[:filter] ||= self.class.filter
@options[:headers] ||= self.class.headers @options[:headers] ||= self.class.headers
@options[:one_line] ||= false @options[:condensed] ||= false
@logger = options[:logger] || self.class.logger || self.class.default_logger @logger = options[:logger] || self.class.logger || self.class.default_logger
end end
@ -28,19 +28,19 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
start_time start_time
# sets env['grape.*'] # sets env['grape.*']
super super
if @options[:one_line]
logger.info %Q(Started #{env[Grape::Env::GRAPE_REQUEST].request_method} "#{env[Grape::Env::GRAPE_REQUEST].path}" at #{start_time.to_s} - Processing by #{processed_by} - Parameters: #{parameters}) log_statements = [
else '',
logger.info '' %Q(Started %s "%s" at %s) % [
logger.info %Q(Started %s "%s" at %s) % [
env[Grape::Env::GRAPE_REQUEST].request_method, env[Grape::Env::GRAPE_REQUEST].request_method,
env[Grape::Env::GRAPE_REQUEST].path, env[Grape::Env::GRAPE_REQUEST].path,
start_time.to_s start_time.to_s
] ],
logger.info %Q(Processing by #{processed_by}) %Q(Processing by #{processed_by}),
logger.info %Q( Parameters: #{parameters}) %Q( Parameters: #{parameters})]
logger.info %Q( Headers: #{headers}) if @options[:headers]
end log_statements.append(%Q( Headers: #{headers})) if @options[:headers]
log_info(log_statements)
end end
# @note Error and exception handling are required for the +after+ hooks # @note Error and exception handling are required for the +after+ hooks
@ -69,8 +69,12 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
end end
def after(status) def after(status)
logger.info "Completed #{status} in #{((Time.now - start_time) * 1000).round(2)}ms" log_info(
logger.info '' unless @options[:one_line] [
"Completed #{status} in #{((Time.now - start_time) * 1000).round(2)}ms",
''
]
)
end end
# #
@ -125,6 +129,14 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
result.concat endpoint.options[:path].map { |path| path.to_s.sub(BACKSLASH, '') } result.concat endpoint.options[:path].map { |path| path.to_s.sub(BACKSLASH, '') }
endpoint.options[:for].to_s << result.join(BACKSLASH) endpoint.options[:for].to_s << result.join(BACKSLASH)
end end
def log_info(log_statements=[])
if @options[:condensed]
logger.info log_statements.compact.delete_if(&:empty?).each(&:strip!).join(" - ")
else
log_statements.each { |log_statement| logger.info log_statement }
end
end
end end
require_relative 'logger/railtie' if defined?(Rails) require_relative 'logger/railtie' if defined?(Rails)

View File

@ -11,8 +11,8 @@ describe Grape::Middleware::Logger, type: :integration do
let(:grape_endpoint) { build(:grape_endpoint) } let(:grape_endpoint) { build(:grape_endpoint) }
let(:env) { build(:expected_env, grape_endpoint: grape_endpoint) } let(:env) { build(:expected_env, grape_endpoint: grape_endpoint) }
context 'when the option[:one_line] is false' do context 'when the option[:condensed] is false' do
let(:options) { { filter: build(:param_filter), logger: Logger.new(Tempfile.new('logger')), one_line: false } } let(:options) { { filter: build(:param_filter), logger: Logger.new(Tempfile.new('logger')), condensed: false } }
it 'logs all parts of the request on multiple lines' do it 'logs all parts of the request on multiple lines' do
expect(subject.logger).to receive(:info).with '' expect(subject.logger).to receive(:info).with ''
@ -25,8 +25,8 @@ describe Grape::Middleware::Logger, type: :integration do
end end
end end
context 'when the options[:one_line is true' do context 'when the options[:condensed is true' do
let(:options) { { filter: build(:param_filter), logger: Logger.new(Tempfile.new('logger')), one_line: true } } let(:options) { { filter: build(:param_filter), logger: Logger.new(Tempfile.new('logger')), condensed: true } }
it 'logs all parts of the request on one line' do it 'logs all parts of the request on one line' do
expect(subject.logger).to receive(:info).with %Q(Started POST "/api/1.0/users" at #{subject.start_time} - Processing by TestAPI/users - Parameters: {"id"=>"101001", "secret"=>"[FILTERED]", "customer"=>[], "name"=>"foo", "password"=>"[FILTERED]"}) expect(subject.logger).to receive(:info).with %Q(Started POST "/api/1.0/users" at #{subject.start_time} - Processing by TestAPI/users - Parameters: {"id"=>"101001", "secret"=>"[FILTERED]", "customer"=>[], "name"=>"foo", "password"=>"[FILTERED]"})

View File

@ -42,8 +42,8 @@ describe Grape::Middleware::Logger, type: :rails_integration do
end end
end end
context 'when the option[:one_line] is false' do context 'when the option[:condensed] is false' do
let(:options) { { one_line: false } } let(:options) { { condensed: false } }
it 'logs all parts of the request on multiple lines' do it 'logs all parts of the request on multiple lines' do
expect(subject.logger).to receive(:info).with '' expect(subject.logger).to receive(:info).with ''
@ -56,8 +56,8 @@ describe Grape::Middleware::Logger, type: :rails_integration do
end end
end end
context 'when the option[:one_line] is true' do context 'when the option[:condensed] is true' do
let(:options) { { one_line: true } } let(:options) { { condensed: true } }
it 'logs all parts of the request on one line' do it 'logs all parts of the request on one line' do
expect(subject.logger).to receive(:info).with %Q(Started POST "/api/1.0/users" at #{subject.start_time} - Processing by TestAPI/users - Parameters: {"id"=>"101001", "secret"=>"key", "customer"=>[], "name"=>"foo", "password"=>"[FILTERED]"}) expect(subject.logger).to receive(:info).with %Q(Started POST "/api/1.0/users" at #{subject.start_time} - Processing by TestAPI/users - Parameters: {"id"=>"101001", "secret"=>"key", "customer"=>[], "name"=>"foo", "password"=>"[FILTERED]"})
expect(subject.logger).to receive(:info).with /Completed 200 in \d+.\d+ms/ expect(subject.logger).to receive(:info).with /Completed 200 in \d+.\d+ms/