Skip to content

Commit c131211

Browse files
committed
Tune up Rails::Rack::Logger. Only put space between requests in development logs.
1 parent 86ebe0b commit c131211

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

railties/lib/rails/rack/logger.rb

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,45 @@
33

44
module Rails
55
module Rack
6-
# Log the request started and flush all loggers after it.
6+
# Sets log tags, logs the request, calls the app, and flushes the logs.
77
class Logger < ActiveSupport::LogSubscriber
8-
def initialize(app, tags=nil)
9-
@app, @tags = app, tags.presence
8+
def initialize(app, taggers = nil)
9+
@app, @taggers = app, taggers || []
1010
end
1111

1212
def call(env)
13-
if @tags && Rails.logger.respond_to?(:tagged)
14-
Rails.logger.tagged(compute_tags(env)) { call_app(env) }
13+
request = ActionDispatch::Request.new(env)
14+
15+
# Put some space between requests in development logs.
16+
Rails.logger.info "\n\n" if Rails.env.development?
17+
18+
if Rails.logger.respond_to?(:tagged)
19+
Rails.logger.tagged(compute_tags(request)) { call_app(request, env) }
1520
else
16-
call_app(env)
21+
call_app(request, env)
1722
end
1823
end
1924

2025
protected
2126

22-
def call_app(env)
23-
request = ActionDispatch::Request.new(env)
24-
path = request.filtered_path
25-
Rails.logger.info "\n\n"
26-
Rails.logger.info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{Time.now.to_default_s}"
27+
def call_app(request, env)
28+
Rails.logger.info started_request_message(request)
2729
@app.call(env)
2830
ensure
2931
ActiveSupport::LogSubscriber.flush_all!
3032
end
3133

32-
def compute_tags(env)
33-
request = ActionDispatch::Request.new(env)
34+
# Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700
35+
def started_request_message(request)
36+
'Started %s "%s" for %s at %s' % [
37+
request.request_method,
38+
request.filtered_path,
39+
request.ip,
40+
Time.now.to_default_s ]
41+
end
3442

35-
@tags.collect do |tag|
43+
def compute_tags(request)
44+
@taggers.collect do |tag|
3645
case tag
3746
when Proc
3847
tag.call(request)

railties/test/application/rack/logger_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ def teardown
2323
end
2424

2525
def logs
26-
@logs ||= @logger.logged(:info)
26+
@logs ||= @logger.logged(:info).join("\n")
2727
end
2828

2929
test "logger logs proper HTTP GET verb and path" do
3030
get "/blah"
3131
wait
32-
assert_match(/^Started GET "\/blah"/, logs[0])
32+
assert_match 'Started GET "/blah"', logs
3333
end
3434

3535
test "logger logs proper HTTP HEAD verb and path" do
3636
head "/blah"
3737
wait
38-
assert_match(/^Started HEAD "\/blah"/, logs[0])
38+
assert_match 'Started HEAD "/blah"', logs
3939
end
4040

4141
test "logger logs HTTP verb override" do
42-
post "/", {:_method => 'put'}
42+
post "/", _method: 'put'
4343
wait
44-
assert_match(/^Started PUT "\/"/, logs[0])
44+
assert_match 'Started PUT "/"', logs
4545
end
4646

4747
test "logger logs HEAD requests" do
48-
post "/", {:_method => 'head'}
48+
post "/", _method: 'head'
4949
wait
50-
assert_match(/^Started HEAD "\/"/, logs[0])
50+
assert_match 'Started HEAD "/"', logs
5151
end
5252
end
5353
end

0 commit comments

Comments
 (0)