Skip to content

Commit

Permalink
Fix now-evaluation at Hanami::CommonLogger (#1061)
Browse files Browse the repository at this point in the history
* FIX: Change evaluation of now from Time.now to a monotonic clock at Hanami::CommonLogger (Rack uses a monotonic clock (Float) to capture began_at)

* CHG: introduce inner class `ElapsedTime`, because of the fact that Rack's `Rack::Utils::clock_time` is used since (Rack 2.1.0), but available since 2.0.0

* FIX: fix typo
  • Loading branch information
wuarmin committed Feb 16, 2021
1 parent e8aad95 commit 0d6425b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/hanami/common_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CommonLogger < Rack::CommonLogger
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def log(env, status, header, began_at)
now = Time.now
now = ElapsedTime.call
length = extract_content_length(header)

msg = Hash[
Expand Down Expand Up @@ -89,5 +89,21 @@ def extract_params(env)
result.merge!(Utils::Hash.deep_stringify(env.fetch(ROUTER_PARAMS, {})))
result
end

# Wrapper which uses Rack's monotonic clock_time (used for began_at since Rack 2.1.0)
#
# @since 1.3.4
# @api private
class ElapsedTime
@clock = if Gem::Version.new(Rack::RELEASE) >= Gem::Version.new('2.1.0')
-> { Rack::Utils.clock_time }
else
-> { Time.now }
end.freeze

def self.call
@clock.call
end
end
end
end
4 changes: 3 additions & 1 deletion spec/unit/hanami/common_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
get "/"

device.rewind
expect(device.read).to include(%(:path=>"logo.png"))
read_device = device.read
expect(read_device).to include(%(:path=>"logo.png"))
expect(read_device).to include(%(:elapsed=>0.))
end
end
end
Expand Down

0 comments on commit 0d6425b

Please sign in to comment.