Behavior
模块允许确定如何显示弃用消息。你可以创建一个自定义行为或从 DEFAULT_BEHAVIORS
常量中设置任何行为。可用行为是
:raise
:stderr
-
将所有弃用警告记录到
$stderr
。 :log
-
将所有弃用警告记录到
Rails.logger
。 :notify
-
使用
ActiveSupport::Notifications
通知deprecation.rails
。 :report
-
使用
ActiveSupport::ErrorReporter
报告弃用。 :silence
-
不执行任何操作。在 Rails 中,设置
config.active_support.report_deprecations = false
以禁用所有行为。
设置行为仅影响在启动时间之后发生的弃用。有关更多信息,你可以阅读 behavior=
方法的文档。
属性
[RW] | debug | 是否打印回溯以及警告。 |
实例公共方法
behavior() 链接
返回当前行为,如果没有设置,则默认为 :stderr
。
源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 74 def behavior @behavior ||= [DEFAULT_BEHAVIORS[:stderr]] end
behavior=(behavior) 链接
将行为设置为指定值。可以是单个值、数组或响应 call
的对象。
可用的行为
:raise
:stderr
-
将所有弃用警告记录到
$stderr
。 :log
-
将所有弃用警告记录到
Rails.logger
。 :notify
-
使用
ActiveSupport::Notifications
通知deprecation.rails
。 :report
-
使用
ActiveSupport::ErrorReporter
报告弃用。 :silence
-
不执行任何操作。
设置行为仅影响在启动时间之后发生的弃用。宝石引发的弃用
警告不受此设置影响,因为它们发生在 Rails 启动之前。
deprecator = ActiveSupport::Deprecation.new
deprecator.behavior = :stderr
deprecator.behavior = [:stderr, :log]
deprecator.behavior = MyCustomHandler
deprecator.behavior = ->(message, callstack, deprecation_horizon, gem_name) {
# custom stuff
}
如果您正在使用 Rails,您可以设置 config.active_support.report_deprecations = false
以禁用所有弃用行为。这类似于 :silence
选项,但性能更高。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 111 def behavior=(behavior) @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) } end
disallowed_behavior() 链接
返回不允许的弃用的当前行为,或者如果未设置,则默认为 :raise
。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 79 def disallowed_behavior @disallowed_behavior ||= [DEFAULT_BEHAVIORS[:raise]] end
disallowed_behavior=(behavior) 链接
将不允许的弃用的行为(由ActiveSupport::Deprecation#disallowed_warnings=
配置的行为)设置为指定的值。与behavior=
一样,这可以是单个值、数组或响应 call
的对象。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 119 def disallowed_behavior=(behavior) @disallowed_behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) } end