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= 方法的文档。
Attributes
| [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-
什么都不做。
设置行为仅影响启动后发生的弃用。由 gem 引发的 Deprecation 警告不受此设置影响,因为它们发生在 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