Module: SemanticLogger::Deprecation

Defined in:
lib/semantic_logger/deprecation.rb

Overview

Reports the use of a deprecated method or option as a Ruby deprecation warning, see Kernel#warn, so that Ruby decides whether and where it is reported, as it does for its own deprecations, rather than a setting of Semantic Logger:

  • Only while deprecation warnings are on, see Warning[:deprecated]. They are off by default, so production stays quiet, and on in tests run by Minitest or test-unit, or with ruby -W:deprecated or RUBYOPT=-W:deprecated.
  • Written to $stderr, unless the application routes them elsewhere, such as to its log, or raises them in its tests, by overriding Warning.warn.

Each warning names the line of the application that used what is deprecated, rather than a line of Semantic Logger, of Ruby itself, or of a gem that Semantic Logger called with a block, see .application_location.

Copied from IOStreams::Deprecation, so that both libraries report deprecations the same way.

Class Method Summary collapse

Class Method Details

.warn(message) ⇒ Object

Reports that the application used something deprecated, unless deprecation warnings are off, with the message, which says what is deprecated and what to use instead, after "SemanticLogger: ", at the line of the application that used it, see .application_location, in the same form as Kernel#warn names the line of its caller, such as config/initializers/logging.rb:12: warning: SemanticLogger: old is deprecated, use new instead, or without a line when the call stack has none outside this library.

Returns nil.



56
57
58
59
60
61
62
63
64
# File 'lib/semantic_logger/deprecation.rb', line 56

def self.warn(message)
  return unless Warning[:deprecated]

  location = application_location
  text     = "#{PREFIX}#{message}"
  text     = "#{location.path}:#{location.lineno}: warning: #{text}" if location
  Kernel.warn(text, category: :deprecated)
  nil
end

.warning?(message) ⇒ Boolean

Returns [true|false] whether the message, as Kernel#warn passes it to Warning.warn, is a warning reported by .warn, so that an application, or its tests, can tell the deprecation warnings of Semantic Logger from the others.

Returns:

  • (Boolean)


68
69
70
# File 'lib/semantic_logger/deprecation.rb', line 68

def self.warning?(message)
  WARNING.match?(message)
end