2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
2012-01-20 14:45:29 -05:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
class BroadcastLoggerTest < TestCase
|
2016-01-05 11:37:04 -05:00
|
|
|
attr_reader :logger, :log1, :log2
|
2016-06-09 20:37:15 -04:00
|
|
|
|
|
|
|
setup do
|
2016-01-05 11:37:04 -05:00
|
|
|
@log1 = FakeLogger.new
|
|
|
|
@log2 = FakeLogger.new
|
|
|
|
@log1.extend Logger.broadcast @log2
|
|
|
|
@logger = @log1
|
2012-01-20 14:52:21 -05:00
|
|
|
end
|
2012-01-20 14:45:29 -05:00
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
Logger::Severity.constants.each do |level_name|
|
|
|
|
method = level_name.downcase
|
|
|
|
level = Logger::Severity.const_get(level_name)
|
|
|
|
|
|
|
|
test "##{method} adds the message to all loggers" do
|
|
|
|
logger.send(method, "msg")
|
|
|
|
|
|
|
|
assert_equal [level, "msg", nil], log1.adds.first
|
|
|
|
assert_equal [level, "msg", nil], log2.adds.first
|
|
|
|
end
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#close broadcasts to all loggers" do
|
2012-01-20 14:45:29 -05:00
|
|
|
logger.close
|
2016-06-09 20:37:15 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
assert log1.closed, "should be closed"
|
|
|
|
assert log2.closed, "should be closed"
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#<< shovels the value into all loggers" do
|
2012-01-20 14:45:29 -05:00
|
|
|
logger << "foo"
|
2016-06-09 20:37:15 -04:00
|
|
|
|
2016-01-05 11:37:04 -05:00
|
|
|
assert_equal %w{ foo }, log1.chevrons
|
|
|
|
assert_equal %w{ foo }, log2.chevrons
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#level= assigns the level to all loggers" do
|
|
|
|
assert_equal ::Logger::DEBUG, logger.level
|
|
|
|
logger.level = ::Logger::FATAL
|
|
|
|
|
|
|
|
assert_equal ::Logger::FATAL, log1.level
|
|
|
|
assert_equal ::Logger::FATAL, log2.level
|
2012-01-20 14:52:21 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#progname= assigns to all the loggers" do
|
2012-01-20 14:52:21 -05:00
|
|
|
assert_nil logger.progname
|
2016-06-09 20:37:15 -04:00
|
|
|
logger.progname = ::Logger::FATAL
|
|
|
|
|
|
|
|
assert_equal ::Logger::FATAL, log1.progname
|
|
|
|
assert_equal ::Logger::FATAL, log2.progname
|
2012-01-20 14:52:21 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#formatter= assigns to all the loggers" do
|
2012-01-20 14:52:21 -05:00
|
|
|
assert_nil logger.formatter
|
2016-06-09 20:37:15 -04:00
|
|
|
logger.formatter = ::Logger::FATAL
|
|
|
|
|
|
|
|
assert_equal ::Logger::FATAL, log1.formatter
|
|
|
|
assert_equal ::Logger::FATAL, log2.formatter
|
|
|
|
end
|
|
|
|
|
|
|
|
test "#local_level= assigns the local_level to all loggers" do
|
|
|
|
assert_equal ::Logger::DEBUG, logger.local_level
|
|
|
|
logger.local_level = ::Logger::FATAL
|
|
|
|
|
|
|
|
assert_equal ::Logger::FATAL, log1.local_level
|
|
|
|
assert_equal ::Logger::FATAL, log2.local_level
|
|
|
|
end
|
|
|
|
|
2016-10-24 06:26:13 -04:00
|
|
|
test "#silence does not break custom loggers" do
|
|
|
|
new_logger = FakeLogger.new
|
|
|
|
custom_logger = CustomLogger.new
|
|
|
|
custom_logger.extend(Logger.broadcast(new_logger))
|
|
|
|
|
|
|
|
custom_logger.silence do
|
|
|
|
custom_logger.error "from error"
|
|
|
|
custom_logger.unknown "from unknown"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [[::Logger::ERROR, "from error", nil], [::Logger::UNKNOWN, "from unknown", nil]], custom_logger.adds
|
|
|
|
assert_equal [[::Logger::ERROR, "from error", nil], [::Logger::UNKNOWN, "from unknown", nil]], new_logger.adds
|
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
test "#silence silences all loggers below the default level of ERROR" do
|
|
|
|
logger.silence do
|
|
|
|
logger.debug "test"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [], log1.adds
|
|
|
|
assert_equal [], log2.adds
|
|
|
|
end
|
|
|
|
|
|
|
|
test "#silence does not silence at or above ERROR" do
|
|
|
|
logger.silence do
|
|
|
|
logger.error "from error"
|
|
|
|
logger.unknown "from unknown"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [[::Logger::ERROR, "from error", nil], [::Logger::UNKNOWN, "from unknown", nil]], log1.adds
|
|
|
|
assert_equal [[::Logger::ERROR, "from error", nil], [::Logger::UNKNOWN, "from unknown", nil]], log2.adds
|
|
|
|
end
|
|
|
|
|
|
|
|
test "#silence allows you to override the silence level" do
|
|
|
|
logger.silence(::Logger::FATAL) do
|
|
|
|
logger.error "unseen"
|
|
|
|
logger.fatal "seen"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [[::Logger::FATAL, "seen", nil]], log1.adds
|
|
|
|
assert_equal [[::Logger::FATAL, "seen", nil]], log2.adds
|
2012-01-20 14:52:21 -05:00
|
|
|
end
|
|
|
|
|
2018-10-01 18:00:18 -04:00
|
|
|
test "Including top constant LoggerSilence is deprecated" do
|
|
|
|
assert_deprecated("Please use `ActiveSupport::LoggerSilence`") do
|
2018-10-03 08:38:57 -04:00
|
|
|
Class.new(CustomLogger) do
|
2018-10-01 18:00:18 -04:00
|
|
|
include ::LoggerSilence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-24 06:26:13 -04:00
|
|
|
class CustomLogger
|
2018-10-02 17:02:27 -04:00
|
|
|
include ActiveSupport::LoggerSilence
|
|
|
|
|
2012-01-20 14:45:29 -05:00
|
|
|
attr_reader :adds, :closed, :chevrons
|
2016-06-09 20:37:15 -04:00
|
|
|
attr_accessor :level, :progname, :formatter, :local_level
|
2012-01-20 14:45:29 -05:00
|
|
|
|
|
|
|
def initialize
|
2016-06-09 20:37:15 -04:00
|
|
|
@adds = []
|
|
|
|
@closed = false
|
|
|
|
@chevrons = []
|
|
|
|
@level = ::Logger::DEBUG
|
|
|
|
@local_level = ::Logger::DEBUG
|
|
|
|
@progname = nil
|
|
|
|
@formatter = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug(message, &block)
|
|
|
|
add(::Logger::DEBUG, message, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def info(message, &block)
|
|
|
|
add(::Logger::INFO, message, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def warn(message, &block)
|
|
|
|
add(::Logger::WARN, message, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(message, &block)
|
|
|
|
add(::Logger::ERROR, message, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fatal(message, &block)
|
|
|
|
add(::Logger::FATAL, message, &block)
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
|
2016-06-09 20:37:15 -04:00
|
|
|
def unknown(message, &block)
|
|
|
|
add(::Logger::UNKNOWN, message, &block)
|
2012-01-20 17:18:29 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 14:20:22 -04:00
|
|
|
def <<(x)
|
2012-01-20 14:45:29 -05:00
|
|
|
@chevrons << x
|
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def add(message_level, message = nil, progname = nil, &block)
|
2016-06-09 20:37:15 -04:00
|
|
|
@adds << [message_level, message, progname] if message_level >= local_level
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
end
|
2016-10-24 06:26:13 -04:00
|
|
|
|
|
|
|
class FakeLogger < CustomLogger
|
|
|
|
end
|
2012-01-20 14:45:29 -05:00
|
|
|
end
|
|
|
|
end
|