Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
27d9836a
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
27d9836a
编写于
1月 17, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ActionDispatch::Railties::Subscriber and finish tidying up the logging.
上级
61ada28e
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
208 addition
and
91 deletion
+208
-91
actionpack/lib/action_controller/metal/instrumentation.rb
actionpack/lib/action_controller/metal/instrumentation.rb
+0
-4
actionpack/lib/action_controller/railtie.rb
actionpack/lib/action_controller/railtie.rb
+0
-48
actionpack/lib/action_controller/railties/subscriber.rb
actionpack/lib/action_controller/railties/subscriber.rb
+1
-6
actionpack/lib/action_dispatch/railtie.rb
actionpack/lib/action_dispatch/railtie.rb
+58
-0
actionpack/lib/action_dispatch/railties/subscriber.rb
actionpack/lib/action_dispatch/railties/subscriber.rb
+17
-0
actionpack/test/activerecord/controller_runtime_test.rb
actionpack/test/activerecord/controller_runtime_test.rb
+2
-2
actionpack/test/controller/subscriber_test.rb
actionpack/test/controller/subscriber_test.rb
+14
-28
actionpack/test/dispatch/subscriber_test.rb
actionpack/test/dispatch/subscriber_test.rb
+112
-0
activerecord/lib/active_record/railties/subscriber.rb
activerecord/lib/active_record/railties/subscriber.rb
+1
-1
railties/lib/rails.rb
railties/lib/rails.rb
+3
-2
未找到文件。
actionpack/lib/action_controller/metal/instrumentation.rb
浏览文件 @
27d9836a
...
@@ -20,11 +20,7 @@ def process_action(action, *args)
...
@@ -20,11 +20,7 @@ def process_action(action, *args)
result
=
super
result
=
super
payload
[
:controller
]
=
self
.
class
.
name
payload
[
:controller
]
=
self
.
class
.
name
payload
[
:action
]
=
self
.
action_name
payload
[
:action
]
=
self
.
action_name
payload
[
:formats
]
=
request
.
formats
.
map
(
&
:to_s
)
payload
[
:remote_ip
]
=
request
.
remote_ip
payload
[
:method
]
=
request
.
method
payload
[
:status
]
=
response
.
status
payload
[
:status
]
=
response
.
status
payload
[
:request_uri
]
=
request
.
request_uri
rescue
"unknown"
append_info_to_payload
(
payload
)
append_info_to_payload
(
payload
)
result
result
end
end
...
...
actionpack/lib/action_controller/railtie.rb
浏览文件 @
27d9836a
...
@@ -39,53 +39,5 @@ class Railtie < Rails::Railtie
...
@@ -39,53 +39,5 @@ class Railtie < Rails::Railtie
ActionController
::
Base
.
view_paths
=
view_path
if
ActionController
::
Base
.
view_paths
.
blank?
ActionController
::
Base
.
view_paths
=
view_path
if
ActionController
::
Base
.
view_paths
.
blank?
end
end
class
MetalMiddlewareBuilder
def
initialize
(
metals
)
@metals
=
metals
end
def
new
(
app
)
ActionDispatch
::
Cascade
.
new
(
@metals
,
app
)
end
def
name
ActionDispatch
::
Cascade
.
name
end
alias_method
:to_s
,
:name
end
initializer
"action_controller.initialize_metal"
do
|
app
|
metal_root
=
"
#{
Rails
.
root
}
/app/metal"
load_list
=
app
.
config
.
metals
||
Dir
[
"
#{
metal_root
}
/**/*.rb"
]
metals
=
load_list
.
map
{
|
metal
|
metal
=
File
.
basename
(
metal
.
gsub
(
"
#{
metal_root
}
/"
,
''
),
'.rb'
)
require_dependency
metal
metal
.
camelize
.
constantize
}.
compact
middleware
=
MetalMiddlewareBuilder
.
new
(
metals
)
app
.
config
.
middleware
.
insert_before
(
:"ActionDispatch::ParamsParser"
,
middleware
)
end
# Prepare dispatcher callbacks and run 'prepare' callbacks
initializer
"action_controller.prepare_dispatcher"
do
|
app
|
# TODO: This used to say unless defined?(Dispatcher). Find out why and fix.
# Notice that at this point, ActionDispatch::Callbacks were already loaded.
require
'rails/dispatcher'
unless
app
.
config
.
cache_classes
# Setup dev mode route reloading
routes_last_modified
=
app
.
routes_changed_at
reload_routes
=
lambda
do
unless
app
.
routes_changed_at
==
routes_last_modified
routes_last_modified
=
app
.
routes_changed_at
app
.
reload_routes!
end
end
ActionDispatch
::
Callbacks
.
before
{
|
callbacks
|
reload_routes
.
call
}
end
end
end
end
end
end
actionpack/lib/action_controller/railties/subscriber.rb
浏览文件 @
27d9836a
...
@@ -3,18 +3,13 @@ module Railties
...
@@ -3,18 +3,13 @@ module Railties
class
Subscriber
<
Rails
::
Subscriber
class
Subscriber
<
Rails
::
Subscriber
def
process_action
(
event
)
def
process_action
(
event
)
payload
=
event
.
payload
payload
=
event
.
payload
info
"
\n
Processed
#{
payload
[
:controller
]
}
#
#{
payload
[
:action
]
}
"
\
"to
#{
payload
[
:formats
].
join
(
', '
)
}
(for
#{
payload
[
:remote_ip
]
}
at
#{
event
.
time
.
to_s
(
:db
)
}
) "
\
"[
#{
payload
[
:method
].
to_s
.
upcase
}
]"
info
" Parameters:
#{
payload
[
:params
].
inspect
}
"
unless
payload
[
:params
].
blank?
info
" Parameters:
#{
payload
[
:params
].
inspect
}
"
unless
payload
[
:params
].
blank?
additions
=
ActionController
::
Base
.
log_process_action
(
payload
)
additions
=
ActionController
::
Base
.
log_process_action
(
payload
)
message
=
"Completed in %.0fms"
%
event
.
duration
message
=
"Completed in %.0fms"
%
event
.
duration
message
<<
" (
#{
additions
.
join
(
" | "
)
}
)"
unless
additions
.
blank?
message
<<
" (
#{
additions
.
join
(
" | "
)
}
)"
unless
additions
.
blank?
message
<<
"
|
#{
payload
[
:status
]
}
[
#{
payload
[
:request_uri
]
}
]
\n\n
"
message
<<
"
by
#{
payload
[
:controller
]
}
#
#{
payload
[
:action
]
}
[
#{
payload
[
:status
]
}
]
"
info
(
message
)
info
(
message
)
end
end
...
...
actionpack/lib/action_dispatch/railtie.rb
0 → 100644
浏览文件 @
27d9836a
require
"action_dispatch"
require
"rails"
module
ActionDispatch
class
Railtie
<
Rails
::
Railtie
plugin_name
:action_dispatch
require
"action_dispatch/railties/subscriber"
subscriber
ActionDispatch
::
Railties
::
Subscriber
.
new
class
MetalMiddlewareBuilder
def
initialize
(
metals
)
@metals
=
metals
end
def
new
(
app
)
ActionDispatch
::
Cascade
.
new
(
@metals
,
app
)
end
def
name
ActionDispatch
::
Cascade
.
name
end
alias_method
:to_s
,
:name
end
initializer
"action_dispatch.initialize_metal"
do
|
app
|
metal_root
=
"
#{
Rails
.
root
}
/app/metal"
load_list
=
app
.
config
.
metals
||
Dir
[
"
#{
metal_root
}
/**/*.rb"
]
metals
=
load_list
.
map
{
|
metal
|
metal
=
File
.
basename
(
metal
.
gsub
(
"
#{
metal_root
}
/"
,
''
),
'.rb'
)
require_dependency
metal
metal
.
camelize
.
constantize
}.
compact
middleware
=
MetalMiddlewareBuilder
.
new
(
metals
)
app
.
config
.
middleware
.
insert_before
(
:"ActionDispatch::ParamsParser"
,
middleware
)
end
# Prepare dispatcher callbacks and run 'prepare' callbacks
initializer
"action_dispatch.prepare_dispatcher"
do
|
app
|
# TODO: This used to say unless defined?(Dispatcher). Find out why and fix.
require
'rails/dispatcher'
unless
app
.
config
.
cache_classes
# Setup dev mode route reloading
routes_last_modified
=
app
.
routes_changed_at
reload_routes
=
lambda
do
unless
app
.
routes_changed_at
==
routes_last_modified
routes_last_modified
=
app
.
routes_changed_at
app
.
reload_routes!
end
end
ActionDispatch
::
Callbacks
.
before
{
|
callbacks
|
reload_routes
.
call
}
end
end
end
end
\ No newline at end of file
actionpack/lib/action_dispatch/railties/subscriber.rb
0 → 100644
浏览文件 @
27d9836a
module
ActionDispatch
module
Railties
class
Subscriber
<
Rails
::
Subscriber
def
before_dispatch
(
event
)
request
=
Request
.
new
(
event
.
payload
[
:env
])
path
=
request
.
request_uri
.
inspect
rescue
"unknown"
info
"
\n\n
Processing
#{
path
}
to
#{
request
.
formats
.
join
(
', '
)
}
"
<<
"(for
#{
request
.
remote_ip
}
at
#{
event
.
time
.
to_s
(
:db
)
}
) [
#{
request
.
method
.
to_s
.
upcase
}
]"
end
def
logger
ActionController
::
Base
.
logger
end
end
end
end
\ No newline at end of file
actionpack/test/activerecord/controller_runtime_test.rb
浏览文件 @
27d9836a
...
@@ -37,8 +37,8 @@ def test_log_with_active_record
...
@@ -37,8 +37,8 @@ def test_log_with_active_record
get
:show
get
:show
wait
wait
assert_equal
2
,
@logger
.
logged
(
:info
).
size
assert_equal
1
,
@logger
.
logged
(
:info
).
size
assert_match
/\(Views: [\d\.]+ms | ActiveRecord: [\d\.]+ms\)/
,
@logger
.
logged
(
:info
)[
1
]
assert_match
/\(Views: [\d\.]+ms | ActiveRecord: [\d\.]+ms\)/
,
@logger
.
logged
(
:info
)[
0
]
end
end
class
SyncSubscriberTest
<
ActionController
::
TestCase
class
SyncSubscriberTest
<
ActionController
::
TestCase
...
...
actionpack/test/controller/subscriber_test.rb
浏览文件 @
27d9836a
...
@@ -66,15 +66,10 @@ def set_logger(logger)
...
@@ -66,15 +66,10 @@ def set_logger(logger)
def
test_process_action
def
test_process_action
get
:show
get
:show
wait
wait
assert_equal
2
,
logs
.
size
assert_equal
1
,
logs
.
size
assert_match
/Processed\sAnother::SubscribersController#show/
,
logs
[
0
]
assert_match
/Completed/
,
logs
.
first
end
assert_match
/\[200\]/
,
logs
.
first
assert_match
/Another::SubscribersController#show/
,
logs
.
first
def
test_process_action_formats
get
:show
wait
assert_equal
2
,
logs
.
size
assert_match
/text\/html/
,
logs
[
0
]
end
end
def
test_process_action_without_parameters
def
test_process_action_without_parameters
...
@@ -87,23 +82,14 @@ def test_process_action_with_parameters
...
@@ -87,23 +82,14 @@ def test_process_action_with_parameters
get
:show
,
:id
=>
'10'
get
:show
,
:id
=>
'10'
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_equal
'Parameters: {"id"=>"10"}'
,
logs
[
1
]
assert_equal
'Parameters: {"id"=>"10"}'
,
logs
[
0
]
end
end
def
test_process_action_with_view_runtime
def
test_process_action_with_view_runtime
get
:show
get
:show
wait
wait
assert_match
/\(Views: [\d\.]+ms\)/
,
logs
[
1
]
assert_match
/\(Views: [\d\.]+ms\)/
,
logs
[
0
]
end
def
test_process_action_with_status_and_request_uri
get
:show
wait
last
=
logs
.
last
assert_match
/Completed/
,
last
assert_match
/200/
,
last
assert_match
/another\/subscribers\/show/
,
last
end
end
def
test_process_action_with_filter_parameters
def
test_process_action_with_filter_parameters
...
@@ -112,7 +98,7 @@ def test_process_action_with_filter_parameters
...
@@ -112,7 +98,7 @@ def test_process_action_with_filter_parameters
get
:show
,
:lifo
=>
'Pratik'
,
:amount
=>
'420'
,
:step
=>
'1'
get
:show
,
:lifo
=>
'Pratik'
,
:amount
=>
'420'
,
:step
=>
'1'
wait
wait
params
=
logs
[
1
]
params
=
logs
[
0
]
assert_match
/"amount"=>"\[FILTERED\]"/
,
params
assert_match
/"amount"=>"\[FILTERED\]"/
,
params
assert_match
/"lifo"=>"\[FILTERED\]"/
,
params
assert_match
/"lifo"=>"\[FILTERED\]"/
,
params
assert_match
/"step"=>"1"/
,
params
assert_match
/"step"=>"1"/
,
params
...
@@ -122,7 +108,7 @@ def test_redirect_to
...
@@ -122,7 +108,7 @@ def test_redirect_to
get
:redirector
get
:redirector
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_equal
"Redirected to http://foo.bar/"
,
logs
[
0
]
assert_equal
"Redirected to http://foo.bar/"
,
logs
[
0
]
end
end
...
@@ -130,7 +116,7 @@ def test_send_data
...
@@ -130,7 +116,7 @@ def test_send_data
get
:data_sender
get
:data_sender
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_match
/Sent data omg\.txt/
,
logs
[
0
]
assert_match
/Sent data omg\.txt/
,
logs
[
0
]
end
end
...
@@ -138,7 +124,7 @@ def test_send_file
...
@@ -138,7 +124,7 @@ def test_send_file
get
:file_sender
get
:file_sender
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_match
/Sent file/
,
logs
[
0
]
assert_match
/Sent file/
,
logs
[
0
]
assert_match
/test\/fixtures\/company\.rb/
,
logs
[
0
]
assert_match
/test\/fixtures\/company\.rb/
,
logs
[
0
]
end
end
...
@@ -147,7 +133,7 @@ def test_send_xfile
...
@@ -147,7 +133,7 @@ def test_send_xfile
get
:xfile_sender
get
:xfile_sender
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_match
/Sent X\-Sendfile header/
,
logs
[
0
]
assert_match
/Sent X\-Sendfile header/
,
logs
[
0
]
assert_match
/test\/fixtures\/company\.rb/
,
logs
[
0
]
assert_match
/test\/fixtures\/company\.rb/
,
logs
[
0
]
end
end
...
@@ -157,7 +143,7 @@ def test_with_fragment_cache
...
@@ -157,7 +143,7 @@ def test_with_fragment_cache
get
:with_fragment_cache
get
:with_fragment_cache
wait
wait
assert_equal
4
,
logs
.
size
assert_equal
3
,
logs
.
size
assert_match
/Exist fragment\? views\/foo/
,
logs
[
0
]
assert_match
/Exist fragment\? views\/foo/
,
logs
[
0
]
assert_match
/Write fragment views\/foo/
,
logs
[
1
]
assert_match
/Write fragment views\/foo/
,
logs
[
1
]
ensure
ensure
...
@@ -169,7 +155,7 @@ def test_with_page_cache
...
@@ -169,7 +155,7 @@ def test_with_page_cache
get
:with_page_cache
get
:with_page_cache
wait
wait
assert_equal
3
,
logs
.
size
assert_equal
2
,
logs
.
size
assert_match
/Write page/
,
logs
[
0
]
assert_match
/Write page/
,
logs
[
0
]
assert_match
/\/index\.html/
,
logs
[
0
]
assert_match
/\/index\.html/
,
logs
[
0
]
ensure
ensure
...
...
actionpack/test/dispatch/
notifications
_test.rb
→
actionpack/test/dispatch/
subscriber
_test.rb
浏览文件 @
27d9836a
require
'abstract_unit'
require
"abstract_unit"
require
"rails/subscriber/test_helper"
require
"action_dispatch/railties/subscriber"
class
NotificationsMiddlewareTest
<
ActionController
::
Integration
Test
module
DispatcherSubscriber
Test
Boomer
=
lambda
do
|
env
|
Boomer
=
lambda
do
|
env
|
req
=
ActionDispatch
::
Request
.
new
(
env
)
req
=
ActionDispatch
::
Request
.
new
(
env
)
case
req
.
path
case
req
.
path
...
@@ -14,21 +16,23 @@ class NotificationsMiddlewareTest < ActionController::IntegrationTest
...
@@ -14,21 +16,23 @@ class NotificationsMiddlewareTest < ActionController::IntegrationTest
App
=
ActionDispatch
::
Notifications
.
new
(
Boomer
)
App
=
ActionDispatch
::
Notifications
.
new
(
Boomer
)
def
setup
def
setup
@queue
=
ActiveSupport
::
Notifications
::
Fanout
.
new
Rails
::
Subscriber
.
add
(
:action_dispatch
,
ActionDispatch
::
Railties
::
Subscriber
.
new
)
@
notifier
=
ActiveSupport
::
Notifications
::
Notifier
.
new
(
@queue
)
@
app
=
App
ActiveSupport
::
Notifications
.
notifier
=
@notifi
er
sup
er
@events
=
[]
@events
=
[]
ActiveSupport
::
Notifications
.
subscribe
do
|*
args
|
ActiveSupport
::
Notifications
.
subscribe
do
|*
args
|
@events
<<
args
@events
<<
args
end
end
end
@app
=
App
def
set_logger
(
logger
)
ActionController
::
Base
.
logger
=
logger
end
end
test
"publishes notifications"
do
def
test_publishes_notifications
get
"/"
get
"/"
ActiveSupport
::
Notifications
.
notifier
.
wait
wait
assert_equal
2
,
@events
.
size
assert_equal
2
,
@events
.
size
before
,
after
=
@events
before
,
after
=
@events
...
@@ -42,13 +46,13 @@ def setup
...
@@ -42,13 +46,13 @@ def setup
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
end
end
test
"publishes notifications on failure"
do
def
test_publishes_notifications_even_on_failures
begin
begin
get
"/puke"
get
"/puke"
rescue
rescue
end
end
ActiveSupport
::
Notifications
.
notifier
.
wait
wait
assert_equal
3
,
@events
.
size
assert_equal
3
,
@events
.
size
before
,
after
,
exception
=
@events
before
,
after
,
exception
=
@events
...
@@ -59,11 +63,50 @@ def setup
...
@@ -59,11 +63,50 @@ def setup
assert_equal
'action_dispatch.after_dispatch'
,
after
[
0
]
assert_equal
'action_dispatch.after_dispatch'
,
after
[
0
]
assert_kind_of
Hash
,
after
[
4
][
:env
]
assert_kind_of
Hash
,
after
[
4
][
:env
]
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'GET'
,
after
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'action_dispatch.exception'
,
exception
[
0
]
assert_equal
'action_dispatch.exception'
,
exception
[
0
]
assert_kind_of
Hash
,
exception
[
4
][
:env
]
assert_kind_of
Hash
,
exception
[
4
][
:env
]
assert_equal
'GET'
,
exception
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_equal
'GET'
,
exception
[
4
][
:env
][
"REQUEST_METHOD"
]
assert_kind_of
RuntimeError
,
exception
[
4
][
:exception
]
assert_kind_of
RuntimeError
,
exception
[
4
][
:exception
]
end
end
def
test_subscriber_logs_notifications
get
"/"
wait
log
=
@logger
.
logged
(
:info
).
first
assert_equal
1
,
@logger
.
logged
(
:info
).
size
assert_match
%r{^Processing "/" to text/html}
,
log
assert_match
%r{
\(
for 127
\.
0
\.
0
\.
1}
,
log
assert_match
%r{
\[
GET
\]
}
,
log
end
def
test_subscriber_has_its_logged_flushed_after_request
assert_equal
0
,
@logger
.
flush_count
get
"/"
wait
assert_equal
1
,
@logger
.
flush_count
end
def
test_subscriber_has_its_logged_flushed_even_after_busted_requests
assert_equal
0
,
@logger
.
flush_count
begin
get
"/puke"
rescue
end
wait
assert_equal
1
,
@logger
.
flush_count
end
class
SyncSubscriberTest
<
ActionController
::
IntegrationTest
include
Rails
::
Subscriber
::
SyncTestHelper
include
DispatcherSubscriberTest
end
class
AsyncSubscriberTest
<
ActionController
::
IntegrationTest
include
Rails
::
Subscriber
::
AsyncTestHelper
include
DispatcherSubscriberTest
end
end
end
\ No newline at end of file
activerecord/lib/active_record/railties/subscriber.rb
浏览文件 @
27d9836a
...
@@ -12,7 +12,7 @@ def sql(event)
...
@@ -12,7 +12,7 @@ def sql(event)
name
=
color
(
name
,
:magenta
,
true
)
name
=
color
(
name
,
:magenta
,
true
)
end
end
debug
"
#{
name
}
#{
sql
}
"
debug
"
#{
name
}
#{
sql
}
"
end
end
def
odd?
def
odd?
...
...
railties/lib/rails.rb
浏览文件 @
27d9836a
require
"pathname"
require
'pathname'
require
'active_support'
require
'active_support'
require
'active_support/core_ext/kernel/reporting'
require
'active_support/core_ext/kernel/reporting'
require
'active_support/core_ext/logger'
require
'active_support/core_ext/logger'
require
'action_dispatch'
require
'rails/initializable'
require
'rails/initializable'
require
'rails/application'
require
'rails/application'
...
@@ -18,6 +17,8 @@
...
@@ -18,6 +17,8 @@
require
'rails/subscriber'
require
'rails/subscriber'
require
'rails/ruby_version_check'
require
'rails/ruby_version_check'
require
'action_dispatch/railtie'
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
# multibyte safe operations. Plugin authors supporting other encodings
# multibyte safe operations. Plugin authors supporting other encodings
# should override this behaviour and set the relevant +default_charset+
# should override this behaviour and set the relevant +default_charset+
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录